//
//  GameScene.swift
//  DouShouQiIOS
//
//  Created by Pierre FERREIRA on 24/05/2024.
//

import Foundation
import DouShouQiModel
import SpriteKit
import SwiftUI

class GameScene : SKScene{
    
    @ObservedObject var gameVm : GameVM = GameVM(andPlayer1: Player(withName: "Meruemu", andId: .player1)!,
                                                 andPlayer2: Player(withName: "Kumogi", andId: .player2)!)

    
    let imgBoard : SKSpriteNode = SKSpriteNode(imageNamed: "BoardImg")
    let defaultSize : CGSize = CGSize(width: 120, height: 120)
    
    var game : Game = try! Game(withRules: ClassicRules(),
                           andPlayer1: Player(withName: "Meruemu", andId: .player1)!,
                           andPlayer2: Player(withName: "Kumogi", andId: .player2)!)
        
//    let pieces : [ Owner : [Animal : SpriteMeeple]] =
//    [
//        .player1 : [
//            .rat : SpriteMeeple(imageName: "RatMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .cat : SpriteMeeple(imageName: "CatMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .dog : SpriteMeeple(imageName: "DogoMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .wolf : SpriteMeeple(imageName: "WolfMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .leopard : SpriteMeeple(imageName: "LeoMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .tiger : SpriteMeeple(imageName: "CatMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .lion : SpriteMeeple(imageName: "LionMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//            .elephant : SpriteMeeple(imageName: "ElphMeeple", size: CGSize(width: 120, height: 120), color: Color.player1),
//        ],
//
//        .player2: [
//            .rat : SpriteMeeple(imageName: "RatMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .cat : SpriteMeeple(imageName: "CatMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .dog : SpriteMeeple(imageName: "DogoMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .wolf : SpriteMeeple(imageName: "WolfMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .leopard : SpriteMeeple(imageName: "LeoMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .tiger : SpriteMeeple(imageName: "CatMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .lion : SpriteMeeple(imageName: "LionMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//            .elephant : SpriteMeeple(imageName: "ElphMeeple", size: CGSize(width: 120, height: 120), color: Color.player2),
//        ]
//    ]
    
    override init(size s: CGSize){
        super.init(size: s)
        self.addChild(imgBoard)
        
        self.displayBoard(game.board)
                
        self.scaleMode = .aspectFit
        self.anchorPoint = CGPoint(x: 0.5, y:0.5)
    }
    
    private func displayBoard(_ board : DouShouQiModel.Board){
        for row in 0..<board.nbRows{
            for col in 0..<board.nbColumns{
                if let p = board.grid[row][col].piece{
                    gameVm.pieces[p.owner]?[p.animal]?.cellPosition = CGPoint(x: row, y: col);
                    gameVm.pieces[p.owner]?[p.animal]?.imageNode.size = CGSize(width: 120, height: 120)
                    self.addChild(gameVm.pieces[p.owner]![p.animal]!);
                }
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}
