//
//  GroupStageTeamView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 03/04/2024.
//

import SwiftUI
import LeStorage

struct GroupStageTeamView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(\.dismiss) private var dismiss
    let groupStage: GroupStage
    var team: TeamRegistration
    
    var body: some View {
        List {
            Section {
                ForEach(team.players()) { player in
                    EditablePlayerView(player: player, editingOptions: [.licenceId, .name, .payment])
                }
            }

            if groupStage.tournamentObject()?.hasEnded() == false {
//                if team.qualified && team.bracketPosition == nil, let tournament = team.tournamentObject() {
//                    Section {
//                        NavigationLink {
//                            SpinDrawView(drawees: [team], segments: tournament.matchesWithSpace()) { results in
//                                
//                            }
//                        } label: {
//                            Text("Tirage au sort visuel")
//                        }
//                    }
//                    
//                    Section {
//                        RowButtonView("Tirage au sort automatique", role: .destructive) {
//                        }
//                    }
//                }
                
                if team.qualified == false {
                    Section {
                        NavigationLink {
                            GroupStageTeamReplacementView(team: team)
                        } label: {
                            Text("Chercher à remplacer")
                        }
                    }
                }
                
                Section {
                    if team.qualified == false {
                        RowButtonView("Qualifier l'équipe", role: .destructive) {
                            team.qualified = true
                            //team.bracketPosition = nil
                            _save()
                        }
                    } else {
                        RowButtonView("Annuler la qualification", role: .destructive) {
                            team.qualified = false
                            groupStage.tournamentObject()?.resetTeamScores(in: team.bracketPosition)
                            team.bracketPosition = nil
                            _save()
                        }
                    }
                }
                
                if team.qualified == false {
                    Section {
                        RowButtonView("Retirer de la poule", role: .destructive) {
                            team.groupStagePosition = nil
                            team.groupStage = nil
                            _save()
                            groupStage._matches().forEach({ $0.updateTeamScores() })
                        }
                    }
                }
            }
        }
        .toolbarBackground(.visible, for: .navigationBar)
        .navigationTitle("Détail de l'équipe")
    }
    
    private func _save() {
        do {
            try dataStore.teamRegistrations.addOrUpdate(instance: team)
        } catch {
            Logger.error(error)
        }
        dismiss()
    }
}
