//
//  MainMenuView.swift
//  DouShouQi_App
//
//  Created by Rémi REGNAULT on 16/05/2024.
//

import SwiftUI

struct MainMenuView: View {
    
    @State private var showSplash = true
    
    var body: some View {
        NavigationView {
            VStack {
                if showSplash {
                    SplashScreenView()
                        .transition(.opacity)
                        .animation(.easeOut(duration: 3), value: showSplash)
                        .onAppear {
                            DispatchQueue.main.asyncAfter(deadline: .now() + 8) {
                                withAnimation {
                                    showSplash = false
                                }
                            }
                        }
                } else {
                    TitlePageFrame(Text: "DOU SHOU QI", ImageWidth: 200, ImageHeight: 200)
                    Spacer()
                    
                    VStack {
                        HStack {
                            VStack(spacing: 25) {
                                MainMenuButton(text: "Play", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
                                MainMenuButton(text: "Historique", destination: HistoricView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
                                MainMenuButton(text: "Best Scores", destination: ScoreBoardView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
                                MainMenuButton(text: "Players", destination: PlayersView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
                                MainMenuButton(text: "Settings", destination: SettingsView(), sound: "TitleScreenButtonSound", topRightCorner: 10, bottomRightCorner: 10)
                            }
                            Image(AppImages.SemiLion)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width: 200, height: 500)
                        }
                    }
                    Spacer()
                    HStack {
                        Text("Copyright @")
                            .font(.headline)
                            .frame(alignment: .trailing)
                        
                        Text("Dou Shou Qi Team")
                        
                        Spacer()
                        
                        Text("2024")
                            .frame(alignment: .trailing)
                    }
                    .padding()
                    .background(Color.white)
                    .overlay(
                        Rectangle()
                            .stroke(Color.black, lineWidth: 1)
                    )
                    
                    Spacer()
                        .onAppear {
                            MusicPlayer.shared.playBackgroundMusic(music: "TitleScreenMusic")
                        }
                        .onDisappear {
                            MusicPlayer.shared.stopBackgroundMusic()
                        }
                }
            }
        }
    }
}

struct MainMenuView_Previews: PreviewProvider {
    static var previews: some View {
        MainMenuView()
    }
}
