//
//  SDKThemes.swift
//  Bill24OnlinePaymentSdk
//
//  Created by MacbookPro on 31/10/23.
//

import Foundation

public struct Themes{
    public struct Property {
        static var primaryTextColor: String = ""
        static var primaryBackgroundColor: String = ""
        static var indicatorColor: String = ""
        static var secondaryTextColor: String = ""
        static var secondaryBackgroundColor: String = ""
        static var labelTextColor: String = ""
        static var labelBackgroundColor: String = ""
        static var alertTextColor: String = ""
        static var alertBackgroundColor: String = ""
        static var favoriteButtonTextColor: String = ""
        static var favoriteButtonBackgroundColor: String = ""
        static var bankButtonBackgroundColor: String = ""
        static var bankButtonTextPrimaryColor: String = ""
        static var bankButtonTextSecondaryColor: String = ""
        static var actionButtonTextColor: String = ""
        static var actionButtonBackgroundColor: String = ""
        static var buttonTextColor: String = ""
        static var buttonBackgroundColor: String = ""
    }

    private struct Defaults {
        static let light = (
            primaryTextColor: "#212E3B",
            primaryBackgroundColor: "#FFFFFF",
            indicatorColor: "#D1D5DB",
            secondaryTextColor: "#214775",
            secondaryBackgroundColor: "#FFFFFF",
            labelTextColor: "#54A6DB",
            labelBackgroundColor: "#F2F0ED",
            alertTextColor: "#FFFFFF",
            alertBackgroundColor: "#FF5952",
            favoriteButtonTextColor: "#17A865",
            favoriteButtonBackgroundColor: "#FFFFFF",
            bankButtonBackgroundColor: "#FFFFFF",
            bankButtonTextPrimaryColor: "#212E3B",
            bankButtonTextSecondaryColor: "#214775",
            actionButtonTextColor: "#212E3B",
            actionButtonBackgroundColor: "#F0F0F0",
            buttonTextColor: "#FFFFFF",
            buttonBackgroundColor: "#54A6DB"
        )

        static let dark = (
            primaryTextColor: "#F2F0ED",
            primaryBackgroundColor: "#1A2535",
            indicatorColor: "#4B5563",
            secondaryTextColor: "#7DAFD4",
            secondaryBackgroundColor: "#1A2535",
            labelTextColor: "#67B5E0",
            labelBackgroundColor: "#0F1923",
            alertTextColor: "#FFFFFF",
            alertBackgroundColor: "#FF6B64",
            favoriteButtonTextColor: "#17A865",
            favoriteButtonBackgroundColor: "#1A2535",
            bankButtonBackgroundColor: "#1A2535",
            bankButtonTextPrimaryColor: "#F2F0ED",
            bankButtonTextSecondaryColor: "#7DAFD4",
            actionButtonTextColor: "#F2F0ED",
            actionButtonBackgroundColor: "#2A3545",
            buttonTextColor: "#FFFFFF",
            buttonBackgroundColor: "#67B5E0"
        )
    }

    private static func resolvedColor(_ apiValue: String, fallback: String) -> String {
        let trimmed = apiValue.trimmingCharacters(in: .whitespacesAndNewlines)
        return trimmed.isEmpty ? fallback : trimmed
    }
 
    public static func useDefaults(isDarkMode: Bool) {
        let d = isDarkMode ? Defaults.dark : Defaults.light
        Property.primaryTextColor = d.primaryTextColor
        Property.primaryBackgroundColor = d.primaryBackgroundColor
        Property.indicatorColor = d.indicatorColor
        Property.secondaryTextColor = d.secondaryTextColor
        Property.secondaryBackgroundColor = d.secondaryBackgroundColor
        Property.labelTextColor = d.labelTextColor
        Property.labelBackgroundColor = d.labelBackgroundColor
        Property.alertTextColor = d.alertTextColor
        Property.alertBackgroundColor = d.alertBackgroundColor
        Property.favoriteButtonTextColor = d.favoriteButtonTextColor
        Property.favoriteButtonBackgroundColor = d.favoriteButtonBackgroundColor
        Property.bankButtonTextPrimaryColor = d.bankButtonTextPrimaryColor
        Property.bankButtonTextSecondaryColor = d.bankButtonTextSecondaryColor
        Property.bankButtonBackgroundColor = d.bankButtonBackgroundColor
        Property.actionButtonTextColor = d.actionButtonTextColor
        Property.actionButtonBackgroundColor = d.actionButtonBackgroundColor
        Property.buttonTextColor = d.buttonTextColor
        Property.buttonBackgroundColor = d.buttonBackgroundColor
    }

