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

import SwiftUI

struct TournamentFilterView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(NavigationViewModel.self) private var navigation
    @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>
    @State private 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 {
                
                Section {
                    Picker(selection: $federalDataViewModel.dayDuration) {
                        Text("aucune").tag(nil as Int?)
                        Text(1.formatted()).tag(1 as Int?)
                        Text(2.formatted()).tag(2 as Int?)
                        Text(3.formatted()).tag(3 as Int?)
                    } label: {
                        Text("Durée souhaitée (en jours)")
                    }
                    
                    Picker(selection: $federalDataViewModel.dayPeriod) {
                        ForEach(DayPeriod.allCases) {
                            Text($0.localizedDayPeriodLabel()).tag($0)
                        }
                    } label: {
                        Text("En semaine ou week-end")
                    }
                }
                
                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.localizedLevelLabel(.title))
                        }
                    }
                } header: {
                    Text("Niveaux")
                }
                
                Section {
                    ForEach([TournamentCategory.men, TournamentCategory.women, TournamentCategory.mix]) { 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(.wide))
                        }
                    }
                } 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.localizedFederalAgeLabel(.title))
                        }
                    }
                } header: {
                    Text("Catégories d'âge")
                }

                if navigation.agendaDestination == .around {
                    let clubs : [FederalClub] = federalDataViewModel.searchedClubs
                    if clubs.isEmpty == false {
                        Section {
                            ForEach(clubs) { club in
                                LabeledContent {
                                    Button {
                                        if selectedClubs.contains(club.federalClubCode) {
                                            selectedClubs.remove(club.federalClubCode)
                                        } else {
                                            selectedClubs.insert(club.federalClubCode)
                                        }
                                    } label: {
                                        if selectedClubs.contains(club.federalClubCode) {
                                            Image(systemName: "checkmark.circle.fill")
                                        }
                                    }
                                } label: {
                                    Text(club.federalClubName)
                                }
                            }
                        } header: {
                            Text("Clubs")
                        }
                    }
                } else {
                    let clubs : [Club] = dataStore.user.clubsObjects().filter({ $0.code != nil })
                    if clubs.isEmpty == false {
                        Section {
                            ForEach(clubs) { 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")
                        }
                    }
                }
            }
            .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())
//}
