//
//  DoushiQiPicker.swift
//  ArkitDoushiQi
//
//  Created by Johan LACHENAL on 21/05/2024.
//

import SwiftUI

struct DoushiQiPicker<EnumType: RawRepresentable & Identifiable & Hashable>: View where EnumType.RawValue == String {
    let title: String
    @Binding var selectedOption: EnumType
    let options: [EnumType]
    
    init(title: String, selectedOption: Binding<EnumType>, options: [EnumType]) {
        self.title = title
        self._selectedOption = selectedOption
        self.options = options
    }
    
    var body: some View {
        VStack(spacing: 0) {
            Divider().background(Color.gray)
            Menu {
                Picker(title, selection: $selectedOption) {
                    ForEach(options) { option in
                        Text(option.rawValue.description)
                        .padding()
                    }
                }
                .labelsHidden()
                .pickerStyle(InlinePickerStyle())
            } label: {
                HStack {
                    Text(title)
                        .foregroundColor(.black)
                        .padding()
                    Spacer() // .frame(width : (20 - CGFloat(title.count)))
                    Text(selectedOption.rawValue.description)
                        .foregroundColor(.black)
                        .padding()
                }
                .frame(maxWidth: .infinity)
                .padding(EdgeInsets(top: 0, leading: 32, bottom: 0, trailing: 32))
            }
            Divider().background(Color.gray)
        }
    }
}

struct DoushiQiPicker_Previews: PreviewProvider {
    enum AI: String, CaseIterable, Identifiable, Hashable {
        case RandomAction = "IA Random"
        case EasyTrainedAI = "IA Facile"
        case MediumTrainedAI = "IA Intermédiaire"
        
        var id: String { self.rawValue }
    }
    @State static var selectedItem = AI.RandomAction
    static var previews: some View {
        DoushiQiPicker(
            title: "Selectionne une IA :",
            selectedOption: $selectedItem,
            options: AI.allCases
        )
    }
}




