//
//  GameView.swift
//  DouShouQiIOS
//
//  Created by Pierre FERREIRA on 22/05/2024.
//

import SwiftUI
import SpriteKit

struct GameView: View {
    
    @Environment(\.colorScheme) var colorScheme

    @State private var turnNumber = 1
    @State private var gameTime = 0.0 // Temps en secondes
    @State private var statusMessage = "C'est votre tour"
    @State private var player1Pieces = 8
    @State private var player2Pieces = 8

    var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    let gridItems = Array(repeating: GridItem(.flexible()), count: 8)

    ///SK
    var gs : GameScene = GameScene(size: CGSizeMake(940,740))

    
    var body: some View {
        ZStack{
            Rectangle().fill(Color.bgColor).edgesIgnoringSafeArea(.all)
            VStack {
                VStack {
                    Text(statusMessage)
                        .font(.title)
                        .padding(.top, 20)
                    
                    HStack {
                        ClassicTextDisplay(text: "Tour: \(turnNumber)")
                        
                        Spacer()
                        
                        ClassicTextDisplay(text: "Temps: \(Int(gameTime))s").onReceive(timer) { _ in
                            gameTime += 1
                        }
                    }
                    .padding([.leading, .trailing], 20)
                }

                /*
                 
                 ///Old Grid Support
                 
                 LazyVGrid(columns: gridItems, spacing: 5) {
                    ForEach(0..<64) { index in
                        Rectangle()
                            .foregroundColor((index / 8 + index % 8) % 2 == 0 ? .orange : .gray.opacity(0.2))
                            .frame(height: 35)
                            .cornerRadius(10)
                    }
                }
                .padding(10)
                .background(Color.yellow)
                .cornerRadius(15)
                .padding(20)
                 
                 */
                SpriteView(scene: gs)
                    .frame(width: 350, height: 275)
                    .padding(10)
                    .background(Color.primaryColor)
                    .cornerRadius(15)
                    .padding(20)
                
                
                HStack {
                    VStack {
                        Text("Pièces Joueur 1")
                            .font(.headline)
                            .padding(.bottom, 5)
                        
                        Text("\(player1Pieces)")
                            .font(.largeTitle)
                            .padding()
                            .background(Color.player1.opacity(colorScheme == .dark ? 0.5 : 0.2))
                            .cornerRadius(10)
                    }
                    .padding(20)

                    Spacer()

                    VStack {
                        Text("Pièces Joueur 2")
                            .font(.headline)
                            .padding(.bottom, 5)
                        
                        Text("\(player2Pieces)")
                            .font(.largeTitle)
                            .padding()
                            .background(Color.player2.opacity(colorScheme == .dark ? 0.5 : 0.2))
                            .cornerRadius(10)
                    }
                    .padding(20)
                }
            }
            .navigationBarTitle("DouShouQi", displayMode: .inline)
        }
    }
}

struct GameView_Previews: PreviewProvider {
    static var previews: some View {
        GameView()
    }
}
