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

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    
    var entities = [GKEntity]()
    var graphs = [String : GKGraph]()
    var bases = [Base]()
    var player = [Base]()
    var popUpOnBaseCollision = SKSpriteNode()
    
    struct physicsBodyNumber {
        static let basePlayer1Number: UInt32 = 0b1
        static let basePlayer2Number: UInt32 = 0b10
        static let base5Number: UInt32 = 0b101
        static let base6Number: UInt32 = 0b110
        static let base7Number: UInt32 = 0b111
        static let emptyNumber: UInt32 = 0b100
    }

    override func sceneDidLoad() {
        
        self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
        self.physicsWorld.contactDelegate = self

        let maxX = self.size.width
        let midY = self.size.height / 2

        let basePlayer1 = Base(color: SKColor.red, position: CGPoint(x: maxX * 0.1, y: midY), name: "Player1", unitCount: 100, unitType: Unit.General)
        let basePlayer2 = Base(color: SKColor.blue, position: CGPoint(x: maxX * 0.9, y: midY), name: "Player2", unitCount: 100, unitType: Unit.General)
        basePlayer2.physicsBody = SKPhysicsBody(circleOfRadius: 50)
        basePlayer2.physicsBody?.categoryBitMask = physicsBodyNumber.basePlayer2Number
        basePlayer2.physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
        basePlayer2.physicsBody?.contactTestBitMask = physicsBodyNumber.base5Number & physicsBodyNumber.base6Number & physicsBodyNumber.base7Number
        
        popUpOnBaseCollision = SKSpriteNode(color: SKColor.blue , size: CGSize(width: self.size.width * 0.4, height: self.size.height * 0.4))
        popUpOnBaseCollision.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
        popUpOnBaseCollision.zPosition = 2

        self.addChild(basePlayer1)
        self.addChild(basePlayer2)
        player.append(basePlayer1)
        player.append(basePlayer2)
        createVirginBases()
        connectBases()
        addPhysicsBodyToBase()

    }
    override func didMove(to view: SKView) {
         let background = SKSpriteNode(imageNamed: "Background")
         background.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
         background.zPosition = -1
         background.size = self.size
         self.addChild(background)
     }
     
    
    func addPhysicsBodyToBase() {
        bases[5].physicsBody = SKPhysicsBody(circleOfRadius: 20)
        bases[5].physicsBody?.categoryBitMask = physicsBodyNumber.base5Number
        bases[5].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
        bases[5].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
        
        bases[6].physicsBody = SKPhysicsBody(circleOfRadius: 20)
        bases[6].physicsBody?.categoryBitMask = physicsBodyNumber.base6Number
        bases[6].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
        bases[6].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
        
        bases[7].physicsBody = SKPhysicsBody(circleOfRadius: 20)
        bases[7].physicsBody?.categoryBitMask = physicsBodyNumber.base7Number
        bases[7].physicsBody?.collisionBitMask = physicsBodyNumber.emptyNumber
        bases[7].physicsBody?.contactTestBitMask = physicsBodyNumber.basePlayer2Number
    }

    func createVirginBases() {
        for i in 0...7 {
           let base:Base
           let color = SKColor.green

            var position = CGPoint(x: 0, y: 0)
            switch i {
              case 0...2:
                  let width = self.size.width * 0.25

                  if i == 0 {
                   position = CGPoint(x: width, y: self.size.height * 0.25)
                  }
                  if i == 1 {
                   position = CGPoint(x: width, y: self.size.height * 0.5)
                  }
                  if i == 2{
                   position = CGPoint(x: width, y: self.size.height * 0.75)
                  }

              case 3...4:
                  let width = self.size.width * 0.5

                  if i == 3{
                   position = CGPoint(x: width, y: self.size.height * 0.333)
                  }
                  if i == 4{
                   position = CGPoint(x: width, y: self.size.height * 0.666)
                  }

              case 5...7:
                  let width = self.size.width * 0.75

                  if i == 5{
                   position = CGPoint(x: width, y: self.size.height * 0.25)
                  }
                  if i == 6{
                   position = CGPoint(x: width, y: self.size.height * 0.5)
                  }
                  if i == 7{
                   position = CGPoint(x: width, y: self.size.height * 0.75)
                  }

              default:
                   break
           }

            base = Base(color: color, position: position, name: "Base\(i)")
            bases.append(base)
            self.addChild(base)
      }
    }

    func addLine(base1: Base , base2: Base){
        let line = SKShapeNode()
        let linePath = CGMutablePath()
        linePath.move(to: base1.position)
        linePath.addLine(to: base2.position)
        line.path = linePath
        line.strokeColor = SKColor.white
        addChild(line)

    }
    
    func connectBases(){
        player[0].addAvailableBase(base: bases[0])
        player[0].addAvailableBase(base: bases[1])
        player[0].addAvailableBase(base: bases[2])

        bases[0].addAvailableBase(base: bases[3])

        bases[1].addAvailableBase(base: bases[3])
        bases[1].addAvailableBase(base: bases[4])

        bases[2].addAvailableBase(base: bases[4])

        bases[3].addAvailableBase(base: bases[5])
        bases[3].addAvailableBase(base: bases[6])

        bases[4].addAvailableBase(base: bases[7])
        bases[4].addAvailableBase(base: bases[6])

        player[1].addAvailableBase(base: bases[5])
        player[1].addAvailableBase(base: bases[6])
        player[1].addAvailableBase(base: bases[7])

        for base in player{
            for availableBase in base.availableBases{
                addLine(base1: base, base2: availableBase)
            }
        }

        for base in bases{
            for availableBase in base.availableBases{
                addLine(base1: base, base2: availableBase)
            }
        }

    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let locationUser = touch.location(in: self)
            if atPoint(locationUser) == popUpOnBaseCollision {
                popUpOnBaseCollision.removeFromParent()
            }
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches {
            let locationUser = touch.location(in: self)
            if atPoint(locationUser) == player[1]{
                player[1].position = locationUser
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        let maxX = self.size.width
        let midY = self.size.height / 2
        
        player[1].position = CGPoint(x:  maxX * 0.9, y: midY)
    }
    
    func didBegin(_ contact: SKPhysicsContact) {
        self.addChild(popUpOnBaseCollision)
    }
}
