//
//  EventTournamentsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 17/05/2024.
//

import SwiftUI
import LeStorage

struct EventTournamentsView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(NavigationViewModel.self) private var navigation
    let event: Event
    @State private var newTournament: Tournament?

    var presentTournamentCreationView: Binding<Bool> { Binding(
        get: { newTournament != nil },
        set: { isPresented in
            if isPresented == false {
                newTournament = nil
            }
        }
    )}

    var body: some View {
        let tournaments = event.tournaments
        List {
            ForEach(tournaments) { tournament in
                TournamentCellView(tournament: tournament)
                    .contextMenu {
                        Button {
                            navigation.openTournamentInOrganizer(tournament)
                        } label: {
                            Label("Voir dans le gestionnaire", systemImage: "line.diagonal.arrow")
                        }
                    }
            }
        }
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                BarButtonView("Ajouter une épreuve", icon: "plus.circle.fill") {
                    let tournament = Tournament.newEmptyInstance()
                    newTournament = tournament
                }
            }
        }
        .headerProminence(.increased)
        .sheet(isPresented: presentTournamentCreationView) {
            if let newTournament {
                NavigationStack {
                    List {
                        Section {
                            TournamentConfigurationView(tournament: newTournament)
                        }
                        Section {
                            RowButtonView("Ajouter l'épreuve") {
                                newTournament.event = event.id
                                newTournament.courtCount = event.eventCourtCount()
                                newTournament.startDate = event.eventStartDate()
                                newTournament.dayDuration = event.eventDayDuration()
                                newTournament.setupFederalSettings()
                                
                                do {
                                    try dataStore.tournaments.addOrUpdate(instance: newTournament)
                                } catch {
                                    Logger.error(error)
                                }
                                
                                self.newTournament = nil
                            }
                        }
                    }
                    .toolbarBackground(.visible, for: .navigationBar)
                    .navigationBarTitleDisplayMode(.inline)
                    .navigationTitle("Nouvelle épreuve")
                    .toolbar {
                        ToolbarItem(placement: .cancellationAction) {
                            Button("Annuler", role: .cancel) {
                                self.newTournament = nil
                            }
                        }
                    }
                }
            }
        }
    }
}
