//
//  TournamentCallView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 16/04/2024.
//

import SwiftUI

enum CallDestination: Identifiable, Selectable, Equatable {
    
    static func == (lhs: CallDestination, rhs: CallDestination) -> Bool {
        return lhs.id == rhs.id
    }

    
    case seeds(Tournament)
    case groupStages(Tournament)
    
    var id: String {
        switch self {
        case .seeds:
            return "seed"
        case .groupStages:
            return "groupStage"
        }
    }
    
    func selectionLabel() -> String {
        switch self {
        case .seeds:
            return "Têtes de série"
        case .groupStages:
            return "Poules"
        }
    }
    
    func badgeValue() -> Int? {
        switch self {
        case .seeds(let tournament):
            let allSeedCalled = tournament.seeds().filter({ tournament.isStartDateIsDifferentThanCallDate($0) || $0.callDate == nil })
            return allSeedCalled.count
        case .groupStages(let tournament):
            let allSeedCalled = tournament.groupStageTeams().filter({ tournament.isStartDateIsDifferentThanCallDate($0) || $0.callDate == nil })
            return allSeedCalled.count
        }
    }
    
    func badgeValueColor() -> Color? {
        return nil
    }
    
    func badgeImage() -> Badge? {
        switch self {
        case .seeds(let tournament):
            let allSeedCalled = tournament.seeds().allSatisfy({ tournament.isStartDateIsDifferentThanCallDate($0) == false })
            return allSeedCalled ? .checkmark : nil
        case .groupStages(let tournament):
            let allSeedCalled = tournament.groupStageTeams().allSatisfy({ tournament.isStartDateIsDifferentThanCallDate($0) == false })
            return allSeedCalled ? .checkmark : nil
        }
    }

}


struct TournamentCallView: View {
    var tournament: Tournament
    @State private var selectedDestination: CallDestination?
    let allDestinations: [CallDestination]
    
    init(tournament: Tournament) {
        self.tournament = tournament
        var destinations = [CallDestination]()
        let groupStageTeams = tournament.groupStageTeams()
        if groupStageTeams.isEmpty == false {
            destinations.append(.groupStages(tournament))
            self._selectedDestination = State(wrappedValue: .groupStages(tournament))
        }
        if tournament.seededTeams().isEmpty == false {
            destinations.append(.seeds(tournament))
            if groupStageTeams.isEmpty {
                self._selectedDestination = State(wrappedValue: .seeds(tournament))
            }
        }
        self.allDestinations = destinations
    }
    
    var body: some View {
        VStack(spacing: 0) {
            GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations, nilDestinationIsValid: true)
            switch selectedDestination {
            case .none:
                CallSettingsView()
            case .some(let selectedCall):
                switch selectedCall {
                case .groupStages:
                    GroupStageCallingView()
                case .seeds:
                    SeedsCallingView()
                }
            }
        }
        .environment(tournament)
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationTitle("Convocations")
    }

}

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