//
//  TournamentFilterView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 29/04/2024.
//

import SwiftUI

struct TournamentFilterView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(\.dismiss) private var dismiss
    @State private var levels: Set<TournamentLevel>
    @State private var categories: Set<TournamentCategory>
    @State private var ageCategories: Set<FederalTournamentAge>
    @State private var selectedClubs: Set<String>
    var federalDataViewModel: FederalDataViewModel
    
    init(federalDataViewModel: FederalDataViewModel) {
        self.federalDataViewModel = federalDataViewModel
        _levels = State(wrappedValue: federalDataViewModel.levels)
        _categories = State(wrappedValue: federalDataViewModel.categories)
        _ageCategories = State(wrappedValue: federalDataViewModel.ageCategories)
        _selectedClubs = State(wrappedValue: federalDataViewModel.selectedClubs)
    }
    
    var body: some View {
        NavigationView {
            Form {
                let clubs : [Club] = dataStore.user.clubsObjects()
                if clubs.filter({ $0.code != nil }).isEmpty == false {
                    Section {
                        ForEach(clubs.filter({ $0.code != nil })) { club in
                            LabeledContent {
                                Button {
                                    if selectedClubs.contains(club.code!) {
                                        selectedClubs.remove(club.code!)
                                    } else {
                                        selectedClubs.insert(club.code!)
                                    }
                                } label: {
                                    if selectedClubs.contains(club.code!) {
                                        Image(systemName: "checkmark.circle.fill")
                                    }
                                }
                            } label: {
                                Text(club.clubTitle())
                            }
                        }
                    } header: {
                        Text("Clubs")
                    }
                }
                Section {
                    ForEach(TournamentLevel.allCases) { level in
                        LabeledContent {
                            Button {
                                if levels.contains(level) {
                                    levels.remove(level)
                                } else {
                                    levels.insert(level)
                                }
                            } label: {
                                if levels.contains(level) {
                                    Image(systemName: "checkmark.circle.fill")
                                }
                            }
                        } label: {
                            Text(level.localizedLabel(.title))
                        }
                    }
                } header: {
                    Text("Niveaux")
                }
                
                Section {
                    ForEach(TournamentCategory.allCases) { category in
                        LabeledContent {
                            Button {
                                if categories.contains(category) {
                                    categories.remove(category)
                                } else {
                                    categories.insert(category)
                                }
                            } label: {
                                if categories.contains(category) {
                                    Image(systemName: "checkmark.circle.fill")
                                }
                            }
                        } label: {
                            Text(category.localizedLabel(.title))
                        }
                    }
                } header: {
                    Text("Catégories")
                }

                Section {
                    ForEach(FederalTournamentAge.allCases) { category in
                        LabeledContent {
                            Button {
                                if ageCategories.contains(category) {
                                    ageCategories.remove(category)
                                } else {
                                    ageCategories.insert(category)
                                }
                            } label: {
                                if ageCategories.contains(category) {
                                    Image(systemName: "checkmark.circle.fill")
                                }
                            }
                        } label: {
                            Text(category.localizedLabel(.title))
                        }
                    }
                } header: {
                    Text("Catégories d'âge")
                }
            }
            .toolbar {
                ToolbarItem(placement: .topBarLeading) {
                    if federalDataViewModel.areFiltersEnabled() {
                        Button("Tout retirer", role: .cancel) {
                            federalDataViewModel.removeFilters()
                            dismiss()
                        }
                    } else {
                        Button("Fermer", role: .cancel) {
                            dismiss()
                        }
                    }
                }
                ToolbarItem(placement: .topBarTrailing) {
                    ButtonValidateView {
                        federalDataViewModel.levels = levels
                        federalDataViewModel.categories = categories
                        federalDataViewModel.ageCategories = ageCategories
                        federalDataViewModel.selectedClubs = selectedClubs
                        federalDataViewModel.id = UUID()
                        dismiss()
                    }
                }
            }
            .headerProminence(.increased)
            .navigationTitle("Filtres")
            .navigationBarTitleDisplayMode(.inline)
            .toolbarBackground(.visible, for: .navigationBar)
        }
    }
}

//#Preview {
//    TournamentFilterView(federalDataViewModel: FederalDataViewModel())
//}
