//
//  CallSettingsView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 16/04/2024.
//

import SwiftUI
import LeStorage

struct CallSettingsView: View {
    @EnvironmentObject var dataStore: DataStore
    @Environment(Tournament.self) var tournament: Tournament
    @State private var showSendToAllView: Bool = false
    @State private var addLink: Bool = false
    
    var body: some View {
        List {
            
            Section {
                NavigationLink {
                    CallMessageCustomizationView(tournament: tournament)
                } label: {
                    Text("Personnaliser le message de convocation")
                }
            }

            Section {
                RowButtonView("Envoyer un message à tout le monde") {
                    showSendToAllView = true
                }
            }

            Section {
                RowButtonView("Envoyer le lien du tournoi") {
                    addLink = true
                }
                .disabled(tournament.isPrivate)
            } footer: {
                if tournament.isPrivate {
                    Button {
                        tournament.isPrivate = false
                        do {
                            try dataStore.tournaments.addOrUpdate(instance: tournament)
                        } catch {
                            Logger.error(error)
                        }
                    } label: {
                        Text(.init("Le tournoi n'est pas visible sur [Padel Club](\(URLs.main.rawValue)), ")).foregroundStyle(.logoRed) + Text("le rendre visible ?").underline().foregroundStyle(.master)
                    }
                }
            }

            // #if DEBUG
            Section {
                RowButtonView("Annuler toutes les convocations", role: .destructive) {
                    let teams = tournament.unsortedTeams()
                    teams.forEach { team in
                        team.callDate = nil
                    }
                    do {
                        try dataStore.teamRegistrations.addOrUpdate(contentOfs: teams)
                    } catch {
                        Logger.error(error)
                    }
                }
            }

            Section {
                RowButtonView("Tout le monde a été convoqué", role: .destructive) {
                    let teams = tournament.unsortedTeams()
                    teams.forEach { team in
                        team.callDate = team.expectedSummonDate()
                    }
                    do {
                        try dataStore.teamRegistrations.addOrUpdate(contentOfs: teams)
                    } catch {
                        Logger.error(error)
                    }
                }
            }
            //#endif
        }
        .sheet(isPresented: $showSendToAllView) {
            SendToAllView(addLink: false)
                .tint(.master)
        }
        .sheet(isPresented: $addLink) {
            SendToAllView(addLink: true)
                .tint(.master)
        }
    }
}

//#Preview {
//    CallSettingsView()
//}
