//
// FaceTec Device SDK config file.
// Auto-generated via the FaceTec SDK Configuration Wizard
//
import UIKit
import Foundation
import FaceTecSDK

class Config {
    // -------------------------------------
    // REQUIRED
    // Available at https://dev.facetec.com/account
    // NOTE: This field is auto-populated by the FaceTec SDK Configuration Wizard.
    static var DeviceKeyIdentifier = ""

    // -------------------------------------
    // REQUIRED
    // The URL to call to process FaceTec SDK Sessions.
    // In Production, you likely will handle network requests elsewhere and without the use of this variable.
    // See https://dev.facetec.com/security-best-practices#server-rest-endpoint-security for more information.

    // Developer Note: In Your Production Application, networking requests from Your App will call Your Webservice.
    // Calling the FaceTec Server Webservice directly from Your App is not allowed (except for initial testing).
    // Please see the FaceTec Architecture Diagram here more information:  https://dev.facetec.com/configuration-options#zoom-architecture-and-data-flow
    //
    // This field is auto-populated by the FaceTec SDK Configuration Wizard.
    static var BaseURL = "https://api.facetec.com/api/v4/biometrics/process-request"

    // External database reference ID for enrollment/authentication
    static var externalDatabaseRefID = ""

    // -------------------------------------
    // This app can modify the customization to demonstrate different look/feel preferences
    // NOTE: This function is auto-populated by the FaceTec SDK Configuration Wizard based on your UI Customizations you picked in the Configuration Wizard GUI.
    public static func retrieveConfigurationWizardCustomization() -> FaceTecCustomization {
        // Set a Default Customization
        let defaultCustomization = FaceTecCustomization()
        return defaultCustomization
    }

    // -------------------------------------
    // NOTE: Changing text color for Low Light mode so it's readable
    public static func retrieveLowLightConfigurationWizardCustomization() -> FaceTecCustomization {
        // CUSTOMIZATION PROPERTIES START
        // For Color Customization
        let textColor = UIColor(hexString: "#000000")

        let lowLightCustomization = retrieveConfigurationWizardCustomization()

        // Set Guidance Customization
        lowLightCustomization.guidanceCustomization.foregroundColor = textColor

        // Set Result Screen Customization
        lowLightCustomization.resultScreenCustomization.foregroundColor = textColor

        // Set ID Scan Customization
        lowLightCustomization.idScanCustomization.selectionScreenForegroundColor = textColor

        // CUSTOMIZATION PROPERTIES ASSIGNMENT END
        return lowLightCustomization
    }

    // -------------------------------------
    // NOTE: Changing text color for Dynamic Dimming mode so it's readable
    public static func retrieveDynamicDimmingConfigurationWizardCustomization() -> FaceTecCustomization {
        // CUSTOMIZATION PROPERTIES START
        // For Color Customization
        let textColor = UIColor(hexString: "#ffffff")

        let dynamicDimmingCustomization = retrieveConfigurationWizardCustomization()

        // Set Guidance Customization
        dynamicDimmingCustomization.guidanceCustomization.foregroundColor = textColor

        // Set Result Screen Customization
        dynamicDimmingCustomization.resultScreenCustomization.foregroundColor = textColor

        // Set ID Scan Customization
        dynamicDimmingCustomization.idScanCustomization.selectionScreenForegroundColor = textColor

        // CUSTOMIZATION PROPERTIES ASSIGNMENT END
        return dynamicDimmingCustomization
    }

    static var currentCustomization: FaceTecCustomization = retrieveConfigurationWizardCustomization()
    static var currentLowLightCustomization: FaceTecCustomization = retrieveLowLightConfigurationWizardCustomization()
    static var currentDynamicDimmingCustomization: FaceTecCustomization = retrieveDynamicDimmingConfigurationWizardCustomization()

    // Set theme customization
    static func setCustomization(_ customization: FaceTecCustomization) {
        currentCustomization = customization
        FaceTec.sdk.setCustomization(customization)
    }

