//
//  CourtView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 23/04/2024.
//

import SwiftUI
import LeStorage

struct CourtView: View {
    @EnvironmentObject var dataStore: DataStore
    @Bindable var court: Court
    @State private var name: String = ""
    @FocusState var focusedField: Court.CodingKeys?
    
    init(court: Court) {
        self.court = court
        _name = State(wrappedValue: court.name ?? "")
    }
    
    var body: some View {
        Form {
            Section {
                LabeledContent {
                    TextField("Nom", text: $name)
                        .focused($focusedField, equals: ._name)
                        .autocorrectionDisabled()
                        .keyboardType(.alphabet)
                        .multilineTextAlignment(.trailing)
                        .frame(maxWidth: .infinity)
                        .onSubmit {
                            let courtName = name.prefixTrimmed(50)
                            if courtName.isEmpty {
                                court.name = nil
                            } else {
                                court.name = courtName
                            }
                            do {
                                try dataStore.courts.addOrUpdate(instance: court)
                            } catch {
                                Logger.error(error)
                            }
                        }
                } label: {
                    Text("Nom du terrain")
                }
            } footer: {
                if court.name?.isEmpty == false {
                    FooterButtonView("nom par défaut") {
                        name = ""
                        court.name = nil
                        do {
                            try dataStore.courts.addOrUpdate(instance: court)
                        } catch {
                            Logger.error(error)
                        }
                    }
                }
            }
            
            Section {
                Toggle(isOn: $court.exitAllowed) {
                    Text("Sortie autorisée")
                }
                Toggle(isOn: $court.indoor) {
                    Text("Terrain intérieur")
                }
            }
        }
        .onChange(of: [court.indoor, court.exitAllowed]) {
            do {
                try dataStore.courts.addOrUpdate(instance: court)
            } catch {
                Logger.error(error)
            }
        }
        .navigationBarBackButtonHidden(focusedField != nil)
        .toolbar(content: {
            if focusedField != nil {
                ToolbarItem(placement: .topBarLeading) {
                    Button("Annuler", role: .cancel) {
                        focusedField = nil
                    }
                }
            }
        })
        .navigationTitle(court.courtTitle())
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
    }
}

//#Preview {
//    CourtView(court: Court.mock())
//}