    public static func setTheme(isDarkMode: Bool, themeData: Appearance) {
        if isDarkMode {
            Themes.Property.primaryTextColor = resolvedColor(themeData.darkMode.primaryColor.textColor, fallback: Defaults.dark.primaryTextColor)
            Themes.Property.primaryBackgroundColor = resolvedColor(themeData.darkMode.primaryColor.backgroundColor, fallback: Defaults.dark.primaryBackgroundColor)
            Themes.Property.indicatorColor = resolvedColor(themeData.darkMode.indicatorColor, fallback: Defaults.dark.indicatorColor)

            Themes.Property.secondaryTextColor = resolvedColor(themeData.darkMode.secondaryColor.textColor, fallback: Defaults.dark.secondaryTextColor)
            Themes.Property.secondaryBackgroundColor = resolvedColor(themeData.darkMode.secondaryColor.backgroundColor, fallback: Defaults.dark.secondaryBackgroundColor)

            Themes.Property.labelTextColor = resolvedColor(themeData.darkMode.label.textColor, fallback: Defaults.dark.labelTextColor)
            Themes.Property.labelBackgroundColor = resolvedColor(themeData.darkMode.label.backgroundColor, fallback: Defaults.dark.labelBackgroundColor)

            Themes.Property.alertTextColor = resolvedColor(themeData.darkMode.alert.textColor, fallback: Defaults.dark.alertTextColor)
            Themes.Property.alertBackgroundColor = resolvedColor(themeData.darkMode.alert.backgroundColor, fallback: Defaults.dark.alertBackgroundColor)

            Themes.Property.favoriteButtonTextColor = resolvedColor(themeData.darkMode.button.favoriteButton.textColor, fallback: Defaults.dark.favoriteButtonTextColor)
            Themes.Property.favoriteButtonBackgroundColor = resolvedColor(themeData.darkMode.button.favoriteButton.backgroundColor, fallback: Defaults.dark.favoriteButtonBackgroundColor)

            Themes.Property.bankButtonTextPrimaryColor = resolvedColor(themeData.darkMode.button.bankButton.textPrimary, fallback: Defaults.dark.bankButtonTextPrimaryColor)
            Themes.Property.bankButtonTextSecondaryColor = resolvedColor(themeData.darkMode.button.bankButton.textSecondary, fallback: Defaults.dark.bankButtonTextSecondaryColor)
            Themes.Property.bankButtonBackgroundColor = resolvedColor(themeData.darkMode.button.bankButton.backgroundColor, fallback: Defaults.dark.bankButtonBackgroundColor)

            Themes.Property.actionButtonTextColor = resolvedColor(themeData.darkMode.button.actionButton.textColor, fallback: Defaults.dark.actionButtonTextColor)
            Themes.Property.actionButtonBackgroundColor = resolvedColor(themeData.darkMode.button.actionButton.backgroundColor, fallback: Defaults.dark.actionButtonBackgroundColor)

            Themes.Property.buttonTextColor = resolvedColor(themeData.darkMode.button.textColor, fallback: Defaults.dark.buttonTextColor)
            Themes.Property.buttonBackgroundColor = resolvedColor(themeData.darkMode.button.backgroundColor, fallback: Defaults.dark.buttonBackgroundColor)
        } else {
            Themes.Property.primaryTextColor = resolvedColor(themeData.lightMode.primaryColor.textColor, fallback: Defaults.light.primaryTextColor)
            Themes.Property.primaryBackgroundColor = resolvedColor(themeData.lightMode.primaryColor.backgroundColor, fallback: Defaults.light.primaryBackgroundColor)
            Themes.Property.indicatorColor = resolvedColor(themeData.lightMode.indicatorColor, fallback: Defaults.light.indicatorColor)

            Themes.Property.secondaryTextColor = resolvedColor(themeData.lightMode.secondaryColor.textColor, fallback: Defaults.light.secondaryTextColor)
            Themes.Property.secondaryBackgroundColor = resolvedColor(themeData.lightMode.secondaryColor.backgroundColor, fallback: Defaults.light.secondaryBackgroundColor)

            Themes.Property.labelTextColor = resolvedColor(themeData.lightMode.label.textColor, fallback: Defaults.light.labelTextColor)
            Themes.Property.labelBackgroundColor = resolvedColor(themeData.lightMode.label.backgroundColor, fallback: Defaults.light.labelBackgroundColor)

            Themes.Property.alertTextColor = resolvedColor(themeData.lightMode.alert.textColor, fallback: Defaults.light.alertTextColor)
            Themes.Property.alertBackgroundColor = resolvedColor(themeData.lightMode.alert.backgroundColor, fallback: Defaults.light.alertBackgroundColor)

            Themes.Property.favoriteButtonTextColor = resolvedColor(themeData.lightMode.button.favoriteButton.textColor, fallback: Defaults.light.favoriteButtonTextColor)
            Themes.Property.favoriteButtonBackgroundColor = resolvedColor(themeData.lightMode.button.favoriteButton.backgroundColor, fallback: Defaults.light.favoriteButtonBackgroundColor)

            Themes.Property.bankButtonTextPrimaryColor = resolvedColor(themeData.lightMode.button.bankButton.textPrimary, fallback: Defaults.light.bankButtonTextPrimaryColor)
            Themes.Property.bankButtonTextSecondaryColor = resolvedColor(themeData.lightMode.button.bankButton.textSecondary, fallback: Defaults.light.bankButtonTextSecondaryColor)
            Themes.Property.bankButtonBackgroundColor = resolvedColor(themeData.lightMode.button.bankButton.backgroundColor, fallback: Defaults.light.bankButtonBackgroundColor)

            Themes.Property.actionButtonTextColor = resolvedColor(themeData.lightMode.button.actionButton.textColor, fallback: Defaults.light.actionButtonTextColor)
            Themes.Property.actionButtonBackgroundColor = resolvedColor(themeData.lightMode.button.actionButton.backgroundColor, fallback: Defaults.light.actionButtonBackgroundColor)

            Themes.Property.buttonTextColor = resolvedColor(themeData.lightMode.button.textColor, fallback: Defaults.light.buttonTextColor)
            Themes.Property.buttonBackgroundColor = resolvedColor(themeData.lightMode.button.backgroundColor, fallback: Defaults.light.buttonBackgroundColor)
        }
    }

}



