//
//  StopwatchFormView.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 01/02/2023.
//

import SwiftUI

struct StopwatchFormView: View {
     
    enum StopwatchField: Int, Hashable {
        case name
    }
    
    @FocusState private var focusedField: StopwatchField?

    var nameBinding: Binding<String>
    var imageBinding: Binding<CoolPic>
    var playSoundBinding: Binding<Bool>

    @EnvironmentObject var model: TimerModel

//    var textFieldIsFocused: FocusState<Bool>.Binding

    var body: some View {
        
        Form {
            Section(header: Text("Name for tracking the activity")) {
                TextField("name", text: nameBinding)
                    .focused($focusedField, equals: .name)
                    .onSubmit {
                        self.focusedField = nil
                    }
            }

            SoundImageFormView(imageBinding: imageBinding,
                               optionalSound: playSoundBinding)
                .environmentObject(self.model)
                               
        }.toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Button {
                    self.focusedField = nil
                } label: {
                    Image(systemName: "keyboard.chevron.compact.down")
                }
                Spacer()
            }
        }
    }
}

struct StopwatchFormView_Previews: PreviewProvider {

    @FocusState static var textFieldIsFocused: Bool
    
    static var previews: some View {
        StopwatchFormView(
            nameBinding: .constant(""),
            imageBinding: .constant(.pic1),
            playSoundBinding: .constant(true))
        .environmentObject(TimerModel())
    }
}
