import SwiftUI

// MARK: - 简化的选择器组件
struct SimplePickerView<T: Hashable>: View {
    let title: String
    let isRequired: Bool
    let selection: Binding<T>
    let options: [T]
    let optionTitle: (T) -> String
    let icon: String?
    
    init(
        title: String,
        isRequired: Bool = false,
        selection: Binding<T>,
        options: [T],
        optionTitle: @escaping (T) -> String,
        icon: String? = nil
    ) {
        self.title = title
        self.isRequired = isRequired
        self.selection = selection
        self.options = options
        self.optionTitle = optionTitle
        self.icon = icon
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            InputTitleView.required(title, icon: icon)
            
            Picker(title, selection: selection) {
                ForEach(options, id: \.self) { option in
                    Text(optionTitle(option)).tag(option)
                }
            }
            .pickerStyle(.menu)
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color(.systemGray6))
            )
        }
    }
}

// MARK: - 常用的选择器类型
extension SimplePickerView {
    static func wifiEncryption(
        selection: Binding<WiFiInputView.WiFiEncryptionType>,
        icon: String? = "lock"
    ) -> SimplePickerView<WiFiInputView.WiFiEncryptionType> {
        SimplePickerView<WiFiInputView.WiFiEncryptionType>(
            title: "encryption_type".localized,
            selection: selection,
            options: WiFiInputView.WiFiEncryptionType.allCases,
            optionTitle: { $0.displayName },
            icon: icon
        )
    }
    
    static func socialPlatform(
        selection: Binding<SocialInputView.SocialPlatform>,
        icon: String? = "globe"
    ) -> SimplePickerView<SocialInputView.SocialPlatform> {
        SimplePickerView<SocialInputView.SocialPlatform>(
            title: "social_platform".localized,
            selection: selection,
            options: SocialInputView.SocialPlatform.allCases,
            optionTitle: { $0.displayName },
            icon: icon
        )
    }
    
    static func phoneType(
        selection: Binding<PhoneInputView.PhoneInputType>,
        icon: String? = "phone"
    ) -> SimplePickerView<PhoneInputView.PhoneInputType> {
        SimplePickerView<PhoneInputView.PhoneInputType>(
            title: "phone_type".localized,
            selection: selection,
            options: PhoneInputView.PhoneInputType.allCases,
            optionTitle: { $0.displayName },
            icon: icon
        )
    }
}

#Preview {
    VStack(spacing: 16) {
        // WiFi加密类型选择器
        SimplePickerView<WiFiInputView.WiFiEncryptionType>.wifiEncryption(
            selection: .constant(.wpa2)
        )
        
        // 社交平台选择器
        SimplePickerView<SocialInputView.SocialPlatform>.socialPlatform(
            selection: .constant(.instagram)
        )
        
        // 电话类型选择器
        SimplePickerView<PhoneInputView.PhoneInputType>.phoneType(
            selection: .constant(.phone)
        )
    }
    .padding()
} 