//
//  TeamRestingView.swift
//  PadelClub
//
//  Created by razmig on 19/10/2024.
//

import SwiftUI

struct TeamRestingView: View {
    @Environment(Tournament.self) var tournament: Tournament
    @State private var displayMode: DisplayMode = .teams
    @State private var selectedCourt: Int?
    @State private var readyMatches: [Match] = []
    @State private var matchesLeft: [Match] = []
    @State private var teams: [TeamRegistration] = []
    
    enum DisplayMode: Int, Identifiable, CaseIterable {
        var id: Int { self.rawValue }
        case teams
        case restingTime
        
        func localizedSortingModeLabel() -> String {
            switch self {
            case .teams:
                return "Équipes"
            case .restingTime:
                return "Matchs"
            }
        }
    }

    func contentUnavailableDescriptionLabel() -> String {
        switch displayMode {
        case .restingTime:
            return "Ce tournoi n'a aucun match prêt à démarrer"
        case .teams:
            return "Ce tournoi n'a aucune équipe ayant déjà terminé un match."
        }
    }

    var sortedMatches: [Match] {
        return readyMatches.sorted(by: \.restingTimeForSorting)
    }

    var sortedTeams: [TeamRegistration] {
        return teams
    }

    var body: some View {
        List {
            Section {
                switch displayMode {
                case .teams:
                    if sortedTeams.isEmpty == false {
                        ForEach(sortedTeams) { team in
                            TeamRowView(team: team, displayRestingTime: true)
                        }
                    } else {
                        ContentUnavailableView("Aucune équipe en repos", systemImage: "xmark.circle", description: Text(contentUnavailableDescriptionLabel()))
                    }
                case .restingTime:
                    if sortedMatches.isEmpty == false {
                        ForEach(sortedMatches) { match in
                            MatchRowView(match: match, updatedField: selectedCourt)
                                .matchViewStyle(.followUpStyle)
                        }
                    } else {
                        ContentUnavailableView("Aucun match à venir", systemImage: "xmark.circle", description: Text(contentUnavailableDescriptionLabel()))
                    }
                }
            } header: {
                Picker(selection: $displayMode) {
                    ForEach(DisplayMode.allCases) { sortingMode in
                        Text(sortingMode.localizedSortingModeLabel()).tag(sortingMode)
                    }
                } label: {
                    Text("Affichage")
                }
                .labelsHidden()
                .pickerStyle(.segmented)
            }
            .textCase(nil)
        }
        .navigationTitle("Temps de repos")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .onAppear {
            let allMatches = tournament.allMatches()
            let matchesLeft = Tournament.matchesLeft(allMatches)
            let runningMatches = Tournament.runningMatches(allMatches)
            let readyMatches = Tournament.readyMatches(allMatches)
            self.readyMatches = Tournament.availableToStart(readyMatches, in: runningMatches, checkCanPlay: false)
            self.matchesLeft = matchesLeft
            self.teams = tournament.selectedSortedTeams().filter({ $0.restingTime() != nil }).sorted(by: \.restingTimeForSorting)
        }
    }
}
