package com.base.scanqr.utils

import android.content.Context
import android.os.LocaleList
import java.util.Locale

object LanguageUtils {

    fun getSystemLanguage(): String {
        return Locale.getDefault().language
    }

    val supportLanguage = listOf(
        Locale.ENGLISH.language,
        Locale.PRC.language,
        Locale("pt", "BR").language,
        Locale("es").language,
        Locale.FRENCH.language,
        Locale("de").language,
        Locale("th", "TH").language,
        Locale("in", "ID").language,
        Locale("ms", "MY").language,
        Locale("vi", "VN").language,
        Locale("ja", "JP").language,
        Locale("it", "IT").language,
        Locale("ko", "KR").language,
        Locale("nl", "NL").language,
        Locale("zh", "TW").language,
        Locale("tr", "TR").language
    )

    fun getSystemLanguageDefault(): String {
        var systemLanguage = getSystemLanguage()
        if (!supportLanguage.contains(systemLanguage)) {
            systemLanguage = Locale.ENGLISH.language
        }
        return systemLanguage
    }

    fun isSupportSystemLanguage(): Boolean {
        return supportLanguage.contains(getSystemLanguage())
    }

    fun changeAppLanguage(context: Context, languageCountry: String) {
        val config = context.resources.configuration
        val lc = languageCountry.split("_")
        val locale = if (lc.size == 2) {
            Locale(lc[0], lc[1])
        } else {
            Locale(lc[0])
        }

        //Android 7.0以上的方法
        config.setLocale(locale)
        config.setLocales(LocaleList(locale))
        context.createConfigurationContext(config)
        context.resources.updateConfiguration(config, context.resources.displayMetrics)
    }


}