import SwiftUI

// MARK: - 邮件输入配置
struct EmailInputConfig {
    let emailAddress: Binding<String>
    let emailSubject: Binding<String>
    let emailBody: Binding<String>
    let emailCc: Binding<String>
    let emailBcc: Binding<String>
}

// MARK: - WiFi输入配置
struct WiFiInputConfig {
    let ssid: Binding<String>
    let password: Binding<String>
    let encryptionType: Binding<WiFiInputView.WiFiEncryptionType>
}

// MARK: - 联系人输入配置
struct ContactInputConfig {
    let firstName: Binding<String>
    let lastName: Binding<String>
    let phone: Binding<String>
    let email: Binding<String>
    let company: Binding<String>
    let title: Binding<String>
    let address: Binding<String>
    let website: Binding<String>
    let nickname: Binding<String>
    let birthday: Binding<Date>
    let note: Binding<String>
}

// MARK: - 位置输入配置
struct LocationInputConfig {
    let latitude: Binding<String>
    let longitude: Binding<String>
    let locationName: Binding<String>
}

// MARK: - 日历输入配置
struct CalendarInputConfig {
    let eventTitle: Binding<String>
    let eventDescription: Binding<String>
    let startDate: Binding<Date>
    let endDate: Binding<Date>
    let location: Binding<String>
}

// MARK: - 社交输入配置
struct SocialInputConfig {
    let username: Binding<String>
    let message: Binding<String>
}

// MARK: - 电话输入配置
struct PhoneInputConfig {
    let phoneNumber: Binding<String>
    let phoneMessage: Binding<String>
}

// MARK: - URL输入配置
struct URLInputConfig {
    let url: Binding<String>
}

// MARK: - 文本输入配置
struct TextInputConfig {
    let content: Binding<String>
}

// MARK: - 输入组件工厂
struct InputComponentFactory {
    
    // 创建邮件输入组件
    static func createEmailInput(with config: EmailInputConfig) -> AnyView {
        return AnyView(
            EmailInputView(
                emailAddress: config.emailAddress,
                emailSubject: config.emailSubject,
                emailBody: config.emailBody,
                emailCc: config.emailCc,
                emailBcc: config.emailBcc
            )
        )
    }
    
    // 创建WiFi输入组件
    static func createWiFiInput(with config: WiFiInputConfig) -> AnyView {
        return AnyView(
            WiFiInputView(
                ssid: config.ssid,
                password: config.password,
                encryptionType: config.encryptionType
            )
        )
    }
    
    // 创建联系人输入组件
    static func createContactInput(with config: ContactInputConfig) -> AnyView {
        return AnyView(
            ContactInputView(
                firstName: config.firstName,
                lastName: config.lastName,
                phone: config.phone,
                email: config.email,
                company: config.company,
                title: config.title,
                address: config.address,
                website: config.website,
                nickname: config.nickname,
                birthday: config.birthday,
                note: config.note
            )
        )
    }
    
    // 创建位置输入组件
    static func createLocationInput(with config: LocationInputConfig) -> AnyView {
        return AnyView(
            LocationInputView(
                latitude: config.latitude,
                longitude: config.longitude,
                locationName: config.locationName
            )
        )
    }
    
    // 创建日历输入组件
    static func createCalendarInput(with config: CalendarInputConfig) -> AnyView {
        return AnyView(
            CalendarInputView(
                eventTitle: config.eventTitle,
                eventDescription: config.eventDescription,
                startDate: config.startDate,
                endDate: config.endDate,
                location: config.location
            )
        )
    }
    
    // 创建社交输入组件
    static func createSocialInput(with config: SocialInputConfig, platform: SocialInputView.SocialPlatform) -> AnyView {
        return AnyView(
            SocialInputView(
                username: config.username,
                message: config.message,
                platform: platform
            )
        )
    }
    
    // 创建电话输入组件
    static func createPhoneInput(with config: PhoneInputConfig, inputType: PhoneInputView.PhoneInputType) -> AnyView {
        return AnyView(
            PhoneInputView(
                phoneNumber: config.phoneNumber,
                message: config.phoneMessage,
                inputType: inputType
            )
        )
    }
    
    // 创建URL输入组件
    static func createURLInput(with config: URLInputConfig) -> AnyView {
        return AnyView(
            URLInputView(
                url: config.url
            )
        )
    }
    
