//
//  Base.swift
//  GoldWars
//
//  Created by Marcel Schwarz on 18.04.20.
//  Copyright © 2020 SP2. All rights reserved.
//

import SpriteKit
import GameplayKit

class Base : SKSpriteNode{
    public static let colorSelected = SKColor.yellow
    var availableBases = [Base]()
    var defaultColor = SKColor.green
    var unitType: Unit = Unit.General;
    var unitCount: Int = 0;

    init(color: UIColor, position: CGPoint, name: String, unitCount: Int = 0, unitType: Unit = Unit.General) {
        super.init(texture: SKTexture(imageNamed: "Base"),
                   color: color,
                   size: CGSize(width: 50.0, height: 50.0)
        )

        defaultColor = color
        self.colorBlendFactor = 1
        self.name = name
        self.position = position
        self.zPosition = 2
        self.isUserInteractionEnabled = true
        self.unitCount = unitCount
        self.unitType = unitType
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func addAvailableBase(base: Base) {
        availableBases.append(base)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       self.run(
               SKAction.sequence(
                   [
                       SKAction.resize(byWidth: 20, height: 20, duration: 0.5),
                       SKAction.resize(byWidth: -20, height: -20, duration: 0.5)
                   ]
               )
           )
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.color = SKColor.yellow

        for base in availableBases {
            base.color = Base.colorSelected
            base.size = CGSize(width: 60, height: 60)
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.color = defaultColor
        for base in availableBases {
            base.color = base.defaultColor
            base.size = CGSize(width: 50, height: 50)
        }
    }

}
