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

import SwiftUI
import LeStorage

struct ToolboxView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(NavigationViewModel.self) private var navigation: NavigationViewModel

    var body: some View {
        @Bindable var navigation = navigation
        NavigationStack(path: $navigation.toolboxPath) {
            List {
                
                Section {
                    Text("Version de l'application").badge(PadelClubApp.appVersion)
                    SupportButtonView(contentIsUnavailable: false)
                }

                #if DEBUG
                
                Section {
                    NavigationLink("Settings") {
                        DebugSettingsView()
                    }
                    NavigationLink("API calls") {
                        APICallsListView()
                    }
                }
                
                Section {
                    RowButtonView("Reset ALL API Calls") {
                        Store.main.resetApiCalls()
                        Logger.log("Api calls reset")
                    }
                }
                Section {
                    RowButtonView("Fix Names") {
                        let playerRegistrations = dataStore.playerRegistrations
                        playerRegistrations.forEach { player in
                            player.firstName = player.firstName.trimmed.capitalized
                            player.lastName = player.lastName.trimmed.uppercased()
                        }
                        
                        do {
                            try dataStore.playerRegistrations.addOrUpdate(contentOfs: playerRegistrations)
                        } catch {
                            Logger.error(error)
                        }
                    }
                }
                
                Section {
                    RowButtonView("Delete teams") {
                        let teamRegistrations = dataStore.teamRegistrations.filter({ $0.tournamentObject() == nil })
                        do {
                            try dataStore.teamRegistrations.delete(contentOfs: teamRegistrations)
                        } catch {
                            Logger.error(error)
                        }
                    }
                }
                
                Section {
                    RowButtonView("Delete players") {
                        let playersRegistrations = dataStore.playerRegistrations.filter({ $0.team() == nil })
                        do {
                            try dataStore.playerRegistrations.delete(contentOfs: playersRegistrations)
                        } catch {
                            Logger.error(error)
                        }
                    }

                }
                #endif

                Section {
                    Link(destination: URLs.main.url) {
                        Text("Accéder au site Padel Club")
                    }
                }
                
                Section {
                    NavigationLink {
                        SelectablePlayerListView()
                    } label: {
                        Label("Rechercher un joueur", systemImage: "person.fill.viewfinder")
                    }
                }
                
                Section {
                    NavigationLink {
                        RankCalculatorView()
                    } label: {
                        Label("Calculateur de points", systemImage: "scalemass")
                    }
                }

                Section {
                    NavigationLink {
                        GlobalSettingsView()
                    } label: {
                        Label("Formats de jeu par défaut", systemImage: "megaphone")
                    }
                    NavigationLink {
                        DurationSettingsView()
                    } label: {
                        Label("Définir les durées moyennes", systemImage: "deskclock")
                    }
                } footer: {
                    Text("Vous pouvez définir vos propres estimations de durées de match en fonction du format de jeu.")
                }
                
                Section {
                    Link("Accéder au guide de la compétition", destination: URLs.padelRules.url)
                }
            }
            .navigationTitle(TabDestination.toolbox.title)
        }
    }
}

//#Preview {
//    ToolboxView()
//}
