//
//  ActivityView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 29/02/2024.
//

import SwiftUI

struct ActivityView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(NavigationViewModel.self) private var navigation
    @State private var federalDataViewModel: FederalDataViewModel = FederalDataViewModel.shared
    @State private var searchText: String = ""
    
    @State private var presentFilterView: Bool = false
    @State private var presentToolbar: Bool = false
    @State private var newTournament: Tournament?
    @State private var viewStyle: AgendaDestination.ViewStyle = .list
    @State private var isGatheringFederalTournaments: Bool = false
    @State private var error: Error?
    @State private var uuid: UUID = UUID()
    @State private var presentClubSearchView: Bool = false
    @State private var quickAccessScreen: QuickAccessScreen? = nil
    
    enum QuickAccessScreen : Identifiable, Hashable {
        case inscription(pasteString: String)
        
        var id: String {
            switch self {
            case .inscription:
                return "inscription"
            }
        }
    }
    
    var runningTournaments: [FederalTournamentHolder] {
        return dataStore.tournaments.filter({ $0.endDate == nil })
            .filter({ federalDataViewModel.isTournamentValidForFilters($0) })
    }
    
    func getRunningTournaments() -> [Tournament] {
        return dataStore.tournaments.filter({ $0.endDate == nil })
            .filter({ federalDataViewModel.isTournamentValidForFilters($0) })
    }
    
    var endedTournaments: [Tournament] {
        return dataStore.tournaments.filter({ $0.endDate != nil })
            .filter({ federalDataViewModel.isTournamentValidForFilters($0) })
    }
