package com.app.baselibrary.base.common;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.KeyEvent;

import com.app.baselibrary.BuildConfig;
import com.app.baselibrary.R;
import com.app.baselibrary.base.app.AppManager;
import com.app.baselibrary.commonUtil.LogUtil;
import com.app.baselibrary.commonUtil.StatusBarCompat;
import com.app.baselibrary.commonUtil.ToastUtil;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Desc: base activity;
 * Author: pl
 * Date: 2017-03-15 16:44
 */

public abstract class BaseActivity extends RxAppCompatActivity {

    public RxPermissions mRxPermissions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseInit();
        setContentView(getLayoutId());//设置布局文件；
        ButterKnife.bind(this);
        init();
    }

    //设置布局；
    protected abstract int getLayoutId();

    protected abstract void init();

    private void baseInit() {

        mRxPermissions = new RxPermissions(this);
        // 把actvity放到application栈中管理
        AppManager.getInstance().addActivity(this);
        // 无标题
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置状态栏的主颜色：
//        SetTranslanteBar();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!BuildConfig.LOG_DEBUG)
            MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!BuildConfig.LOG_DEBUG)
            MobclickAgent.onPause(this);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {

        if (newConfig.equals(Configuration.ORIENTATION_PORTRAIT)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor() {
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.statusBar_default_color));
    }

    /**
     * 着色状态栏（4.4以上系统有效）
     */
    protected void SetStatusBarColor(int color) {
        StatusBarCompat.setStatusBarColor(this, color);
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.statusBar_default_color));
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }


    /**
     * 通用的页面跳转方法；
     *
     * @param cls
     */
    public void startActivity(Class cls) {
        startActivity(new Intent(this, cls));
    }

    /**
     * 通用的页面跳转方法；传递String的数据；通过","进行切分后进行arg的赋值；
     *
     * @param cls
     */
    public void startActivity(Class cls, String... args) {
        Intent intent = new Intent(this, cls);
        for (String str :
                args) {
            String[] arg = str.split(",");
            if (arg.length == 2 && !"".equals(arg[0]) && !"".equals(arg[1])) {
                intent.putExtra(arg[0], arg[1]);
            }

        }
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getInstance().killActivity(this);
    }

    /**
     * 画页是否存活
     *
     * @return
     */
    public boolean isActive() {
        return !isFinishing();
    }

    /**
     * 弹出加载对话框
     */
    public void showLoadingDialog() {

    }

    /**
     * 关闭加载对话框
     */
    public void closeLoadingDialog() {

    }

    private long mLastOnKeyTime = -1;

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            //两次点击大于1秒
            long currentTime = System.currentTimeMillis();
            if (mLastOnKeyTime > 0 && currentTime - mLastOnKeyTime < 500) {
                return true;
            }
            mLastOnKeyTime = currentTime;
            LogUtil.e("---查看按键对应的数字---" + event.getKeyCode());
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_DEL://返回键
                    finish();
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}
