package com.base.pdfreaderallpdfreader.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,language:String) {
        val config = context.resources.configuration
        val locale = Locale(language)

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


}