    // Create customization with colors from React Native (matching Android implementation)
    static func createCustomization(colors: [String: Any]?, showLogo: Bool) -> FaceTecCustomization {
        // Parse colors from React Native with default values (matching Android defaults)
        let outerBackgroundColor = UIColor(hexString: colors?["outerBackgroundColor"] as? String ?? "#ffffff")
        let frameColor = UIColor(hexString: colors?["frameColor"] as? String ?? "#ffffff")
        let borderColor = UIColor(hexString: colors?["borderColor"] as? String ?? "#417FB2")
        let ovalColor = UIColor(hexString: colors?["ovalColor"] as? String ?? "#417FB2")
        let dualSpinnerColor = UIColor(hexString: colors?["dualSpinnerColor"] as? String ?? "#417FB2")
        let textColor = UIColor(hexString: colors?["textColor"] as? String ?? "#417FB2")
        let buttonAndFeedbackBarColor = UIColor(hexString: colors?["buttonAndFeedbackBarColor"] as? String ?? "#417FB2")
        let buttonAndFeedbackBarTextColor = UIColor(hexString: colors?["buttonAndFeedbackBarTextColor"] as? String ?? "#ffffff")
        let buttonColorHighlight = UIColor(hexString: colors?["buttonColorHighlight"] as? String ?? "#396E99")
        let buttonColorDisabled = UIColor(hexString: colors?["buttonColorDisabled"] as? String ?? "#B9CCDE")

        // Create gradient layer for feedback background
        let feedbackBackgroundLayer = CAGradientLayer()
        feedbackBackgroundLayer.colors = [buttonAndFeedbackBarColor.cgColor, buttonAndFeedbackBarColor.cgColor]
        feedbackBackgroundLayer.locations = [0, 1]
        feedbackBackgroundLayer.startPoint = CGPoint(x: 0, y: 0)
        feedbackBackgroundLayer.endPoint = CGPoint(x: 1, y: 0)

        // Frame customization
        let frameCornerRadius: Int32 = 20

        // Load images from resource bundle
        let bundle = Bundle(for: Config.self)
        var cancelImage: UIImage? = nil
        var yourAppLogoImage: UIImage? = nil

        if let resourceBundleURL = bundle.url(forResource: "FaceTecAssets", withExtension: "bundle"),
           let resourceBundle = Bundle(url: resourceBundleURL) {
            cancelImage = UIImage(named: "FaceTec_cancel", in: resourceBundle, compatibleWith: nil)
            yourAppLogoImage = showLogo ? UIImage(named: "FaceTec_your_app_logo", in: resourceBundle, compatibleWith: nil) : nil
        }

        let cancelButtonLocation: FaceTecCancelButtonLocation = .topLeft

        // Logo and watermark
        let securityWatermarkImage: FaceTecSecurityWatermarkImage = .faceTec

        // Create customization
        let customization = FaceTecCustomization()

        // Set Frame Customization
        customization.frameCustomization.cornerRadius = frameCornerRadius
        customization.frameCustomization.backgroundColor = frameColor
        customization.frameCustomization.borderColor = borderColor

        // Set Overlay Customization
        customization.overlayCustomization.brandingImage = yourAppLogoImage
        customization.overlayCustomization.backgroundColor = outerBackgroundColor

        // Set Guidance Customization
        customization.guidanceCustomization.backgroundColors = [frameColor, frameColor]
        customization.guidanceCustomization.foregroundColor = textColor
        customization.guidanceCustomization.buttonBackgroundNormalColor = buttonAndFeedbackBarColor
        customization.guidanceCustomization.buttonBackgroundDisabledColor = buttonColorDisabled
        customization.guidanceCustomization.buttonBackgroundHighlightColor = buttonColorHighlight
        customization.guidanceCustomization.buttonTextNormalColor = buttonAndFeedbackBarTextColor
        customization.guidanceCustomization.buttonTextDisabledColor = buttonAndFeedbackBarTextColor
        customization.guidanceCustomization.buttonTextHighlightColor = buttonAndFeedbackBarTextColor
        customization.guidanceCustomization.retryScreenImageBorderColor = borderColor
        customization.guidanceCustomization.retryScreenOvalStrokeColor = borderColor

        // Set Oval Customization
        customization.ovalCustomization.strokeColor = ovalColor
        customization.ovalCustomization.progressColor1 = dualSpinnerColor
        customization.ovalCustomization.progressColor2 = dualSpinnerColor

        // Set Feedback Customization
        customization.feedbackCustomization.backgroundColor = feedbackBackgroundLayer
        customization.feedbackCustomization.textColor = buttonAndFeedbackBarTextColor

        // Set Cancel Customization
        customization.cancelButtonCustomization.customImage = cancelImage
        customization.cancelButtonCustomization.location = cancelButtonLocation

        // Set Result Screen Customization
        customization.resultScreenCustomization.backgroundColors = [frameColor, frameColor]
        customization.resultScreenCustomization.foregroundColor = textColor
        customization.resultScreenCustomization.activityIndicatorColor = buttonAndFeedbackBarColor
        customization.resultScreenCustomization.resultAnimationBackgroundColor = buttonAndFeedbackBarColor
        customization.resultScreenCustomization.resultAnimationForegroundColor = buttonAndFeedbackBarTextColor
        customization.resultScreenCustomization.uploadProgressFillColor = buttonAndFeedbackBarColor

        // Set Security Watermark Customization
        customization.securityWatermarkImage = securityWatermarkImage

        // Set ID Scan Customization
        customization.idScanCustomization.selectionScreenBackgroundColors = [frameColor, frameColor]
        customization.idScanCustomization.selectionScreenForegroundColor = textColor
        customization.idScanCustomization.reviewScreenBackgroundColors = [frameColor, frameColor]
        customization.idScanCustomization.reviewScreenForegroundColor = buttonAndFeedbackBarTextColor
        customization.idScanCustomization.reviewScreenTextBackgroundColor = buttonAndFeedbackBarColor
        customization.idScanCustomization.captureScreenForegroundColor = buttonAndFeedbackBarTextColor
        customization.idScanCustomization.captureScreenTextBackgroundColor = buttonAndFeedbackBarColor
        customization.idScanCustomization.buttonBackgroundNormalColor = buttonAndFeedbackBarColor
        customization.idScanCustomization.buttonBackgroundDisabledColor = buttonColorDisabled
        customization.idScanCustomization.buttonBackgroundHighlightColor = buttonColorHighlight
        customization.idScanCustomization.buttonTextNormalColor = buttonAndFeedbackBarTextColor
        customization.idScanCustomization.buttonTextDisabledColor = buttonAndFeedbackBarTextColor
        customization.idScanCustomization.buttonTextHighlightColor = buttonAndFeedbackBarTextColor
        customization.idScanCustomization.captureScreenBackgroundColor = frameColor
        customization.idScanCustomization.captureFrameStrokeColor = borderColor

        return customization
    }
}

// Extension to create UIColor from hex string
extension UIColor {
    convenience init(hexString: String) {
        let hex = hexString.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
        var int = UInt64()
        Scanner(string: hex).scanHexInt64(&int)
        let a, r, g, b: UInt64
        switch hex.count {
        case 3: // RGB (12-bit)
            (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
        case 6: // RGB (24-bit)
            (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
        case 8: // ARGB (32-bit)
            (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
        default:
            (a, r, g, b) = (255, 0, 0, 0)
        }
        self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255)
    }
}
