import SwiftUI

// MARK: - WiFi Input Component
struct WiFiInputView: View {
    @Binding var ssid: String
    @Binding var password: String
    @Binding var encryptionType: WiFiEncryptionType
    @FocusState var focusedField: WiFiField?
    
    // WiFi field enum
    enum WiFiField: Hashable {
        case ssid, password
    }
    
    // WiFi encryption type
    enum WiFiEncryptionType: String, CaseIterable, Identifiable {
        case none = "None"
        case wep = "WEP"
        case wpa = "WPA"
        case wpa2 = "WPA2"
        case wpa3 = "WPA3"
        
        var id: String { rawValue }
        
        var displayName: String {
            switch self {
            case .none: return "no_encryption".localized
            case .wep: return "WEP"
            case .wpa: return "WPA"
            case .wpa2: return "WPA2"
            case .wpa3: return "WPA3"
            }
        }
    }
    
    var body: some View {
        VStack(spacing: 16) {
            // SSID (required)
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("network_name".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Text("*")
                        .foregroundColor(.red)
                    Spacer()
                }
                
                TextField("MyWiFi", text: $ssid)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .focused($focusedField, equals: .ssid)
            }
            
            // Password
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("wifi_password".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Spacer()
                }
                
                SecureField("wifi_password_placeholder".localized, text: $password)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .focused($focusedField, equals: .password)
            }
            
            // Encryption type
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Text("encryption_type".localized)
                        .font(.subheadline)
                        .foregroundColor(.primary)
                    Spacer()
                }
                
                Picker("encryption_type".localized, selection: $encryptionType) {
                    ForEach(WiFiEncryptionType.allCases) { type in
                        Text(type.displayName).tag(type)
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }
            
            // Format instructions
            VStack(alignment: .leading, spacing: 8) {
                HStack {
                    Image(systemName: "info.circle")
                        .font(.caption)
                        .foregroundColor(.blue)
                    
                    Text("format_instructions".localized)
                        .font(.caption)
                        .foregroundColor(.primary)
                    
                    Spacer()
                }
                
                Text("wifi_format_details".localized)
                    .font(.caption)
                    .foregroundColor(.secondary)
                    .lineLimit(nil)
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
            .background(
                RoundedRectangle(cornerRadius: 8)
                    .fill(Color.blue.opacity(0.1))
            )
        }
        .toolbar {
            ToolbarItemGroup(placement: .keyboard) {
                Spacer()
                Button("done".localized) {
                    focusedField = nil
                }
                .foregroundColor(.blue)
                .font(.system(size: 16, weight: .medium))
            }
        }
    }
}

#Preview {
    WiFiInputView(
        ssid: .constant(""),
        password: .constant(""),
        encryptionType: .constant(.wpa2)
    )
} 