package com.bokecc.room.ui.view.base;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import com.bokecc.common.utils.Tools;
import com.bokecc.room.ui.R;
import com.bokecc.sskt.base.common.dialog.CustomProgressDialog;
import com.bokecc.sskt.base.common.util.SharedPreferencesUtils;

import java.util.Locale;

/**
 * Activity基类
 * Created by wy on 2019/1/4.
 */
public abstract class CCBaseActivity extends AppCompatActivity {

    protected CCBaseActivity mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        Tools.log(getLocalClassName(),"onCreate");
        beforeSetContentView();
        setContentView(getLayoutId());
        //沉浸式标题栏，android 5.0以后支持
        /*if (Build.VERSION.SDK_INT >= 21 && isHideStatusbar) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);

            //如果使用沉浸式标题栏，会使adjustResize模式失效，加入以下代码可以修复
            AndroidBug5497Workaround.assistActivity(this);
        }*/
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }

    /**设置view之前调用*/
    protected void beforeSetContentView() {}

    @Override
    protected void onResume() {
        super.onResume();
        Tools.log(getLocalClassName(),"onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Tools.log(getLocalClassName(),"onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Tools.log(getLocalClassName(),"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Tools.log(getLocalClassName(),"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Tools.log(getLocalClassName(),"onDestroy");
    }

    /**获取布局文件id*/
    protected abstract int getLayoutId();

    /**
     * 显示加载
     */
    protected void showLoading(){
        startProgress(Tools.getString(R.string.load_tip));
    }

    /**
     * 显示加载
     */
    protected void showLoading(int id){
        startProgress(Tools.getString(id));
    }

    private CustomProgressDialog pd = null;
    private void startProgress(String msg){
        if(pd == null){
            pd = new CustomProgressDialog(this, msg,true,null);
        }
    }

    /**
     * 隐藏加载
     */
    protected void dismissLoading(){
        if(pd != null && pd.isShowing()){
            pd.dismiss();
            pd = null;
        }
    }

    protected void go(Class clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        startActivityForResult(intent, requestCode);
    }
    protected void go(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }
    protected void go(Class clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode, bundle);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(getAttachContext(newBase));
    }

    public static final int LANGUAGE_CHINESE = 0;
    public static final int LANGUAGE_ENGLISH = 1;
    public static final int LANGUAGE_JAPANESE = 2;

    public Context getAttachContext(Context context) {
        int language = SharedPreferencesUtils.getInt(context,"language",0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //7.0及以后
            return updateResources(context,language);
        } else {
            //7.0之前
            Configuration configuration = context.getResources().getConfiguration();
            if (language == LANGUAGE_CHINESE) {
                configuration.locale = Locale.SIMPLIFIED_CHINESE;
            } else if(language == LANGUAGE_JAPANESE){
                configuration.locale = Locale.JAPANESE;
            } else {
                configuration.locale = Locale.ENGLISH;
            }

            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            context.getResources().updateConfiguration(configuration, displayMetrics);
            return context;
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private Context updateResources(Context context,int language) {
        Resources resources = context.getResources();
        Configuration configuration = resources.getConfiguration();
        if (language == LANGUAGE_CHINESE) {
            configuration.locale = Locale.SIMPLIFIED_CHINESE;
        } else if(language == LANGUAGE_JAPANESE){
            configuration.locale = Locale.JAPANESE;
        } else {
            configuration.locale = Locale.ENGLISH;
        }

        Tools.log("language", "language is " + (language == 0
                ? "  SIMPLIFIED_CHINESE" : "  ENGLISH"));
        return context.createConfigurationContext(configuration);
    }
}
