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

import SwiftUI

struct GroupStageCallingView: View {
    @Environment(Tournament.self) var tournament: Tournament
    @State private var displayByTeam: Bool = false
    
    var body: some View {
        let groupStages = tournament.groupStages()
        List {
            _sameTimeGroupStageView(groupStages: groupStages)

            ForEach(groupStages) { groupStage in

                let seeds = groupStage.teams()
                let callSeeds = seeds.filter({ tournament.isStartDateIsDifferentThanCallDate($0) == false })

                if seeds.isEmpty == false {
                    Section {
                        NavigationLink {
                            _groupStageView(groupStage: groupStage)
                                .environment(tournament)
                        } label: {
                            CallView.CallStatusView(count: callSeeds.count, total: seeds.count, startDate: groupStage.startDate)
                        }
                    } header: {
                        Text(groupStage.groupStageTitle())
                    } footer: {
                        if let startDate = groupStage.startDate {
                            CallView(teams: seeds, callDate: startDate, matchFormat: groupStage.matchFormat, roundLabel: "poule")
                        }
                    }
                }
            }
        }
        .headerProminence(.increased)
    }
    
    @ViewBuilder
    func _sameTimeGroupStageView(groupStages: [GroupStage]) -> some View {
        let times = Dictionary(grouping: groupStages) { groupStage in
            groupStage.startDate
        }
        let keys = times.keys.compactMap { $0 }.sorted()
        
        if keys.count != groupStages.count {
            ForEach(keys, id: \.self) { key in
                if let _groupStages = times[key] {
                    let teams = _groupStages.flatMap { $0.teams() }
                    let callSeeds = teams.filter({ tournament.isStartDateIsDifferentThanCallDate($0) == false })
                    Section {
                        CallView.CallStatusView(count: callSeeds.count, total: teams.count, startDate: key)
                    } header: {
                        Text(_groupStages.map { $0.groupStageTitle() }.joined(separator: ", "))
                    } footer: {
                        CallView(teams: teams, callDate: key, matchFormat: tournament.groupStageMatchFormat, roundLabel: "poule")
                    }
                }
            }
        }
    }
    
    @ViewBuilder
    private func _groupStageView(groupStage: GroupStage) -> some View {
        let teams = groupStage.teams()
        List {
            if let startDate = groupStage.startDate {
                ForEach(teams) { team in
                    Section {
                        CallView.TeamView(team: team)
                    } header: {
                        Text(startDate.localizedDate())
                    } footer: {
                        CallView(teams: [team], callDate: startDate, matchFormat: groupStage.matchFormat, roundLabel: "poule")
                    }
                }

            }
        }
        .overlay {
            if groupStage.startDate == nil {
                ContentUnavailableView {
                    Label("Aucun horaire défini", systemImage: "clock.badge.questionmark")
                } description: {
                    Text("Vous n'avez pas encore défini d'horaire pour les différentes phases du tournoi")
                } actions: {
//                    RowButtonView("Horaire intelligent") {
//                        selectedScheduleDestination = nil
//                    }
                }
            }
        }
        .headerProminence(.increased)
        .navigationTitle(groupStage.groupStageTitle())
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}

//#Preview {
//    GroupStageCallingView()
//}
