package com.quyunshuo.androidbaseframemvvm.base.utils

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import android.util.Log
import com.quyunshuo.androidbaseframemvvm.base.BaseApplication
import java.util.Locale


object LanguageUtil {
    // 中文 英文 德语 俄语 日语 西班牙语 法语

        val language: Array<String> = arrayOf(
            "zh_CN", "en", "de", "ru", "ja", "es",
            "fr", "bg", "da", "fi", "nl", "cs",
            "ro", "no", "pt", "sv", "sk", "hu",
            "it", "ab", "ko", "pl", "uk", "hy",
            "ar", "th", "sq", "ka", "aa", "ba",
            "tr", "lv", "ak", "et", "mn", "am",
            "sl", "hr", "tn"
        )
        val CHINESE: String = language[0] //中文
        val ENGLISH: String = language[1] //英文
        val GERMAN: String = language[2] //德语
        val RUSSIA: String = language[3] //俄语
        val JAPANESE: String = language[4] //日语
        val SPANISH: String = language[5] //西班牙语
        val FRENCH: String = language[6] //法语
        val BULGARIA: String = language[7] //保加利亚
        val DanMai: String = language[8] //丹麦
        val FenLan: String = language[9] //芬兰
        val HeLan: String = language[10] //荷兰
        val JieKe: String = language[11] //捷克
        val RouMaNiYa: String = language[12] //罗马尼亚
        val NuoWei: String = language[13] //挪威
        val PuTaoYa: String = language[14] //葡萄牙
        val RuiShi: String = language[15] //瑞士
        val SiFaLuoKe: String = language[16] //斯伐洛克
        val XiongYaLi: String = language[17] //匈牙利
        val YiDaLi: String = language[18] //意大利
        val TaiWang: String = language[19] //臺灣
        val hangguo: String = language[20] //韩语
        val bolan: String = language[21] //波兰
        val wukelan: String = language[22] //乌克兰
        val ymny: String = language[23] //亚美尼亚
        val alb: String = language[24] //阿拉伯语
        val tg: String = language[25] //泰國
        val ysl: String = language[26] //以色列
        val brazil: String = language[27] //巴西
        val gljy: String = language[28] //格鲁吉亚
        val brazi2: String = language[29] //巴西3
        val turkey: String = language[30] //土耳其
        val latvia: String = language[31] //拉脱维亚
        val ricardoAmaro: String = language[32] //葡萄牙  Ricardo Amaro
        val eesti: String = language[33] //爱沙尼亚

        val mgy: String = language[34] //蒙古

        val bsy: String = language[35]
        val slwny: String = language[36] //斯洛文尼亚语
        val kldy: String = language[37] //克罗地亚语
        val bsny: String = language[38] //波斯尼亚


    val LANGUAGE_TEXT: Array<String> = arrayOf(
        "中文",
        "English(英语)",
        "Deutsch(德语)",
        "русский(俄语)",
        "日本語(日语)",
        "Español(西班牙语)",
        "Français(法语)",
        "България(保加利亚语)",
        "Dansk(丹麦语)",
        "Suomalainen(芬兰语)",
        "Nederlands(荷兰语)",
        "Česky(捷克语)",
        "Românesc(罗马尼亚语)",
        "Norsk språk(挪威语)",
        "Português(葡萄牙语)",
        "Svenska(瑞典语)",
        "Slovenského(斯洛伐克语)",
        "Magyar(匈牙利语)",
        "Lingua italiana(意大利语)",
        "繁體中文",
        "한국어(韩语)",
        "Polska(波兰语)",
        "Ukrainian(乌克兰语)",
        "Հայաստան(亚美尼亚语)",
        "(阿拉伯语)عرب",
        "ไทย(泰语)",
        "(希伯来语)יִשׂרְאֵלִי",
        "Brazil-2DHR(巴西语)",
        "საქართველოს(格鲁吉亚语)",
        "Brazil-3(巴西语)",
        "turkey(土耳其语)",
        "Latvija(拉脱维亚语)",
        "Portugal-RA(葡萄牙语)",
        "Eesti(爱沙尼亚)",
        "Монгол(蒙古语)",
        "(波斯语)فارسی",
        "Slovenščina(斯洛文尼亚语)",
        "(Hrvatski)克罗地亚语",
        "(Bosanski)波斯尼亚语",
    )

    val LANGUAGE2: ArrayList<String> = ArrayList<String>(listOf(
        "请选择第二语言",
        "中文",
        "English(英语)",
        "Deutsch(德语)",
        "русский(俄语)",
        "日本語(日语)",
        "Español(西班牙语)",
        "Français(法语)",
        "България(保加利亚语)",
        "Dansk(丹麦语)",
        "Suomalainen(芬兰语)",
        "Nederlands(荷兰语)",
        "Česky(捷克语)",
        "Românesc(罗马尼亚语)",
        "Norsk språk(挪威语)",
        "Português(葡萄牙语)",
        "Svenska(瑞典语)",
        "Slovenského(斯洛伐克语)",
        "Magyar(匈牙利语)",
        "Lingua italiana(意大利语)",
        "繁體中文",
        "한국어(韩语)",
        "Polska(波兰语)",
        "Ukrainian(乌克兰语)",
        "Հայաստան(亚美尼亚语)",
        "(阿拉伯语)عرب",
        "ไทย(泰语)",
        "(希伯来语)יִשׂרְאֵלִי",
        "Brazil-2DHR(巴西语)",
        "საქართველოს(格鲁吉亚语)",
        "Brazil-3(巴西语)",
        "turkey(土耳其语)",
        "Latvija(拉脱维亚语)",
        "Portugal-RA(葡萄牙语)",
        "Eesti(爱沙尼亚)",
        "Монгол(蒙古语)",
        "(波斯语)فارسی",
        "Slovenščina(斯洛文尼亚语)",
        "(Hrvatski)克罗地亚语",
        "(Bosanski)波斯尼亚语",
    ))

