//
//  TournamentView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 29/02/2024.
//

import SwiftUI
import LeStorage
import TipKit

struct TournamentView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(NavigationViewModel.self) var navigation: NavigationViewModel
    @State var tournament: Tournament
    var presentationContext: PresentationContext = .agenda
    
    let tournamentSelectionTip: TournamentSelectionTip = TournamentSelectionTip()
    let tournamentRunningTip: TournamentRunningTip = TournamentRunningTip()
    
    var selectedTournamentId: Binding<String> { Binding(
        get: { tournament.id },
        set: { id in
            if let tournamentFound: Tournament = Store.main.findById(id) {
                tournament = tournamentFound
            }
        }
    )}

    var lastDataSource: String? {
        dataStore.appSettings.lastDataSource
    }

    var _lastDataSourceDate: Date? {
        guard let lastDataSource else { return nil }
        return URL.importDateFormatter.date(from: lastDataSource)
    }

    var body: some View {
        VStack(spacing: 0.0) {
            List {
                TipView(tournamentRunningTip)
                    .tipStyle(tint: nil)
                
                if tournament.state() != .finished {
                    SubscriptionInfoView()
                }

                switch tournament.state() {
                case .canceled:
                    Section {
                        RowButtonView("Reprendre le tournoi", role: .destructive) {
                            tournament.isCanceled = false
                            tournament.endDate = nil
                            _save()
                        }
                    }
                case .initial:
                    Section {
                        TournamentInscriptionView(tournament: tournament)
                    }
                    TournamentInitView(tournament: tournament)
                case .build:
                    Section {
                        TournamentInscriptionView(tournament: tournament)
                    }
                    TournamentBuildView(tournament: tournament)
                    TournamentInitView(tournament: tournament)
                case .running:
                    TournamentBuildView(tournament: tournament)
                    TournamentRunningView(tournament: tournament)
                case .finished:
                    TournamentBuildView(tournament: tournament)
                    TournamentRunningView(tournament: tournament)
                }
            }
        }
        .id(tournament.id)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationDestination(for: Screen.self, destination: { screen in
            Group {
                switch screen {
                case .structure:
                    TableStructureView()
                case .settings:
                    TournamentSettingsView()
                case .inscription:
                    InscriptionManagerView(tournament: tournament)
                case .groupStage:
                    GroupStagesView(tournament: tournament)
                case .round:
                    RoundsView(tournament: tournament)
                case .schedule:
                    TournamentScheduleView(tournament: tournament)
                case .cashier:
                    TournamentCashierView(tournament: tournament)
                case .call:
                    TournamentCallView(tournament: tournament)
                case .rankings:
                    TournamentRankView()
                case .broadcast:
                    BroadcastView()
                        .environment(navigation)
                case .event:
                    if let event = tournament.eventObject() {
                        EventView(event: event)
                    }
                case .print:
                    PrintSettingsView(tournament: tournament)
                }
            }
            .environment(tournament)
        })
        .navigationBarTitleDisplayMode(.inline)
        .toolbarTitleMenu {
            if let event = tournament.eventObject() {
                Picker(selection: selectedTournamentId) {
                    ForEach(event.tournaments) { tournament in
                        Text(tournament.tournamentTitle(.title)).tag(tournament.id as String)
                    }
                } label: {
                    
                }
                
                Divider()
                
                NavigationLink(value: Screen.event) {
                    Text("Gestion de l'événement")
                }
            }
            
        }
        .toolbarBackground(.visible, for: .navigationBar)
        .toolbar {
            ToolbarItem(placement: .principal) {
                VStack(spacing: -4.0) {
                    Text(tournament.tournamentTitle(.title)).font(.headline)
                    Text(tournament.formattedDate())
                        .font(.subheadline).foregroundStyle(.secondary)
                }
                .popoverTip(tournamentSelectionTip)
                .onAppear {
                    TournamentSelectionTip.tournamentCount = tournament.eventObject()?.tournaments.count
                }
            }
            
            if (presentationContext == .agenda || tournament.state() == .running) && tournament.isCanceled == false {
                ToolbarItem(placement: .topBarTrailing) {
                    Menu {
                        
                        #if DEBUG
                        Button {
                            DataStore.shared.copyToLocalServer(tournament: self.tournament)
                        } label: {
                            Label("Copier sur serveur local", systemImage: "rectangle.portrait.and.arrow.right")
                        }
                        #endif
                        
                        if presentationContext == .agenda {
                            Button {
                                navigation.openTournamentInOrganizer(tournament)
                            } label: {
                                Label("Voir dans le gestionnaire", systemImage: "line.diagonal.arrow")
                            }
                        }
                        
                        Divider()
                        if tournament.state() == .running || tournament.state() == .finished {
                            NavigationLink(value: Screen.event) {
                                Text("Gestion de l'événement")
                            }
                            
                            NavigationLink(value: Screen.settings) {
                                LabelSettings()
                            }
                            NavigationLink(value: Screen.structure) {
                                LabelStructure()
                            }

                            NavigationLink(value: Screen.broadcast) {
                                Label("Publication", systemImage: "airplayvideo")
                            }
                            
                            NavigationLink(value: Screen.print) {
                                Label("Imprimer", systemImage: "printer")
                            }

                        }
                    } label: {
                        LabelOptions()
                    }
                }
            }
        }
        .onAppear {
            TournamentRunningTip.isRunning = tournament.state() == .running
            Logger.log("Payment = \(String(describing: self.tournament.payment)), canceled = \(self.tournament.isCanceled)")
        }
    }

    private func _save() {
        do {
            try dataStore.tournaments.addOrUpdate(instance: tournament)
        } catch {
            Logger.error(error)
        }
    }
}

//#Preview {
//    NavigationStack {
//        TournamentView(tournament: Tournament.mock(), presentationContext: .agenda)
//    }
//}
