import Foundation
import FaceTecSDK

class ThemeHelpers {

    // Apply theme configuration from React Native (matching Android implementation)
    static func setAppTheme(colors: [String: Any]?, showLogo: Bool) {
        // Create customizations with colors (same colors for all lighting conditions)
        let customization = Config.createCustomization(colors: colors, showLogo: showLogo)
        let lowLightCustomization = Config.createCustomization(colors: colors, showLogo: showLogo)
        let dynamicDimmingCustomization = Config.createCustomization(colors: colors, showLogo: showLogo)

        // Set all three customizations (like Android does)
        FaceTec.sdk.setCustomization(customization)
        FaceTec.sdk.setLowLightCustomization(lowLightCustomization)
        FaceTec.sdk.setDynamicDimmingCustomization(dynamicDimmingCustomization)

        // Update stored customization
        Config.currentCustomization = customization
        Config.currentLowLightCustomization = lowLightCustomization
        Config.currentDynamicDimmingCustomization = dynamicDimmingCustomization
    }

    // Apply dark theme variant
    static func setDarkTheme() {
        let darkCustomization = createDarkThemeCustomization()
        Config.setCustomization(darkCustomization)
    }

    // Apply light theme variant
    static func setLightTheme() {
        let lightCustomization = createLightThemeCustomization()
        Config.setCustomization(lightCustomization)
    }

    private static func createDarkThemeCustomization() -> FaceTecCustomization {
        let customization = FaceTecCustomization()

        // Dark theme colors
        customization.frameCustomization.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
        customization.frameCustomization.borderColor = UIColor(red: 0.3, green: 0.3, blue: 0.3, alpha: 1.0)

        customization.overlayCustomization.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.8)

        customization.guidanceCustomization.foregroundColor = UIColor.white
        let darkBgColor = UIColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 1.0)
        customization.guidanceCustomization.backgroundColors = [darkBgColor, darkBgColor]

        customization.ovalCustomization.strokeColor = UIColor.white
        customization.ovalCustomization.progressColor1 = UIColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 1.0)
        customization.ovalCustomization.progressColor2 = UIColor(red: 0.0, green: 0.7, blue: 1.0, alpha: 1.0)

        customization.feedbackCustomization.backgroundColor = createGradientLayer(from: darkBgColor)
        customization.feedbackCustomization.textColor = UIColor.white

        return customization
    }

    private static func createLightThemeCustomization() -> FaceTecCustomization {
        let customization = FaceTecCustomization()

        // Light theme colors
        customization.frameCustomization.backgroundColor = UIColor.white
        customization.frameCustomization.borderColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0)

        customization.overlayCustomization.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.8)

        customization.guidanceCustomization.foregroundColor = UIColor.black
        customization.guidanceCustomization.backgroundColors = [UIColor.white, UIColor.white]

        customization.ovalCustomization.strokeColor = UIColor.black
        customization.ovalCustomization.progressColor1 = UIColor(red: 0.0, green: 0.5, blue: 1.0, alpha: 1.0)
        customization.ovalCustomization.progressColor2 = UIColor(red: 0.0, green: 0.7, blue: 1.0, alpha: 1.0)

        customization.feedbackCustomization.backgroundColor = createGradientLayer(from: UIColor.white)
        customization.feedbackCustomization.textColor = UIColor.black

        return customization
    }

    // Helper to create CAGradientLayer from UIColor
    private static func createGradientLayer(from color: UIColor) -> CAGradientLayer {
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [color.cgColor, color.cgColor]
        gradientLayer.locations = [0, 1]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        return gradientLayer
    }

    // Get current theme colors for debugging/info purposes
    static func getCurrentThemeInfo() -> [String: Any] {
        let customization = Config.currentCustomization

        return [
            "frameBackgroundColor": customization.frameCustomization.backgroundColor.hexString,
            "frameBorderColor": customization.frameCustomization.borderColor.hexString,
            "overlayBackgroundColor": customization.overlayCustomization.backgroundColor.hexString,
            "guidanceForegroundColor": customization.guidanceCustomization.foregroundColor.hexString,
            "guidanceBackgroundColor": customization.guidanceCustomization.backgroundColors.first?.hexString ?? "#FFFFFF",
            "ovalStrokeColor": customization.ovalCustomization.strokeColor.hexString,
            "ovalProgressColor1": customization.ovalCustomization.progressColor1.hexString,
            "ovalProgressColor2": customization.ovalCustomization.progressColor2.hexString
        ]
    }
}

// Extension to convert UIColor to hex string
extension UIColor {
    var hexString: String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0

        getRed(&r, green: &g, blue: &b, alpha: &a)

        let rgb: Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0

        return String(format: "#%06x", rgb)
    }
}