    // 创建文本输入组件
    static func createTextInput(with config: TextInputConfig, placeholder: String, maxCharacters: Int) -> AnyView {
        return AnyView(
            TextInputView(
                content: config.content,
                placeholder: placeholder,
                maxCharacters: maxCharacters
            )
        )
    }
    
    // 根据QR码类型返回相应的输入组件 - 需要从CreateQRCodeView传入具体的绑定
    static func createInputComponent(
        for qrCodeType: QRCodeType,
        emailConfig: EmailInputConfig? = nil,
        wifiConfig: WiFiInputConfig? = nil,
        contactConfig: ContactInputConfig? = nil,
        locationConfig: LocationInputConfig? = nil,
        calendarConfig: CalendarInputConfig? = nil,
        socialConfig: SocialInputConfig? = nil,
        phoneConfig: PhoneInputConfig? = nil,
        urlConfig: URLInputConfig? = nil,
        textConfig: TextInputConfig? = nil
    ) -> AnyView {
        
        switch qrCodeType {
        case .mail:
            guard let config = emailConfig else {
                return AnyView(EmptyView())
            }
            return createEmailInput(with: config)
            
        case .wifi:
            guard let config = wifiConfig else {
                return AnyView(EmptyView())
            }
            return createWiFiInput(with: config)
            
        case .vcard, .mecard:
            guard let config = contactConfig else {
                return AnyView(EmptyView())
            }
            return createContactInput(with: config)
            
        case .location:
            guard let config = locationConfig else {
                return AnyView(EmptyView())
            }
            return createLocationInput(with: config)
            
        case .calendar:
            guard let config = calendarConfig else {
                return AnyView(EmptyView())
            }
            return createCalendarInput(with: config)
            
        case .instagram, .facebook, .twitter, .tiktok, .snapchat, .whatsapp, .viber, .spotify:
            guard let config = socialConfig else {
                return AnyView(EmptyView())
            }
            let platform = SocialInputView.SocialPlatform(rawValue: qrCodeType.rawValue.capitalized) ?? .instagram
            return createSocialInput(with: config, platform: platform)
            
        case .phone, .sms:
            guard let config = phoneConfig else {
                return AnyView(EmptyView())
            }
            let inputType: PhoneInputView.PhoneInputType = qrCodeType == .phone ? .phone : .sms
            return createPhoneInput(with: config, inputType: inputType)
            
        case .url:
            guard let config = urlConfig else {
                return AnyView(EmptyView())
            }
            return createURLInput(with: config)
            
        default:
            guard let config = textConfig else {
                return AnyView(EmptyView())
            }
            return createTextInput(
                with: config,
                placeholder: getPlaceholderText(for: qrCodeType),
                maxCharacters: getMaxCharacters(for: qrCodeType)
            )
        }
    }
    
    // 获取占位符文本
    static func getPlaceholderText(for qrCodeType: QRCodeType) -> String {
        switch qrCodeType {
        case .text:
            return "input_any_text_content".localized
        case .phone:
            return "input_phone_number".localized
        case .sms:
            return "input_sms_content".localized
        case .wifi:
            return "input_wifi_info".localized
        case .vcard:
            return "input_contact_info".localized
        case .mecard:
            return "input_contact_info".localized
        case .location:
            return "input_location_info".localized
        case .calendar:
            return "input_calendar_event_info".localized
        case .instagram:
            return "input_instagram_username".localized
        case .facebook:
            return "input_facebook_user_id_or_link".localized
        case .spotify:
            return "input_artist_and_song_info".localized
        case .twitter:
            return "input_x_info".localized
        case .whatsapp:
            return "input_whatsapp_phone_number".localized
        case .viber:
            return "input_viber_phone_number".localized
        case .snapchat:
            return "input_snapchat_info".localized
        case .tiktok:
            return "input_tiktok_info".localized
        case .mail:
            return "input_email_content".localized
        case .url:
            return "input_website_url".localized
        }
    }
    
    // 获取最大字符数
    private static func getMaxCharacters(for qrCodeType: QRCodeType) -> Int {
        switch qrCodeType {
        case .text:
            return 150
        case .phone, .sms:
            return 100
        case .wifi:
            return 200
        case .vcard, .mecard:
            return 500
        case .location:
            return 200
        case .calendar:
            return 300
        case .instagram, .facebook, .twitter, .tiktok, .snapchat, .whatsapp, .viber, .spotify:
            return 200
        case .mail:
            return 1200
        case .url:
            return 500
        }
    }
} 