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

open class KMAdvertisementModelTransition: NSObject {
    //MARK: 模型转换
    //颜色模型转换
    static func transitionColor(color: KMAdvertisementModel.Color, highlight:Bool) -> UIColor {
        var result = UIColor.clear
        
        if highlight {
            let darkHex = color.height?.dark?.hex
            let darkAlpha = color.height?.dark?.alpha
            let universalHex = color.height?.universal?.hex
            let universalAlpha = color.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)
        } else {
            let darkHex = color.normal?.dark?.hex
            let darkAlpha = color.normal?.dark?.alpha
            let universalHex = color.normal?.universal?.hex
            let universalAlpha = color.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)
        }
        
        return result
    }
    
    //图片转换
    static func transitionImagePath(image: KMAdvertisementModel.Image, highlight:Bool) -> String {
        var result = ""
        
        if highlight {
            if image.height != nil {
                result = transitionLanguage(langeuage: image.height!)
            }
        } else {
            if image.normal != nil {
                result = transitionLanguage(langeuage: image.normal!)
            }
        }
        
        return result
    }
    
    //多语转换
    static func transitionLanguage(langeuage: KMAdvertisementModel.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
    }
    
    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]
        return chooseLanguage
#endif
    }
}
