import Foundation
import UIKit
//import Flutter
import ComplyCubeMobileSDK

/// iOS equivalent of Android LookAndFeelParser with identical fields/aliases. :contentReference[oaicite:3]{index=3} :contentReference[oaicite:4]{index=4}
public final class LookAndFeelParser: LookAndFeelParsing {
    private let colorConverter: ColorConverter

    public init(colorConverter: ColorConverter = ColorConverter()) {
        self.colorConverter = colorConverter
    }

    public func parseSDK(from data: [String: Any]) throws -> LookAndFeel {
        func color(_ key: String) -> UIColor? {
            guard let hex = data[key] as? String else { return nil }
            return colorConverter.hexToColor(hex)
        }

        var lf = LookAndFeel()

        // General
        if let isDark = data["isDarkMode"] as? Bool {
            lf.uiInterfaceStyle = isDark ? .dark : .light
        } else {
            lf.uiInterfaceStyle = .light
        }
//        lf.uiInterfaceStyle = parseUIInterfaceStyle(data["uiInterfaceStyle"] as? String)
//        if let n = data["borderRadius"] as? NSNumber { lf.borderRadius = CGFloat(truncating: n) }
//        lf.enableAnimations = data["enableAnimations"] as? Bool ?? false

        if let raw = data["borderRadius"] {
            switch raw {
            case let s as String where Double(s) != nil:
                lf.borderRadius = CGFloat(Double(s)!)
            case let n as NSNumber:
                lf.borderRadius = CGFloat(truncating: n)
            default: break
            }
        }
        
        // ─── Buttons – primary
          lf.primaryButtonBgColor        = color("primaryButtonBgColor")
          lf.primaryButtonPressedBgColor = color("primaryButtonPressedBgColor")
          lf.primaryButtonTextColor      = color("primaryButtonTextColor")
          lf.primaryButtonBorderColor    = color("primaryButtonBorderColor")

          // ─── Buttons – secondary
          lf.secondaryButtonBgColor        = color("secondaryButtonBgColor")
          lf.secondaryButtonPressedBgColor = color("secondaryButtonPressedBgColor")
          lf.secondaryButtonTextColor      = color("secondaryButtonTextColor")
          lf.secondaryButtonBorderColor    = color("secondaryButtonBorderColor")

          // ─── Document / card selector
          lf.documentTypeSelectorBgColor                = color("documentTypeSelectorBgColor")
          lf.documentTypeSelectorBorderColor            = color("documentTypeSelectorBorderColor")
          lf.documentTypeSelectorIconColor              = color("documentTypeSelectorIconColor")
          lf.documentTypeSelectorDescriptionTextColor   = color("documentSelectorDescriptionTextColor")

          // ─── Info / warning popup
          lf.popUpBgColor                    = color("infoPanelBgColor")
          lf.popUpTitleColor                 = color("infoPanelTitleColor")


          // ─── Text hierarchy & links
          lf.headingTextColor     = color("headingTextColor")
          lf.subheadingTextColor  = color("subheadingTextColor")
          lf.bodyTextColor        = color("bodyTextColor")
          lf.linkButtonTextColor  = color("linkButtonTextColor")
          lf.blueBigType          = color("blueBigType")
          lf.textItemType         = color("textItemType")

          // ─── Camera shutter / FAB
          lf.cameraButtonBgColor  = color("cameraButtonColor")

          // ─── Surfaces, dividers, fields (add properties to LookAndFeel first)
          // lf.backgroundColor               = settings.color("backgroundColor")
          // lf.backgroundContentColor        = settings.color("backgroundContentColor")
          // lf.backgroundContentContrastColor= settings.color("backgroundContentContrastColor")
          // lf.backgroundDividerColor        = settings.color("backgroundDividerColor")
          // lf.editTextColor                 = settings.color("editTextColor")

        
        //        lf.primaryButtonColor       = color("primaryButtonColor")

        // Document selector (modern)
//        lf.documentSelectorColor                = color("documentSelectorColor")
//        lf.documentSelectorBorderColor          = color("documentSelectorBorderColor")
//        lf.documentSelectorIconColor            = color("documentSelectorIconColor")
//        lf.documentSelectorTitleTextColor       = color("documentSelectorTitleTextColor")
//        lf.documentSelectorDescriptionTextColor = color("documentSelectorDescriptionTextColor")



        //        lf.documentTypeSelectorTitleTextColor         = color("documentTypeSelectorTitleTextColor")

        // Info popup (modern)
//        lf.infoPopupColor                 = color("infoPopupColor")
//        lf.infoPopupIconColor             = color("infoPopupIconColor")
//        lf.infoPopupTitleTextColor        = color("infoPopupTitleTextColor")
//        lf.infoPopupDescriptionTextColor  = color("infoPopupDescriptionTextColor")

        // Info panel (legacy aliases)
//        lf.infoPanelColor                 = color("infoPanelColor") ?? color("popUpBgColor")
//        lf.infoPanelBgColor               = color("infoPanelBgColor") ?? color("popUpBgColor")
//        lf.infoPanelIconColor             = color("infoPanelIconColor")
//        lf.infoPanelTitleTextColor        = color("infoPanelTitleTextColor") ?? color("popUpTitleColor")
//        lf.infoPanelDescriptionTextColor  = color("infoPanelDescriptionTextColor")
//
//        // Error popup (modern)
//        lf.errorPopupColor                = color("errorPopupColor")
//        lf.errorPopupIconColor            = color("errorPopupIconColor")
//        lf.errorPopupTitleTextColor       = color("errorPopupTitleTextColor")
//        lf.errorPopupDescriptionTextColor = color("errorPopupDescriptionTextColor")
//
//        // Error panel (legacy)
//        lf.errorPanelColor                = color("errorPanelColor")
//        lf.errorPanelBgColor              = color("errorPanelBgColor")
//        lf.errorPanelIconColor            = color("errorPanelIconColor")
//        lf.errorPanelTitleTextColor       = color("errorPanelTitleTextColor")
//        lf.errorPanelDescriptionTextColor = color("errorPanelDescriptionTextColor")
//
//        // Camera
//        lf.cameraButtonColor = color("cameraButtonColor")

        // Typography / backgrounds
        lf.bodyTextColor                   = color("bodyTextColor") ?? color("textSecondary")
        lf.headingTextColor                = color("headingTextColor") ?? color("headerTitle")
        lf.subheadingTextColor             = color("subheadingTextColor") ?? color("subheaderTitle")
//        lf.backgroundColor                 = color("backgroundColor")
//        lf.backgroundContentColor          = color("backgroundContentColor")
//        lf.backgroundContentContrastColor  = color("backgroundContentContrastColor")
//        lf.backgroundDividerColor          = color("backgroundDividerColor")
//        lf.editTextColor                   = color("editTextColor")

        // iOS-only legacy
        lf.linkButtonTextColor = color("linkButtonTextColor")
        lf.blueBigType         = color("blueBigType")
        lf.textItemType        = color("textItemType")

        return lf
    }

    private func parseUIInterfaceStyle(_ style: String?) -> UIUserInterfaceStyle {
        switch style?.lowercased() {
        case "dark": return .dark
        case "light": return .light
        default: return .unspecified
        }
    }
}
