// // 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 { Binding( get: { tournament.id }, set: { id in if let tournamentFound: Tournament = Store.main.findById(id) { self.tournament = tournamentFound } } )} var lastDataSource: String? { dataStore.appSettings.lastDataSource } var _lastDataSourceDate: Date? { guard let lastDataSource else { return nil } return URL.importDateFormatter.date(from: lastDataSource) } init(tournament: Tournament, presentationContext: PresentationContext = .agenda) { self.tournament = tournament self.presentationContext = presentationContext } var body: some View { VStack(spacing: 0.0) { List { if tournament.state() != .finished && tournament.payment == nil { 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) } } } .environment(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() { if presentationContext == .agenda { 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 tournament.isCanceled == false { ToolbarItem(placement: .topBarTrailing) { Menu { #if _DEBUG_OPTIONS Button { do { try self.tournament.payIfNecessary() } catch { Logger.error(error) } } label: { Label("Payer le tournoi", systemImage: "dollarsign.circle.fill") } #endif if presentationContext == .agenda { Button { navigation.openTournamentInOrganizer(tournament) } label: { Label("Voir dans le gestionnaire", systemImage: "line.diagonal.arrow") } Divider() } 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") } Divider() NavigationLink { TournamentStatusView(tournament: tournament) } label: { Text("Gestion du tournoi") Text("Annuler, supprimer ou terminer le tournoi") } } label: { LabelOptions() .popoverTip(tournamentRunningTip) } } } } .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) // } //}