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.Window;

import com.app.baselibrary.BuildConfig;
import com.app.baselibrary.R;
import com.app.baselibrary.base.app.AppManager;
import com.app.baselibrary.commonUtil.StatusBarCompat;
import com.tbruyelle.rxpermissions.RxPermissions;
import com.trello.rxlifecycle.LifecycleTransformer;
import com.trello.rxlifecycle.android.ActivityEvent;
import com.trello.rxlifecycle.components.support.RxFragmentActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Desc: base FragmentActivity;
 * Author: pl
 * Date: 2017-03-20 16:49
 */

public abstract class BaseFragmentActivity extends RxFragmentActivity {

    public RxPermissions mRxPermissions;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseInit();
        setContentView(getLayoutId());//设置布局文件；
        //Bus.getInstance().register(this);

        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);
        // 设置竖屏
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //设置状态栏的主颜色：
        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_LANDSCAPE)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        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));
        //StatusBarCompat.translucentStatusBar(this);
    }

    /**
     * 含有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();
//        if (Bus.getInstance().isRegistered(this))
//            Bus.getInstance().unregister(this);
        AppManager.getInstance().killActivity(this);
    }

    /**
     * 绑定生命周期
     * @param <T>
     * @return
     */
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.<T>bindUntilEvent(ActivityEvent.DESTROY);
    }
}
