//
//  MenuScene.swift
//  GoldWars
//
//  Created by Aldin Duraki on 18.04.20.
//  Copyright © 2020 SP2. All rights reserved.
//

import SpriteKit

class MenuScene: SKScene {

    var entityManager: EntityManager!

    override func sceneDidLoad() {
        entityManager = EntityManager(scene: self)
        let midX = self.size.width / 2
        let midY = self.size.height / 2
        entityManager.add(Button(name: "startGameButton",
                                 iconName: "",
                                 text: "Start Game",
                                 position: CGPoint(x: midX, y: midY),
                                 onButtonPress: {
                                    self.loadScene(scene: GameScene(size: self.size))
                                 }))
        entityManager.add(Button(name: "settingsButton",
                                 iconName: "",
                                 text: "Settings",
                                 position: CGPoint(x: midX, y: midY - 80 ),
                                 onButtonPress: {
                                    //TODO: create Settings Scene
                                 }))
        entityManager.add(Background(size: self.size))
    }

    func loadScene(scene: SKScene) {
        let transition = SKTransition.flipVertical(withDuration: 0.5)
        self.view?.presentScene(scene, transition: transition)
    }

    override func update(_ currentTime: TimeInterval) {
        entityManager.getBackground()!.update(deltaTime: currentTime)
        entityManager.getButtonByName(buttonName: "startGameButton").component(ofType: ButtonComponent.self)?.buttonNode.isEnabled = GameCenterHelper.isAuthenticated
    }
}
