// // TournamentCashierView.swift // PadelClub // // Created by Razmig Sarkissian on 17/04/2024. // import SwiftUI enum CashierDestination: Identifiable, Selectable { case summary case groupStage(GroupStage) case bracket(Round) case all(Tournament) var id: String { switch self { case .summary, .all: return String(describing: self) case .groupStage(let groupStage): return groupStage.id case .bracket(let round): return round.id } } func selectionLabel() -> String { switch self { case .summary: return "Bilan" case .groupStage(let groupStage): return groupStage.selectionLabel() case .bracket(let round): return round.selectionLabel() case .all: return "Tous" } } func badgeValue() -> Int? { switch self { case .summary: return nil case .groupStage(let groupStage): return groupStage.unsortedPlayers().filter({ $0.hasPaid() == false }).count case .bracket(let round): return round.seeds().flatMap { $0.unsortedPlayers() }.filter({ $0.hasPaid() == false }).count case .all(let tournament): return tournament.selectedPlayers().filter({ $0.hasPaid() == false }).count } } func badgeValueColor() -> Color? { return nil } func badgeImage() -> Badge? { switch self { case .summary: return nil case .groupStage(let groupStage): return groupStage.unsortedPlayers().allSatisfy({ $0.hasPaid() }) ? .checkmark : nil case .bracket(let round): return round.seeds().flatMap { $0.unsortedPlayers() }.allSatisfy({ $0.hasPaid() }) ? .checkmark : nil case .all(let tournament): return tournament.selectedPlayers().allSatisfy({ $0.hasPaid() }) ? .checkmark : nil } } } struct TournamentCashierView: View { var tournament: Tournament @State private var selectedDestination: CashierDestination? func allDestinations() -> [CashierDestination] { var allDestinations : [CashierDestination] = [] let tournamentHasEnded = tournament.hasEnded() if tournamentHasEnded { allDestinations.append(.summary) } allDestinations.append(.all(tournament)) let destinations : [CashierDestination] = tournament.groupStages().map { CashierDestination.groupStage($0) } allDestinations.append(contentsOf: destinations) tournament.rounds().forEach { round in if round.seeds().isEmpty == false { allDestinations.append(CashierDestination.bracket(round)) } } if tournamentHasEnded == false { allDestinations.append(.summary) } return allDestinations } init(tournament: Tournament) { self.tournament = tournament if tournament.hasEnded() { if tournament.players().anySatisfy({ $0.hasPaid() == false }) == false { _selectedDestination = .init(wrappedValue: .summary) } else { _selectedDestination = .init(wrappedValue: .all(tournament)) } } else { let gs = tournament.getActiveGroupStage() if let gs { _selectedDestination = State(wrappedValue: .groupStage(gs)) } else if let rs = tournament.getActiveRound(withSeeds: true) { _selectedDestination = State(wrappedValue: .bracket(rs)) } } } var body: some View { VStack(spacing: 0) { GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations(), nilDestinationIsValid: true) switch selectedDestination { case .none: CashierSettingsView(tournament: tournament) case .some(let selectedCall): switch selectedCall { case .summary: CashierDetailView(tournament: tournament) case .groupStage(let groupStage): CashierView(tournament: tournament, teams: groupStage.teams()) case .bracket(let round): CashierView(tournament: tournament, teams: round.seeds()) case .all(let tournament): CashierView(tournament: tournament, teams: tournament.selectedSortedTeams()) } } } .environment(tournament) .navigationBarTitleDisplayMode(.inline) .toolbarBackground(.visible, for: .navigationBar) .navigationTitle("Encaissement") } } #Preview { TournamentCashierView(tournament: Tournament.mock()) }