//
//  ClubCourtSetupView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 20/05/2024.
//

import SwiftUI
import LeStorage

struct ClubCourtSetupView: View {
    @EnvironmentObject var dataStore: DataStore
    @Bindable var club: Club
    let displayContext: DisplayContext
    @Binding var selectedCourt: Court?
    var hideLockForEditingMessage: Bool = false
    
    @ViewBuilder
    var body: some View {
        Section {
            TournamentFieldsManagerView(localizedStringKey: "Terrains du club", count: $club.courtCount)
                .disabled(displayContext == .lockedForEditing)
                .onChange(of: club.courtCount) {
                    if displayContext != .addition {
                        do {
                            try dataStore.clubs.addOrUpdate(instance: club)
                        } catch {
                            Logger.error(error)
                        }
                    }
                }
            
//            Toggle(isOn: $club.alphabeticalName) {
//                Text("Nommer les terrains alphabétiquement")
//            }
//            .disabled(displayContext == .lockedForEditing)
//            .onChange(of: club.alphabeticalName) {
//                do {
//                    try dataStore.clubs.addOrUpdate(instance: club)
//                } catch {
//                    Logger.error(error)
//                }
//            }
        } footer: {
            if displayContext == .lockedForEditing && hideLockForEditingMessage == false {
                Text("Édition impossible, vous n'êtes pas le créateur de ce club.").foregroundStyle(.logoRed)
            }
        }

        Section {
            ForEach((0..<club.courtCount), id: \.self) { courtIndex in
                _courtView(atIndex: courtIndex, tournamentClub: club)
            }
        }
    }
    
    @ViewBuilder
    private func _courtView(atIndex index: Int, tournamentClub: Club) -> some View {
        let court = tournamentClub.customizedCourts.first(where: { $0.index == index })
        LabeledContent {
            if displayContext == .edition {
                FooterButtonView("personnaliser") {
                    if let court {
                        selectedCourt = court
                    } else {
                        let newCourt = Court(index: index, club: tournamentClub.id)
                        do {
                            try dataStore.courts.addOrUpdate(instance: newCourt)
                        } catch {
                            Logger.error(error)
                        }
                        selectedCourt = newCourt
                    }
                }
            }
        } label: {
            if let court {
                Text(court.courtTitle())
                HStack {
                    if court.indoor {
                        Text("Couvert")
                    }
                    if court.exitAllowed {
                        Text("Sortie autorisée")
                    }
                }
            } else {
                Text(_courtName(atIndex: index))
            }
        }
    }
    
    private func _courtName(atIndex index: Int) -> String {
        Court.courtIndexedTitle(atIndex: index)
    }
}
