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

import SwiftUI

struct MainMenuView: View {
    
    @State private var showSplash = true
    @State private var currentImage: String?
    @State private var timer: Timer?
    @State private var showImage = false
    let images: [String] = [AppImages.SemiLion, AppImages.SemiDog, AppImages.SemiRat, AppImages.SemiWolf, AppImages.SemiLeopard, AppImages.SemiElephant, AppImages.SemiCat] // Add your image names here
    
    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: SelectPlayerView(), 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)
                            }
                            if let currentImage = currentImage {
                                Image(currentImage)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 200, height: 500)
                                    .transition(.opacity)
                                    .animation(.easeInOut(duration: 1), value: showImage)
                                    .onAppear {
                                        withAnimation {
                                            showImage = true
                                        }
                                    }
                            }
                        }
                    }
                    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 {
                            startTimer()
                            MusicPlayer.shared.playBackgroundMusic(music: "TitleScreenMusic")
                        }
                        .onDisappear {
                            stopTimer()
                            MusicPlayer.shared.stopBackgroundMusic()
                        }
                }
            }
        }
    }
    
    private func startTimer() {
        timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in
            changeImage()
        }
        changeImage() // Immediately change the image on appear
    }
    
    private func stopTimer() {
        timer?.invalidate()
        timer = nil
    }
    
    private func changeImage() {
        DispatchQueue.main.async {
            currentImage = images.randomElement()
        }
    }
}

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