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

import SwiftUI

struct TournamentBuildView: View {
    var tournament: Tournament
    @State private var bracketStatus: String?
    @State private var groupStageStatus: String?
    @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()
        
        if tournament.hasEnded() {
            Section {
                NavigationLink(value: Screen.rankings) {
                    Text("Classement final des équipes")
                }
            }
        }
        
        Section {
            if tournament.groupStageCount > 0 {
                NavigationLink(value: Screen.groupStage) {
                    LabeledContent {
                        if let groupStageStatus {
                            Text(groupStageStatus).lineLimit(1)
                                .multilineTextAlignment(.trailing)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Poules")
                        if tournament.shouldVerifyGroupStage {
                            Text("Vérifier les poules").foregroundStyle(.logoRed)
                        }
                    }
                }
                .task {
                    groupStageStatus = await tournament.groupStageStatus()
                }
            }
            
            if tournament.rounds().isEmpty == false {
                NavigationLink(value: Screen.round) {
                    LabeledContent {
                        if let bracketStatus {
                            Text(bracketStatus).lineLimit(1)
                                .multilineTextAlignment(.trailing)
                        } else {
                            ProgressView()
                        }
                    } label: {
                        Text("Tableau")
                        if tournament.shouldVerifyBracket {
                            Text("Vérifier la tableau").foregroundStyle(.logoRed)
                        }
                    }
                }
                .task {
                    bracketStatus = await tournament.bracketStatus()
                }
            }
        }
        
        Section {
            if state == .running || state == .finished {
                TournamentBroadcastRowView(tournament: tournament)
            }

            if state == .running || state == .finished {
                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()
                }
            }

            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()
                }
            }
            
            if state == .running || state == .finished {
                TournamentInscriptionView(tournament: tournament)
            }

        }
    }
}

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