//    
//    func _activityStatus() -> String? {
//        let tournaments = tournaments
//        if tournaments.isEmpty && federalDataViewModel.areFiltersEnabled() == false {
//            return nil
//        } else {
//            let count = tournaments.map { $0.tournaments.count }.reduce(0,+)
//            return "\(count) tournoi" + count.pluralSuffix
//        }
//    }
    
    var tournaments: [FederalTournamentHolder] {
        switch navigation.agendaDestination! {
        case .activity:
            return runningTournaments
        case .history:
            return endedTournaments
        case .tenup:
            return federalDataViewModel.filteredFederalTournaments
        }
    }
    
    @ViewBuilder
    private func _pasteView() -> some View {
        PasteButton(payloadType: String.self) { strings in
            guard let first = strings.first else { return }
            quickAccessScreen = .inscription(pasteString: first)
        }
        .foregroundStyle(.master)
        .labelStyle(.iconOnly)
        .buttonBorderShape(.capsule)
    }
    
    var body: some View {
        @Bindable var navigation = navigation
        NavigationStack(path: $navigation.path) {
            VStack(spacing: 0) {
                GenericDestinationPickerView(selectedDestination: $navigation.agendaDestination, destinations: AgendaDestination.allCases, nilDestinationIsValid: false)
                
                List {
                    switch navigation.agendaDestination! {
                    case .activity:
                        EventListView(tournaments: runningTournaments, viewStyle: viewStyle, sortAscending: true)
                    case .history:
                        EventListView(tournaments: endedTournaments, viewStyle: viewStyle, sortAscending: false)
                    case .tenup:
                        EventListView(tournaments: federalDataViewModel.federalTournaments, viewStyle: viewStyle, sortAscending: true)
                            .id(uuid)
                    }
                }
                .environment(federalDataViewModel)
                .overlay {
                    if let error, navigation.agendaDestination == .tenup {
                        ContentUnavailableView {
                            Label("Erreur", systemImage: "exclamationmark")
                        } description: {
                            Text(error.localizedDescription)
                        } actions: {
                            RowButtonView("D'accord.") {
                                self.error = nil
                            }
                        }
                    } else if isGatheringFederalTournaments {
                        ProgressView()
                    } else {
                        if tournaments.isEmpty && viewStyle == .list {
                            if searchText.isEmpty == false {
                                ContentUnavailableView.search(text: searchText)
                            } else if federalDataViewModel.areFiltersEnabled() {
                                ContentUnavailableView {
                                    Text("Aucun résultat")
                                } description: {
                                    Text(federalDataViewModel.filterStatus())
                                } actions: {
                                    RowButtonView("supprimer le filtre") {
                                        federalDataViewModel.removeFilters()
                                    }
                                }
                            } else {
                                _dataEmptyView()
                            }
                        }
                    }
                }
                //.searchable(text: $searchText)
                .onAppear { presentToolbar = true }
                .onDisappear { presentToolbar = false }
                .sheet(item: $newTournament) { tournament in
                    EventCreationView(tournaments: [tournament], selectedClub: federalDataViewModel.selectedClub())
                        .environment(navigation)
                        .tint(.master)
                }
                .refreshable {
                    if navigation.agendaDestination == .tenup {
                        federalDataViewModel.federalTournaments.removeAll()
                        NetworkFederalService.shared.formId = ""
                        _gatherFederalTournaments()
                    }
                }
                .task {
                    if navigation.agendaDestination == .tenup
                        && dataStore.user.hasTenupClubs() == true
                        && federalDataViewModel.federalTournaments.isEmpty {
                        _gatherFederalTournaments()
                    }
                }
                .onChange(of: navigation.agendaDestination) {
                    if navigation.agendaDestination == .tenup
                        && dataStore.user.hasTenupClubs() == true
                        && federalDataViewModel.federalTournaments.isEmpty {
                        _gatherFederalTournaments()
                    }
                }
                .toolbar {
                    if presentToolbar {
                        //let _activityStatus = _activityStatus()
                        if federalDataViewModel.areFiltersEnabled() {
                            ToolbarItem(placement: .status) {
                                Text(federalDataViewModel.filterStatus())
                            }
                        }
                        
                        ToolbarItemGroup(placement: .topBarLeading) {
                            Button {
                                switch viewStyle {
                                case .list:
                                    viewStyle = .calendar
                                case .calendar:
                                    viewStyle = .list
                                }
                            } label: {
                                Image(systemName: "calendar.circle")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(minHeight: 28)
                            }
                            .symbolVariant(viewStyle == .calendar ? .fill : .none)

                            Button {
                                presentFilterView.toggle()
                            } label: {
                                Image(systemName: "line.3.horizontal.decrease.circle")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(minHeight: 28)
                            }
                            .symbolVariant(federalDataViewModel.areFiltersEnabled() ? .fill : .none)
                            
                            _pasteView()
                        }
                        
                        ToolbarItem(placement: .topBarTrailing) {
                            Button {
                                newTournament = Tournament.newEmptyInstance()
                                
                            } label: {
                                Image(systemName: "plus.circle.fill")
                                    .resizable()
                                    .scaledToFit()
                                    .frame(minHeight: 28)
                            }
                        }
                    }
                }
                .navigationTitle(TabDestination.activity.title)
                .navigationDestination(for: Tournament.self) { tournament in
                    TournamentView(tournament: tournament)
                }
                .sheet(isPresented: $presentFilterView) {
                    TournamentFilterView(federalDataViewModel: federalDataViewModel)
                        .environment(navigation)
                        .tint(.master)
                }
                .sheet(isPresented: $presentClubSearchView, onDismiss: {
                    if dataStore.user.hasTenupClubs() == true {
                        federalDataViewModel.federalTournaments.removeAll()
                        navigation.agendaDestination = .tenup
                    }
                }) {
                    ClubImportView()
                        .tint(.master)
                }
            }
        }
        .sheet(item: $quickAccessScreen) { screen in
            switch screen {
            case .inscription(let pasteString):
                NavigationStack {
                    List {
                        Section {
                            Text(pasteString)
                        } header: {
                            Text("Contenu du presse-papier")
                        }
                         
                        Section {
                            ForEach(getRunningTournaments()) { tournament in
                                NavigationLink {
                                    InscriptionManagerView(tournament: tournament, pasteString: pasteString)
                                        .environment(tournament)
                                } label: {
                                    VStack(alignment: .leading) {
                                        Text(tournament.tournamentTitle())
                                        Text(tournament.formattedDate()).foregroundStyle(.secondary)
                                    }
                                }
                            }
                        } header: {
                            Text("À coller dans la liste d'inscription")
                        }
                    }
                    .toolbar {
                        ToolbarItem(placement: .topBarLeading) {
                            Button("Fermer") {
                                self.quickAccessScreen = nil
                            }
                        }
                    }
                    .navigationTitle("Choix du tournoi")
                    .navigationBarTitleDisplayMode(.inline)
                    .toolbarBackground(.visible, for: .navigationBar)
                }
            }
        }
    }
    
    private func _gatherFederalTournaments() {
        isGatheringFederalTournaments = true
        Task {
            do {
                let clubs : [Club] = dataStore.user.clubsObjects()
                try await federalDataViewModel.gatherTournaments(clubs: clubs.filter { $0.code != nil }, startDate: .now.startOfMonth)
            } catch {
                self.error = error
            }
            isGatheringFederalTournaments = false
            uuid = UUID()
        }
    }
    
    @ViewBuilder
    private func _dataEmptyView() -> some View {
        switch navigation.agendaDestination! {
        case .activity:
            _runningEmptyView()
        case .history:
            _endedEmptyView()
        case .tenup:
            _tenupEmptyView()
        }
    }
    
    private func _runningEmptyView() -> some View {
        ContentUnavailableView {
            Label {
                Text("Bienvenue sur Padel Club")
                    .font(.largeTitle)
            } icon: {
                Image(.padelClubLogoFondclairTransparent)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 100)
            }
        } description: {
            Text("Aucun événement en cours ou à venir dans votre agenda.")
        } actions: {
            RowButtonView("Créer un nouvel événement") {
                newTournament = Tournament.newEmptyInstance()
            }
            if dataStore.user.hasTenupClubs() == false {
                RowButtonView("Chercher l'un de vos clubs") {
                    presentClubSearchView = true
                }
            } else {
                RowButtonView("Importer via Tenup") {
                    navigation.agendaDestination = .tenup
                }
            }
            
            SupportButtonView(contentIsUnavailable: true)
        }
    }

    private func _endedEmptyView() -> some View {
        ContentUnavailableView {
            Label("Aucun tournoi terminé", systemImage: "shield.slash")
        } description: {
            Text("Vos tournois terminés seront listés ici.")
        }
    }

    private func _tenupEmptyView() -> some View {
        if dataStore.user.hasTenupClubs() == false {
            ContentUnavailableView {
                Label("Aucun tournoi", systemImage: "shield.slash")
            } description: {
                Text("Pour voir vos tournois tenup ici, choisissez vos clubs.")
            } actions: {
                RowButtonView("Choisir mes clubs") {
                    navigation.selectedTab = .umpire
                }
            }
        } else {
            ContentUnavailableView {
                Label("Aucun tournoi", systemImage: "shield.slash")
            } description: {
                Text("Aucun tournoi n'a pu être récupéré via tenup.")
            } actions: {
                RowButtonView("Rafraîchir") {
                    NetworkFederalService.shared.formId = ""
                    _gatherFederalTournaments()
                }
            }
        }
    }

}

//#Preview {
//    ActivityView()
//}
