//
//  GroupStagesView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 11/12/2023.
//

import SwiftUI

struct GroupStagesView: View {
    var tournament: Tournament
    @State private var selectedDestination: GroupStageDestination?
    
    enum GroupStageDestination: Selectable, Identifiable, Equatable {
        static func == (lhs: GroupStagesView.GroupStageDestination, rhs: GroupStagesView.GroupStageDestination) -> Bool {
            lhs.id == rhs.id
        }
        
        case all
        case groupStage(GroupStage)
        
        var id: String {
            switch self {
            case .all:
                return "all-group-stage"
            case .groupStage(let groupStage):
                return groupStage.id
            }
        }
        
        func selectionLabel() -> String {
            switch self {
            case .all:
                return "Tout"
            case .groupStage(let groupStage):
                return groupStage.groupStageTitle()
            }
        }
        
        func badgeValue() -> Int? {
            switch self {
            case .all:
                return nil
            case .groupStage(let groupStage):
                return groupStage.badgeValue()
            }
        }
        
        func badgeValueColor() -> Color? {
            return nil
        }
        
        func badgeImage() -> Badge? {
            switch self {
            case .all:
                return nil
            case .groupStage(let groupStage):
                return groupStage.badgeImage()
            }
        }
    }
    
    let allMatches: [Match]
    
    init(tournament: Tournament) {
        self.tournament = tournament
        self.allMatches = tournament.groupStagesMatches()

        if tournament.shouldVerifyGroupStage {
            _selectedDestination = State(wrappedValue: nil)
        } else if tournament.unsortedTeams().filter({ $0.groupStagePosition != nil }).isEmpty {
            _selectedDestination = State(wrappedValue: nil)
        } else {
            let gs = tournament.getActiveGroupStage()
            if let gs {
                _selectedDestination = State(wrappedValue: .groupStage(gs))
            }
        }
    }
    
    func allDestinations() -> [GroupStageDestination] {
        var allDestinations : [GroupStageDestination] = [.all]
        let groupStageDestinations : [GroupStageDestination] = tournament.groupStages().map { GroupStageDestination.groupStage($0) }
        allDestinations.append(contentsOf: groupStageDestinations)
        return allDestinations
    }

    @State private var runningMatches: [Match]?
    @State private var readyMatches: [Match]?
    @State private var availableToStart: [Match]?

    var body: some View {
        VStack(spacing: 0) {
            GenericDestinationPickerView(selectedDestination: $selectedDestination, destinations: allDestinations(), nilDestinationIsValid: true)
            switch selectedDestination {
            case .all:
                let finishedMatches = tournament.finishedMatches(allMatches)

                List {
                    MatchListView(section: "en cours", matches: runningMatches, matchViewStyle: .standardStyle, isExpanded: false)
                    MatchListView(section: "prêt à démarrer", matches: availableToStart, matchViewStyle: .standardStyle, isExpanded: false)
                    MatchListView(section: "à lancer", matches: readyMatches, matchViewStyle: .standardStyle, isExpanded: false)
                    MatchListView(section: "terminés", matches: finishedMatches, matchViewStyle: .standardStyle, isExpanded: false)
                }
                .task {
                    runningMatches = await tournament.asyncRunningMatches(allMatches)
                    availableToStart = await tournament.availableToStart(allMatches, in: runningMatches ?? [])
                    readyMatches = await tournament.readyMatches(allMatches)
                }
                .navigationTitle("Toutes les poules")
            case .groupStage(let groupStage):
                GroupStageView(groupStage: groupStage).id(groupStage.id)
            case nil:
                GroupStageSettingsView()
                    .navigationTitle("Réglages")
            }
        }
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}
