//
//  RoundsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 30/03/2024.
//

import SwiftUI

struct RoundsView: View {
    var tournament: Tournament
    @State private var selectedRound: Round?
    @State private var isEditingTournamentSeed = false

    init(tournament: Tournament) {
        self.tournament = tournament
        let availableSeeds = tournament.availableSeeds()
        if tournament.shouldVerifyBracket && availableSeeds.isEmpty {
            _selectedRound = State(wrappedValue: nil)
        } else {
            _selectedRound = State(wrappedValue: tournament.getActiveRound())
        }
        if availableSeeds.isEmpty == false || tournament.availableQualifiedTeams().isEmpty == false {
            _isEditingTournamentSeed = State(wrappedValue: true)
        }
    }
    
    var body: some View {
        VStack(spacing: 0) {
            GenericDestinationPickerView(selectedDestination: $selectedRound, destinations: tournament.rounds(), nilDestinationIsValid: true)
            switch selectedRound {
            case .none:
                RoundSettingsView()
                    .navigationTitle("Réglages")
            case .some(let selectedRound):
                RoundView(round: selectedRound).id(selectedRound.id)
                    .navigationTitle(selectedRound.roundTitle())
            }
        }
        .environment(\.isEditingTournamentSeed, $isEditingTournamentSeed)
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}

//#Preview {
//    RoundsView(tournament: Tournament.mock())
//}
