package com.bokecc.cloudclass.demo.base;

import android.app.Dialog;
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.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;

import com.bokecc.cloudclass.demo.CCClassApplication;
import com.bokecc.cloudclass.demo.R;
import com.bokecc.common.application.ApplicationData;
import com.bokecc.common.utils.Tools;
import com.bokecc.sskt.base.common.util.SharedPreferencesUtils;

import java.util.Locale;

/**
 * 作者 ${CC视频}.<br/>
 */
public abstract class BaseActivity extends AppCompatActivity {
    public static final int LANGUAGE_CHINESE = 0;
    public static final int LANGUAGE_ENGLISH = 1;
    public static final int LANGUAGE_JAPANESE = 2;
    public static final String LANGUAGE_KEY = "language";

    protected View mRoot;

    private Dialog mProgressDialog;

    public boolean isGo = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        setContentView(getLayoutId());
        mRoot = getWindow().getDecorView().findViewById(android.R.id.content);

        initProgressDialog();
        onViewCreated();
    }

    @Override
    protected void onResume() {
        super.onResume();
        isGo = false;
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 在SetContentView之前进行操作，父类空实现，子类根据需要进行实现
     */
    protected void beforeSetContentView() {}

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

    /**界面创建完成*/
    protected abstract void onViewCreated();

    /**
     * 初始化加载框
     */
    private void initProgressDialog() {
        mProgressDialog = new Dialog(this, R.style.ProgressDialog);
        mProgressDialog.setContentView(R.layout.progress_layout);
        mProgressDialog.setCancelable(false);
        mProgressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    /**
     * 显示加载
     */
    protected void showProgress() {
        if (mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.show();
    }

    /**
     * 隐藏加载
     */
    protected void dismissProgress() {
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    protected void toastOnUiThread(final String msg) {
        if (!TextUtils.isEmpty(msg)){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Tools.showToast(msg);
                }
            });
        }
    }

    protected void go(Class clazz) {
        isGo = true;
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }


    public void changeAppLanguage(Class clazz) {
        int language = SharedPreferencesUtils.getInt(this,LANGUAGE_KEY,LANGUAGE_CHINESE);
        Resources resources = getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        Configuration configuration = resources.getConfiguration();
        Locale locale;
        if (language == LANGUAGE_CHINESE) {
            locale = Locale.SIMPLIFIED_CHINESE;
        } else if(language == LANGUAGE_JAPANESE){
            locale = Locale.JAPANESE;
        } else {
            locale = Locale.ENGLISH;
        }

        configuration.setLocale(locale);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            ApplicationData.globalContext = getApplicationContext().createConfigurationContext(configuration);
        } else {
            getApplicationContext().getResources().updateConfiguration(configuration, metrics);
            ApplicationData.globalContext = getApplicationContext();
        }

        Intent intent = new Intent(this,clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

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