// // TournamentCellView.swift // PadelClub // // Created by Razmig Sarkissian on 29/02/2024. // import SwiftUI struct TournamentCellView: View { @EnvironmentObject var dataStore: DataStore @Environment(NavigationViewModel.self) private var navigation let tournament: FederalTournamentHolder let color: Color = .black var displayStyle: DisplayStyle = .wide var event: Event? { guard let federalTournament = tournament as? FederalTournament else { return nil } return dataStore.events.first(where: { $0.tenupId == federalTournament.id.string }) } var body: some View { ForEach(tournament.tournaments, id: \.id) { build in _buildView(build, existingTournament: event?.existingBuild(build)) } } private func _buildView(_ build: any TournamentBuildHolder, existingTournament: Tournament?) -> some View { HStack { DateBoxView(date: tournament.startDate, displayStyle: .short) Rectangle() .fill(color) .frame(width: 2) VStack(alignment: .leading, spacing: -2) { if let tournament = tournament as? Tournament { Text(tournament.locationLabel(displayStyle)) .font(.caption2) } else { Text(tournament.clubLabel()) .font(.caption2) } HStack(alignment: .bottom) { Text(build.level.localizedLabel()) if displayStyle == .wide { VStack(alignment: .leading, spacing: 0) { Text(build.category.localizedLabel()) Text(build.age.localizedLabel()) } .font(.caption2) } Spacer() if let tournament = tournament as? Tournament, displayStyle == .wide { Text(tournament.sortedTeams().count.formatted()) } else if let federalTournament = tournament as? FederalTournament { Button { if let existingTournament { navigation.agendaDestination = .activity navigation.tournament = existingTournament } else { let event = federalTournament.getEvent() let newTournament = Tournament.newEmptyInstance() newTournament.event = event.id newTournament.tournamentLevel = build.level newTournament.tournamentCategory = build.category newTournament.federalTournamentAge = build.age newTournament.dayDuration = federalTournament.dayDuration newTournament.startDate = federalTournament.startDate try? dataStore.tournaments.addOrUpdate(instance: newTournament) } } label: { Image(systemName: existingTournament != nil ? "checkmark.circle.fill" : "square.and.arrow.down") .resizable() .scaledToFit() .frame(height: 28) .tint(existingTournament != nil ? Color.green : nil) } } } .font(displayStyle == .wide ? .title : .title3) if displayStyle == .wide { HStack { Text(tournament.durationLabel()) Spacer() if let tournament = tournament as? Tournament { Text("équipes") } } Text(tournament.subtitleLabel()) } else { Text(build.category.localizedLabel()) Text(build.age.localizedLabel()) } } } .font(.caption2) } }