package com.develop.food.base.utils;

import android.app.backup.BackupManager;
import android.content.res.Configuration;
import android.os.LocaleList;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class SerialportLibUtils {


    //切换语言
    public static void updateLanguage(Locale locale) {
        try {
            Object objIActMag;
            Class clzIActMag = Class.forName("android.app.IActivityManager");
            Class clzActMagNative = Class
                    .forName("android.app.ActivityManagerNative");
            //amn = ActivityManagerNative.getDefault();
            Method mtdActMagNative$getDefault = clzActMagNative
                    .getDeclaredMethod("getDefault");
            objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);
            // objIActMag = amn.getConfiguration();
            Method mtdIActMag$getConfiguration = clzIActMag
                    .getDeclaredMethod("getConfiguration");
            Configuration config = (Configuration) mtdIActMag$getConfiguration
                    .invoke(objIActMag);
            // set the locale to the new value
            config.locale = locale;
            //持久化  config.userSetLocale = true;
            Class clzConfig = Class
                    .forName("android.content.res.Configuration");
            Field userSetLocale = clzConfig
                    .getField("userSetLocale");
            userSetLocale.set(config, true);
            // 此处需要声明权限:android.permission.CHANGE_CONFIGURATION
            // 会重新调用 onCreate();
            Class[] clzParams = {Configuration.class};
            // objIActMag.updateConfiguration(config);
            Method mtdIActMag$updateConfiguration = clzIActMag
                    .getDeclaredMethod("updateConfiguration", clzParams);
            mtdIActMag$updateConfiguration.invoke(objIActMag, config);
            BackupManager.dataChanged("com.android.providers.settings");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void getAddLocale(List<String> list) {
        List<Locale> listLocale = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Locale locale = getLocale(list.get(i));
            if (locale!=null){
                listLocale.add(locale);
            }
        }

        Locale[] localesList = listLocale.toArray(new Locale[listLocale.size() ]);
        LocaleList ll = new LocaleList(localesList);
        try {
            Class localPicker = Class.forName("com.android.internal.app.LocalePicker");
            Method updateLocale = localPicker.getDeclaredMethod("updateLocales",
                    LocaleList.class);
            updateLocale.invoke(null, ll);
        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException
                 | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void setLanguage(Locale setLocaleLauguage) {
        try {
            Class classActivityManagerNative = Class.forName("android.app.ActivityManagerNative");
            Method getDefault = classActivityManagerNative.getDeclaredMethod("getDefault");
            Object objIActivityManager = getDefault.invoke(classActivityManagerNative);
            Class classIActivityManager = Class.forName("android.app.IActivityManager");
            Method getConfiguration = classIActivityManager.getDeclaredMethod("getConfiguration");
            Configuration config = (Configuration) getConfiguration.invoke(objIActivityManager);
            config.setLocale(setLocaleLauguage);

            Class clzConfig = Class.forName("android.content.res.Configuration");
            Field userSetLocale = clzConfig.getField("userSetLocale");
            userSetLocale.set(config, true);
            Class[] clzParams = new Class[]{Configuration.class};
            Method updateConfiguration = classIActivityManager.getDeclaredMethod("updateConfiguration", clzParams);
            updateConfiguration.invoke(objIActivityManager, config);
            BackupManager.dataChanged("com.android.providers.settings");
        } catch (Exception var12) {

        }
    }

    public static Locale getLocale(String lankey) {
        if (LangKey.ZH_CN.equals(lankey)) {
            return Locale.CHINA;
        } else if (LangKey.EN_US.equals(lankey)) {
            return Locale.ENGLISH;
        } else if (LangKey.JA_JP.equals(lankey)) {
            return Locale.JAPAN;
        } else if (LangKey.KO_KR.equals(lankey)) {
            return Locale.KOREA;
        }else if (LangKey.DE_DE.equals(lankey)){
            return  Locale.GERMAN;
        } else if (LangKey.LO_LA.equals(lankey)) {
            return new Locale("lo", "la");
        } else if (LangKey.KM_KH.equals(lankey)) {
            return new Locale("km", "kh");
        } else if (LangKey.TL_PH.equals(lankey)) {
            return new Locale("tl", "ph");
        } else if (LangKey.MY_MM.equals(lankey)) {
            return new Locale("my", "mm");
        } else if (LangKey.RU_RU.equals(lankey)) {
            return new Locale("ru", "ru");
        } else if (LangKey.CS_CZ.equals(lankey)) {
            return new Locale("cs", "cz");
        } else if (LangKey.PL_PL.equals(lankey)) {
            return new Locale("pl", "pl");
        }else if (LangKey.FR_FR.equals(lankey)){
            return new Locale("fr", "fr");
        }else if (LangKey.NL_NL.equals(lankey)){
            return new Locale("nl", "nl");
        }else if (LangKey.PT_PT.equals(lankey)){
            return new Locale("pt", "pt");
        }else if (LangKey.ES_ES.equals(lankey)){
            return new Locale("es", "es");
        }else if (LangKey.IT_IT.equals(lankey)){
            return new Locale("it", "it");
        }else if (LangKey.GR_GR.equals(lankey)){
            //特殊处理由于语言配置是配置gr，但系统前缀是el
            return new Locale("el", "gr");
        }else if (LangKey.RO_RO.equals(lankey)){
            return new Locale("ro", "ro");
        }else if (LangKey.FI_FI.equals(lankey)){
            return new Locale("fi", "fi");
        }else if (LangKey.NO_NO.equals(lankey)){
            return new Locale("no", "no");
        }else if (LangKey.SE_SE.equals(lankey)){
            //特殊处理由于语言配置是配置se，但系统前缀是sv
            return new Locale("sv", "se");
        }else if (LangKey.DK_DK.equals(lankey)){
            //特殊处理由于语言配置是配置dk，但系统前缀是da
            return new Locale("da", "dk");
        }else if (LangKey.CZ_CZ.equals(lankey)){
            //特殊处理由于语言配置是配置cz，但系统前缀是cs
            return new Locale("cs", "cz");
        }else if (LangKey.EE_EE.equals(lankey)){
            //特殊处理由于语言配置是配置ee，但系统前缀是et
            return new Locale("et", "ee");
        }else if (LangKey.SK_SK.equals(lankey)){
            return new Locale("sk", "sk");
        }else if (LangKey.UK_UK.equals(lankey)){
            return new Locale("uk", "ua");
        }else if (LangKey.AR_AR.equals(lankey)){
            return new Locale("ar", "ae");
        }else if (LangKey.LT_LT.equals(lankey)){
            return new Locale("lt", "lt");
        }else if (LangKey.IR_IR.equals(lankey)){
            return new Locale("ir", "ir");
        }else if (LangKey.KZ_KZ.equals(lankey)){
            return new Locale("kz", "kz");
        }else if (LangKey.BG_BG.equals(lankey)){
            return new Locale("bg", "bg");
        }else if (LangKey.HU_HU.equals(lankey)){
            return new Locale("hu", "hu");
        }else {
            return null;
        }
    }

    public static class LangKey {
        //---------------语言名字key---------------
        //中文简体
        public static final String ZH_CN = "zh";
        //英语
        public static final String EN_US = "en";
        //日语
        public static final String JA_JP = "ja";
        //韩语
        public static final String KO_KR = "ko";
        //柬埔寨语
        public static final String KM_KH = "km";
        //泰国
        public static final String TH_TH = "th";
        //法语
        public static final String FR_FR = "fr";
        //越南语
        public static final String VI_VN = "vi";
        //菲律宾语
        public static final String TL_PH = "tl";
        //马来西亚语
        public static final String MS_MY = "ms";
        //缅甸语
        public static final String MY_MM = "my";
        //捷克语
        public static final String CS_CZ = "cs";
        //印度尼西亚语
        public static final String IN_ID = "in";
        //老挝语
        public static final String LO_LA = "lo";
        //俄语
        public static final String RU_RU = "ru";
        //印度
        public static final String HI_IN = "hi";
        //波兰语
        public static final String PL_PL = "pl";
        //德语
        public static final String DE_DE = "de";
        //荷兰语
        public static final String NL_NL = "nl";
        //葡萄牙
        public static final String PT_PT = "pt";
        //葡萄牙
        public static final String ES_ES = "es";
        //意大利
        public static final String IT_IT = "it";
        //希腊
        public static final String GR_GR = "gr";
        //罗马尼亚语
        public static final String RO_RO = "ro";
        //芬兰语
        public static final String FI_FI = "fi";
        //挪威语
        public static final String NO_NO = "no";
        //瑞典语
        public static final String SE_SE = "se";
        //丹麦语
        public static final String DK_DK = "dk";
        //捷克语
        public static final String CZ_CZ = "cz";
        //爱沙尼亚语
        public static final String EE_EE = "ee";
        //斯洛伐克语
        public static final String SK_SK = "sk";
        //乌克兰语
        public static final String UK_UK = "uk";
        //阿拉伯语
        public static final String AR_AR = "ar";
        //立陶宛语
        public static final String LT_LT = "lt";
        //波斯语
        public static final String IR_IR = "ir";
        //哈萨克语
        public static final String KZ_KZ = "kz";

        //保加利亚
        public static final String BG_BG = "bg";

        //匈牙利文
        public static final String HU_HU = "hu";
    }

}
