//
//  LoserGroupStageSettingsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 29/06/2024.
//

import SwiftUI

extension Round {
    var isGroupStageLoserBracket: Bool {
        return false
    }
}

extension Tournament {
    func groupStageLoserBrackets() -> [Round] {
        []
    }
    
    func removeGroupStageLoserBrackets() {
        
    }
}

struct LoserGroupStageSettingsView: View {
    var tournament: Tournament
    @State private var loserGroupStageBracketType: Int? = nil
    @State private var losers : Set<TeamRegistration> = Set()
    @Environment(\.editMode) private var editMode

    var body: some View {
        List(selection: $losers) {
            if tournament.groupStageLoserBrackets().isEmpty == false {
                //for each all rounds without parent and loserGroupStage, ability to delete them
                Section {
                    RowButtonView("Effacer", role: .destructive) {
                        tournament.removeGroupStageLoserBrackets()
                    }
                }
            }
            
            if self.editMode?.wrappedValue == .active {
                Section {
                    //rajouter + toolbar valider / cancel
                    ForEach(tournament.groupStageTeams().filter({ $0.qualified == false })) { team in
                        TeamRowView(team: team).tag(team)
                    }
                } header: {
                    Text("Sélection des perdants de poules")
                }
            } else {
                Section {
                    RowButtonView("Ajouter un match de perdant") {
                        self.editMode?.wrappedValue = .active
                    }
                } footer: {
                    Text("Permet d'ajouter un match de perdant de poules.")
                }
            }
        }
        .toolbar {
            if self.editMode?.wrappedValue == .active {
                ToolbarItem(placement: .topBarLeading) {
                    Button("Annuler") {
                        self.editMode?.wrappedValue = .inactive
                    }
                }
                
                ToolbarItem(placement: .topBarTrailing) {
                    Button("Valider") {
                        self.editMode?.wrappedValue = .inactive
                        //tournament.createGroupStageLoserBracket()
                    }
                }
            }
        }
        .navigationTitle("Match de perdant de poules")
        .navigationBarBackButtonHidden(self.editMode?.wrappedValue == .active)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar(.visible, for: .navigationBar)
        .headerProminence(.increased)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}
