package com.xplora.xpsettings.Utils;

import android.content.res.Configuration;
import android.os.LocaleList;
import android.util.Log;

import com.android.internal.app.LocaleHelper;
import com.android.internal.app.LocalePicker;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Locale;

public class LanguageUtil {
    public static String TAG = "losion / xponboarding :" + "LanguageUtil";

    public static String getLanguage() {
        LocaleList list = LocalePicker.getLocales();
        if (list.size() <= 0)
            return "";
        String s = list.get(0).toString();
        if (s.isEmpty())
            return "";
        //系统中有en,en-US 2个，单独处理，去除一个
        //国内项目，判断中文简体繁体会有问题，所以不需要判断-
        if (s.equals("en-US") || s.equals("en_US")) {
            if (s.contains("-")) {
                s = s.split("-")[0];
            }
            if (s.contains("_")) {
                s = s.split("_")[0];
            }
        }
        Log.d(TAG, "getLanguage: " + s);
        return s;
    }

    public static int getLanguageIndex() {
        String cur = getLanguage();
        ArrayList<String> list = getLanguageList();
        for (int i = 0; i < list.size(); i++) {
            String s = list.get(i);
            if (isLanguageEquel(s, cur)) {
                return i;
            }
        }
        return -1;
    }

    public static ArrayList<String> getLanguageList() {
        if (Macros.DEBUG)
            return new ArrayList<>();
        String[] languages = LocalePicker.getSystemAssetLocales();

        ArrayList<String> list = new ArrayList<>();
        for (String s:languages) {
            if (s == null || s.isEmpty())
                continue;
            //系统中有en,en-US 2个，单独处理，去除一个
            //国内项目，判断中文简体繁体会有问题，所以不需要判断-
            if (s.equals("en-US") || s.equals("en_US")) {
                if (s.contains("-")) {
                    s = s.split("-")[0];
                }
                if (s.contains("_")) {
                    s = s.split("_")[0];
                }
            }
            boolean isExist = list.contains(s);
            if (isExist)
                continue;
            list.add(s);
        }
        Log.d(TAG, "getLanguageList: " + list.toString());
        return list;
    }

    public static ArrayList<String> getLanguageNameList() {
        ArrayList<String> ret = new ArrayList<>();
        ArrayList<String> list = getLanguageList();
        for (String s: list) {
            Locale locale = LanguageUtil.getLocaleName(s);
            String name = LocaleHelper.getDisplayName(locale, locale, true);
            int endIndex = name.indexOf("(");
            if (endIndex >= 0) {
                name = name.substring(0, endIndex);
            }
            ret.add(name);
        }
        return ret;
    }

    public static boolean isLanguageEquel(String value1, String value2) {
        //-, _ 不同，影响判断
        String v1 = value1.replace("-", "").replace("_", "").trim();
        String v2 = value2.replace("-", "").replace("_", "").trim();
        return v1.equals(v2);
    }

    public static Locale getLocaleName(String language) {
        Locale locale;
        if (language.equals("sr")) {
            locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("latn").build();
        } else {
            int index = language.indexOf('-');
            if (index == -1) {
                locale = new Locale(language);
            } else {
                String lan = language.substring(0, index);
                String reg = language.substring(index+1, language.length());
                locale = new Locale.Builder().setLanguage(lan).setRegion(reg).build();
            }
        }
        return locale;
    }

    public static void updateLanguage(int index) {
        Log.d(TAG, "updateLanguage: " + index);
        ArrayList<String> list = getLanguageList();
        if (index >= list.size())
            return;
        String language = list.get(index);
        Locale locale = LanguageUtil.getLocaleName(language);
        updateLanguageWithLocale(locale);
    }

    public static void updateLanguageWithLocale(Locale locale) {
        Log.d(TAG, "updateLanguage: " + locale.getLanguage());
        try {
            Object objIActMag, objActMagNative;
            Class clzIActMag = Class.forName("android.app.IActivityManager");
            Class clzActMagNative = Class
                    .forName("android.app.ActivityManagerNative");
            Method mtdActMagNative$getDefault = clzActMagNative
                    .getDeclaredMethod("getDefault");
            objIActMag = mtdActMagNative$getDefault.invoke(clzActMagNative);
            Method mtdIActMag$getConfiguration = clzIActMag
                    .getDeclaredMethod("getConfiguration");
            Configuration config = (Configuration) mtdIActMag$getConfiguration
                    .invoke(objIActMag);
            config.locale = locale;
            Class clzConfig = Class
                    .forName("android.content.res.Configuration");
            java.lang.reflect.Field userSetLocale = clzConfig
                    .getField("userSetLocale");
            userSetLocale.set(config, true);
            Class[] clzParams = { Configuration.class };
            Method mtdIActMag$updateConfiguration = clzIActMag
                    .getDeclaredMethod("updateConfiguration", clzParams);
            mtdIActMag$updateConfiguration.invoke(objIActMag, config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
