//
//  KMAdvertisementModelTansition.swift
//  KMAdvertisement
//
//  Created by lizhe on 2022/12/2.
//
#if os(OSX)
    import AppKit
#elseif os(iOS)
    import UIKit
#endif

@objcMembers open class KMAdvertisementModelTransition: NSObject {
    //MARK: 模型转换
    //颜色模型转换
    public static func transitionColor(color: KMAdvertisementModelItem.Color?, highlight:Bool) -> UIColor {
        var result = UIColor.clear
        guard let tempColor = color else { return result }
        
        if highlight {
            let darkHex = tempColor.height?.dark?.hex
            let darkAlpha = tempColor.height?.dark?.alpha
            let universalHex = tempColor.height?.universal?.hex
            let universalAlpha = tempColor.height?.universal?.alpha
            let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
            let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
            
            result = KMAdvertisementConfig.adapterDark(light: light , dark: dark) as! NSColor
        } else {
            let darkHex = tempColor.normal?.dark?.hex
            let darkAlpha = tempColor.normal?.dark?.alpha
            let universalHex = tempColor.normal?.universal?.hex
            let universalAlpha = tempColor.normal?.universal?.alpha
            
            let light = NSColor.init(hex: universalHex ?? "").withAlphaComponent(CGFloat(universalAlpha ?? 1))
            let dark = NSColor.init(hex: darkHex ?? universalHex ?? "").withAlphaComponent(CGFloat(darkAlpha ?? universalAlpha ?? 1))
            result = KMAdvertisementConfig.adapterDark(light: light, dark: dark) as! NSColor
        }
        
        return result
    }
    
    //图片转换
    public static func transitionImagePath(image: KMAdvertisementModelItem.Image?, highlight:Bool) -> String {
        var result = ""
        guard let tempImage = image else { return result }
        
        if highlight {
            if tempImage.height != nil {
                result = transitionLanguage(langeuage: KMAdvertisementConfig.isDarkModel() ? tempImage.height!.dark : tempImage.height!.universal)
            }
        } else {
            if tempImage.normal != nil {
                result = transitionLanguage(langeuage: KMAdvertisementConfig.isDarkModel() ? tempImage.normal?.dark : tempImage.normal?.universal)
            }
        }
        
        return result
    }
    
    //多语转换
    @objc public static func transitionLanguage(langeuage: KMAdvertisementModelItem.Language?) -> String {
        var result = ""
        let languageConfig = KMAdvertisementManager.manager.configuration.language
        if languageConfig != .unknow {
            if languageConfig == .en {
                result = langeuage?.en ?? ""
            } else if languageConfig == .zhCN {
                result = langeuage?.zh_CN ?? ""
            } else if languageConfig == .zhTW {
                result = langeuage?.zh_TW ?? ""
            }
        } else {
            let string = KMAdvertisementModelTransition.getLanguageType()
            if string == "zh-Hans-CN" {
                result = langeuage?.zh_CN ?? ""
            } else if string == "zh-Hans-TW" {
                result = langeuage?.zh_TW ?? ""
            } else {
                result = langeuage?.en ?? ""
            }
        }
        return result
    }
    
    public static func getLanguageType() -> String {
#if os(iOS)
        let def = UserDefaults.standard
        let allLanguages: [String] = def.object(forKey: "AppleLanguages") as! [String]
        let chooseLanguage = allLanguages.first
        return chooseLanguage ?? "en"
#elseif os(OSX)
//        let locale = NSLocale.current.languageCode
        //        let chooseLanguage = Locale.preferredLanguages[0]
        let allLanguages: [String] = UserDefaults.standard.object(forKey: "AppleLanguages") as! [String]
        let chooseLanguage: String = allLanguages.first ?? "en"
        return chooseLanguage
#endif
    }
}
