package com.bokecc.cloudclass.demo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.util.DisplayMetrics;

import com.bokecc.cloudclass.demo.base.BaseActivity;
import com.bokecc.room.drag.CCSaasRoomInteractSDK;
import com.bokecc.sskt.base.common.util.SharedPreferencesUtils;

import java.util.Locale;

/**
 * 全局入口类
 * 作者 ${CC视频}.<br/>
 * @Modify wy
 */
public class CCClassApplication {

    /**Application的context*/
    @SuppressLint("StaticFieldLeak")
    private static Context context = null;

    private CCClassApplication(){}

    public static CCClassApplication getInstance(){
        return CCClassApplication.SingletonHolder.instance;
    }

    private static class SingletonHolder{
        public static CCClassApplication instance = new CCClassApplication();
    }

    public static Context getContext() {
        return context;
    }

    /**
     * 初始化
     * 使用了context和初始化异常捕获等
     * @param globalContext
     */
    public void init(Context globalContext) {
        this.context = globalContext;
        //初始化CCSDK，不可多次调用
        CCSaasRoomInteractSDK.getInstance().init(context);
    }

    /**
     * 释放资源
     */
    public void onTerminate() {
        CCSaasRoomInteractSDK.getInstance().onTerminate();
    }

    /**
     * 切换语言
     * @param context
     * @return
     */
    public Context attachBaseContext(Context context) {
        int language = SharedPreferencesUtils.getInt(context,BaseActivity.LANGUAGE_KEY,BaseActivity.LANGUAGE_CHINESE);
        Configuration configuration = new Configuration();
        if (language == BaseActivity.LANGUAGE_CHINESE) {
            configuration.locale = Locale.SIMPLIFIED_CHINESE;
        }else if(language == BaseActivity.LANGUAGE_JAPANESE){
            configuration.locale = Locale.JAPANESE;
        } else {
            configuration.locale =  Locale.ENGLISH;
        }

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
            //7.0及以后
            return context.createConfigurationContext(configuration);
        } else {
            //7.0之前
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            context.getResources().updateConfiguration(configuration, displayMetrics);
            return context;
        }
    }
}
