You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
PadelClub/PadelClub/Views/Tournament/Screen/TournamentCashierView.swift

157 lines
4.8 KiB

//
// TournamentCashierView.swift
// PadelClub
//
// Created by Razmig Sarkissian on 17/04/2024.
//
import SwiftUI
enum CashierDestination: Identifiable, Selectable, Equatable {
static func == (lhs: CashierDestination, rhs: CashierDestination) -> Bool {
return lhs.id == rhs.id
}
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 displayImageIfValueZero() -> Bool {
return true
}
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 nil
}
}
func badgeValueColor() -> Color? {
return nil
}
func badgeImage() -> Badge? {
switch self {
case .summary:
return nil
case .all:
return nil
default:
return .checkmark
}
}
}
struct TournamentCashierView: View {
var tournament: Tournament
@State private var selectedDestination: CashierDestination?
@StateObject private var cashierViewModel: CashierViewModel = CashierViewModel()
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())
.environmentObject(cashierViewModel)
case .bracket(let round):
CashierView(tournament: tournament, teams: round.seeds())
.environmentObject(cashierViewModel)
case .all(let tournament):
CashierView(tournament: tournament, teams: tournament.selectedSortedTeams())
.environmentObject(cashierViewModel)
}
}
}
.environment(tournament)
.navigationBarTitleDisplayMode(.inline)
.toolbarBackground(.visible, for: .navigationBar)
.navigationTitle("Encaissement")
}
}
#Preview {
TournamentCashierView(tournament: Tournament.mock())
}