//
//  GameScene.swift
//  DouShouQi_App
//
//  Created by Nathan Verdier on 28/05/2024.
//

import Foundation
import SpriteKit
import DouShouQiModel

class GameScene : SKScene {
    var game: Game = try! Game(withRules: ClassicRules(), andPlayer1: RandomPlayer(withName: "Rayhan", andId: .player1)!, andPlayer2: RandomPlayer(withName: "Rémi", andId: .player2)!)
    
    let pieces: [Owner : [Animal:SpriteMeeple]] = [
        .player1: [
            .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .tiger: SpriteMeeple(imageNamed: "", size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
            .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: Colors.MeepleP1!),
        ],
        .player2: [
            .rat: SpriteMeeple(imageNamed: AppImages.Rat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .cat: SpriteMeeple(imageNamed: AppImages.Cat, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .dog: SpriteMeeple(imageNamed: AppImages.Dog, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .wolf: SpriteMeeple(imageNamed: AppImages.Wolf, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .leopard: SpriteMeeple(imageNamed: AppImages.Leopard, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .lion: SpriteMeeple(imageNamed: AppImages.Lion, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .tiger: SpriteMeeple(imageNamed: "", size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
            .elephant: SpriteMeeple(imageNamed: AppImages.Elephant, size: CGSize(width: 100, height: 100), backgroundColor: .blue, imageRotation: 180),
        ],
    ]
    
    let imageBoard: SKSpriteNode = SKSpriteNode(imageNamed: AppImages.boardGame)
    
    override init(size: CGSize) {
        super.init(size: size)
        imageBoard.size = size
        //self.scaleMode = .aspectFit
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        self.backgroundColor = .white
        
        self.addChild(imageBoard)
        
        for piece in pieces.flatMap({owner, pieces in pieces.values}) {
            self.addChild(piece)
        }
        
        initializeBoard(game.board)
        displayBoard(game.board)
    }
    
    func initializeBoard(_ board: Board) {
            let player1Animals: [Animal] = [.rat, .cat, .dog, .wolf, .leopard, .lion, .tiger, .elephant]
            let player2Animals: [Animal] = [.rat, .cat, .dog, .wolf, .leopard, .lion, .tiger, .elephant]

            for (index, animal) in player1Animals.enumerated() {
                pieces[.player1]?[animal]?.cellPosition = CGPoint(x: index, y: 0)
                pieces[.player1]?[animal]?.cellPosition = CGPoint(x: index, y: 1)
            }

            for (index, animal) in player2Animals.enumerated() {
                pieces[.player2]?[animal]?.cellPosition = CGPoint(x: index, y: board.nbRows - 1)
                pieces[.player2]?[animal]?.cellPosition = CGPoint(x: index, y: board.nbRows - 2)
            }
        }

    
    func displayBoard(_ board: Board) {
        for row in 0..<board.nbRows {
            for col in 0..<board.nbColumns {
                if let p = board.grid[row][col].piece, let sprite = pieces[p.owner]?[p.animal] {
                    sprite.cellPosition = CGPoint(x: row, y: col)
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder);
    }
}