    fun setLanguage():String {
        var languageType = SpUtils.getInt("LANGUAGE_TYPE", 0)
        Log.d("backinfo", "languageType:" + languageType)

        var languageStr = ""
        if (languageType == 0) {
            languageStr = CHINESE
        } else if (languageType == 1) {
            languageStr = ENGLISH
        } else if (languageType == 2) {
            languageStr = GERMAN
        } else if (languageType == 3) {
            languageStr = RUSSIA
        } else if (languageType == 4) {
            languageStr = JAPANESE
        } else if (languageType == 5) {
            languageStr = SPANISH
        } else if (languageType == 6) {
            languageStr = FRENCH
        } else if (languageType == 7) {
            languageStr = BULGARIA
        } else if (languageType == 8) {
            languageStr = DanMai
        } else if (languageType == 9) {
            languageStr = FenLan
        } else if (languageType == 10) {
            languageStr = HeLan
        } else if (languageType == 11) {
            languageStr = JieKe
        } else if (languageType == 12) {
            languageStr = RouMaNiYa
        } else if (languageType == 13) {
            languageStr = NuoWei
        } else if (languageType == 14) {
            languageStr = PuTaoYa
        } else if (languageType == 15) {
            languageStr = RuiShi
        } else if (languageType == 16) {
            languageStr = SiFaLuoKe
        } else if (languageType == 17) {
            languageStr = XiongYaLi
        } else if (languageType == 18) {
            languageStr = YiDaLi
        } else if (languageType == 19) {
            languageStr = TaiWang
        } else if (languageType == 20) {
            languageStr = hangguo
        } else if (languageType == 21) {
            languageStr = bolan
        } else if (languageType == 22) {
            languageStr = wukelan
        } else if (languageType == 23) {
            languageStr = ymny
        } else if (languageType == 24) {
            languageStr = alb
        } else if (languageType == 25) {
            languageStr = tg
        } else if (languageType == 26) {
            languageStr = ysl
        } else if (languageType == 27) {
            languageStr = brazil
        } else if (languageType == 28) {
            languageStr = gljy
        } else if (languageType == 29) {
            languageStr = brazi2
        } else if (languageType == 30) {
            languageStr = turkey
        } else if (languageType == 31) {
            languageStr = latvia
        } else if (languageType == 32) {
            languageStr = ricardoAmaro
        } else if (languageType == 33) {
            languageStr = eesti
        } else if (languageType == 34) {
            languageStr = mgy
        } else if (languageType == 35) {
            languageStr = bsy
        } else if (languageType == 36) {
            languageStr = slwny
        } else if (languageType == 37) {
            languageStr = kldy
        } else if (languageType == 38) {
            languageStr = bsny
        } else {
            languageStr = CHINESE
            SpUtils.putInt("LANGUAGE_TYPE", 0)
        }

        selectLanguage(languageStr)

        return languageStr
    }


    /**
     * 设置语言
     *
     * @param lauType
     */
//    private fun languageStr = lauType: String) {
//        // 本地语言设置
//        val myLocale = Locale(lauType)
//        val res: Resources = BaseApplication.context.resources
//        val dm = res.displayMetrics
//        val conf = res.configuration
//        conf.locale = myLocale
//        res.updateConfiguration(conf, dm)
//    }
    fun selectLanguage(language: String?) {
        //设置语言类型
        val resources: Resources = BaseApplication.context.resources
        val configuration: Configuration = resources.configuration
        val displayMetrics = resources.displayMetrics
        val myLocale = Locale(language)
        configuration.locale = myLocale
        resources.updateConfiguration(configuration, displayMetrics)


    }
    fun applyLanguage(context: Context, language: String?) {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val res = context.resources
        val config = Configuration(res.configuration)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale)
            context.createConfigurationContext(config)
        } else {
            config.locale = locale
            res.updateConfiguration(config, res.displayMetrics)
        }
    }

    fun wrapContext(context: Context, language: String?): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val config = context.resources.configuration
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            config.setLocale(locale)
            return context.createConfigurationContext(config)
        } else {
            config.locale = locale
            return object : ContextWrapper(context) {
                override fun getResources(): Resources {
                    val res = super.getResources()
                    res.updateConfiguration(config, res.displayMetrics)
                    return res
                }
            }
        }
    }
    fun setAppLanguage(context: Context, languageCode: String?): Context {
        var context = context
        val locale = Locale(languageCode)
        Locale.setDefault(locale)
        val res = context.resources
        val config = Configuration(res.configuration)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale)
            context = context.createConfigurationContext(config)
        } else {
            config.locale = locale
            res.updateConfiguration(config, res.displayMetrics)
        }
        return context
    }


}