//
//  MainMenuView.swift
//  DouShouQi_App
//
//  Created by Nathan Verdier on 26/05/2024.
//

import SwiftUI
import SpriteKit
import DouShouQiModel

struct GameView: View {
    
    @StateObject var gameVM: PlayingGameVM
    
    @State private var showHome = false
    
    var body: some View {
        ZStack {
            VStack {
                TopGameBoard(gameVM: gameVM).frame(maxHeight: 200)
                SpriteView(scene: GameScene(size: CGSize(width: 700, height: 900), gameVM: gameVM))
            }
            .onAppear {
                MusicPlayer.shared.playBackgroundMusic(music: "GameMusic")
            }
            .onDisappear {
                MusicPlayer.shared.stopBackgroundMusic()
            }
            
            if (gameVM.game.isOver) {
                HStack {
                    Spacer()
                    
                    VStack(alignment: .center) {
                        Text("Game Over")
                            .font(.title2)
                        Text("Winner: \(gameVM.winner)")
                    }
                    .background(Colors.WinnerBackground)
                    
                    Spacer()
                }
                .onAppear {
                    Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { _ in
                        
                        // Il faut retourner au home
                    }
                }
            }
        }
    }
}

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