//
//  TournamentBuildView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 19/05/2024.
//

import SwiftUI

struct TournamentBuildView: View {
    var tournament: Tournament
    @State private var bracketStatus: (status: String, cut: TeamRegistration.TeamRange?)?
    @State private var groupStageStatus: (String, TeamRegistration.TeamRange?)?
    @State private var callStatus: Tournament.TournamentStatus?
    @State private var scheduleStatus: Tournament.TournamentStatus?
    @State private var cashierStatus: Tournament.TournamentStatus?

    @ViewBuilder
    var body: some View {
        let state = tournament.state()
             
        Section {
            if tournament.groupStageCount > 0 {
                NavigationLink(value: Screen.groupStage) {
                    LabeledContent {
                        if let groupStageStatus {
                            Text(groupStageStatus.0).lineLimit(1)
                                .multilineTextAlignment(.trailing)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Poules")
                        if tournament.shouldVerifyGroupStage {
                            Text("Vérifier les poules").foregroundStyle(.logoRed)
                        } else if let range = groupStageStatus?.1 {
                            HStack {
                                if let left = range.left {
                                    Text(left.weight.formatted())
                                }
                                
                                if let right = range.right {
                                    Image(systemName: "arrowshape.forward.fill")
                                    Text(right.weight.formatted())
                                }
                            }
                        }
                    }
                }
                .task {
                    groupStageStatus = await tournament.groupStageStatus()
                }
            }
            
            if tournament.rounds().isEmpty == false {
                NavigationLink(value: Screen.round) {
                    LabeledContent {
                        if let bracketStatus {
                            Text(bracketStatus.0).lineLimit(1)
                                .multilineTextAlignment(.trailing)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Tableau")
                        if tournament.shouldVerifyBracket {
                            Text("Vérifier la tableau").foregroundStyle(.logoRed)
                        } else if let range = bracketStatus?.1 {
                            HStack {
                                if let left = range.left {
                                    Text(left.weight.formatted())
                                }
                                
                                if let right = range.right {
                                    Image(systemName: "arrowshape.forward.fill")
                                    Text(right.weight.formatted())
                                }
                            }
                        }
                    }
                }
                .task {
                    bracketStatus = await tournament.bracketStatus()
                }
            }
        }

        
        Section {
            if tournament.hasEnded() {
                NavigationLink(value: Screen.rankings) {
                    Text("Classement final des équipes")
                }
            }

            if state == .running || state == .finished {
                TournamentInscriptionView(tournament: tournament)
                TournamentBroadcastRowView(tournament: tournament)
            }

            if state != .finished {
                NavigationLink(value: Screen.schedule) {
                    let tournamentStatus = scheduleStatus
                    LabeledContent {
                        if let tournamentStatus {
                            Text(tournamentStatus.completion)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Horaires")
                        if let tournamentStatus {
                            Text(tournamentStatus.label).lineLimit(1)
                        } else {
                            Text(" ")
                        }
                    }
                }
                .task {
                    scheduleStatus = await tournament.scheduleStatus()
                }
                
                NavigationLink(value: Screen.call) {
                    let tournamentStatus = callStatus
                    LabeledContent {
                        if let tournamentStatus {
                            Text(tournamentStatus.completion)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Convocations")
                        if let tournamentStatus {
                            Text(tournamentStatus.label).lineLimit(1)
                        } else {
                            Text(" ")
                        }
                    }
                }
                .task {
                    callStatus = await tournament.callStatus()
                }
            }
            
            NavigationLink(value: Screen.cashier) {
                let tournamentStatus = cashierStatus
                LabeledContent {
                    if let tournamentStatus {
                        Text(tournamentStatus.completion)
                    } else {
                        ProgressView()
                    }
                } label: {
                    Text("Encaissement")
                    if let tournamentStatus {
                        Text(tournamentStatus.label).lineLimit(1)
                    } else {
                        Text(" ")
                    }
                }
            }
            .task {
                cashierStatus = await tournament.cashierStatus()
            }
        }
    }
}

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