package com.wdkl.app.ncs.callingbed.helper;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

import com.wdkl.app.ncs.callingbed.R;
import com.wdkl.app.ncs.callingbed.settings.SettingConfig;
import com.wdkl.ncs.android.lib.base.BaseApplication;

import java.util.Locale;

public class LocaleMangerUtils {

    public static Locale getSystemLocale() {
        return Locale.getDefault();
    }

    //获取当前语言id: 0--auto, 1--English, 2--中文, 3--西班牙语, 4--俄语
    public static int getCurrentLocaleIndex() {
        int languageSize = BaseApplication.appContext.getResources().getStringArray(R.array.language_list).length;
        int index = SettingConfig.getLanguageId(BaseApplication.appContext);
        //Log.d("wzl", "current language index: " + index);
        if (index >= 0 && index <languageSize) {
            return index;
        } else {
            return 0;
        }
    }

    public static void setApplicationLanguageByIndex(Context context, int index) {
        Locale locale = Locale.getDefault();
        switch (index) {
            case 0:
                //
                break;
            case 1:
                locale = Locale.ENGLISH;
                break;
            case 2:
                locale = Locale.CHINESE;
                break;
            case 3:
                locale = new Locale("es");
                break;
            case 4:
                locale = new Locale("ru");
                break;
        }

        setApplicationLanguage(context, locale);
    }

    public static void setApplicationLanguage(Context context, Locale locale) {
        //Log.d("wzl", "set locale language: " + locale.getLanguage());
        Configuration configuration = context.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        // 更新context中的语言设置
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        resources.updateConfiguration(configuration, dm);
    }

    public static Locale getApplicationLocale() {
        Configuration config = BaseApplication.appContext.getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return config.locale;
        } else {
            return config.getLocales().get(0);
        }
    }

}
