package cn.dankal.basiclib.base.activity;

import android.app.Activity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;


import butterknife.ButterKnife;
import cn.dankal.basiclib.DankalApplication;
import cn.dankal.basiclib.R;
import cn.dankal.basiclib.base.mvp.BasePresenter;
import cn.dankal.basiclib.base.mvp.BaseView;
import cn.dankal.basiclib.base.lifecycle.LifeCycleUtil;
import cn.dankal.basiclib.base.lifecycle.LifecycleTransformer;
import cn.dankal.basiclib.base.lifecycle.MyLifeCycleObserver;
import cn.dankal.basiclib.util.TitleBarUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.TipDialog;
import cn.dankal.basiclib.widget.statubar.QMUIStatusBarHelper;
import cn.dankal.basiclib.widget.titlebar.ITitleBar;
import cn.dankal.basiclib.widget.titlebar.SingleTextTitle;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author Dankal Android Developer
 * @since 2018/7/3
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {

    protected TipDialog loadingDialog;
    private CompositeDisposable mDisposables = new CompositeDisposable();

    private LifecycleRegistry lifecycleRegistry;
    private MyLifeCycleObserver myLifeCycleObserver;


    public T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lifecycleRegistry = new LifecycleRegistry(this);
        myLifeCycleObserver = new MyLifeCycleObserver();
        lifecycleRegistry.addObserver(myLifeCycleObserver);
        lifecycleRegistry.markState(Lifecycle.State.CREATED);
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        if (getLayoutId() != 0) {

            setContentView(getLayoutId());

            mPresenter = createPresenter();
            if (mPresenter!=null){
                mPresenter.attachView(this);
            }


            //ButterKnife绑定
            ButterKnife.bind(this);

            initStatusBar();
            //初始化组件
            initComponents();

            obtainData();
        }

    }


    protected void initStatusBar() {
//        TitleBarUtils.compat(this, getResources().getColor(R.color.white));
        QMUIStatusBarHelper.setStatusBarLightMode(this);
//        QMUIStatusBarHelper.translucent(this);
    }

    public void setTranslucent(Activity context, View view) {
        if (view != null) {
//            QMUIStatusBarHelper.translucent(this);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
                view.setPadding(0, QMUIStatusBarHelper.getStatusbarHeight(context), 0, 0);
            }
        }
    }


    public abstract T createPresenter();

    /**
     * setContentView(LayoutResId)
     *
     * @return LayoutResId
     */
    @LayoutRes
    protected abstract int getLayoutId();

    /**
     * 初始化组件
     */
    protected abstract void initComponents();


    /**
     * 添加标题栏
     */
    public void addTitleBar(ITitleBar iTitleBar) {
        if (iTitleBar == null) return;

        int titleBarResId = iTitleBar.getViewResId();

        View toolbarContainer = TitleBarUtils.init(this, titleBarResId);

        toolbarContainer.findViewById(R.id.iv_onback)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onBackPressed();
                    }
                });

        iTitleBar.onBindTitleBar(toolbarContainer);
    }

    public void addSindleTitleBar(String title) {
        addTitleBar(new SingleTextTitle(title));
    }

    public void obtainData() {
    }

    @Override
    public void showLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
        loadingDialog = new TipDialog.Builder(this)
                .setIconType(TipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();
        loadingDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showToast(String message) {
        try {
            ToastUtils.showShort(message);
        } catch (Exception ignored) {
        }
    }

    @Override
    public void tokenInvalid() {
        DankalApplication.getContext().tokenInvalidOperate();
    }


    @Override
    public void addNetworkRequest(Disposable d) {
        mDisposables.add(d);
    }

    @Override
    protected void onStart() {
        super.onStart();
        lifecycleRegistry.markState(Lifecycle.State.STARTED);
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
    }

    @Override
    protected void onResume() {
        super.onResume();
        lifecycleRegistry.markState(Lifecycle.State.RESUMED);
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
    }


    @Override
    protected void onPause() {
        super.onPause();
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        lifecycleRegistry.markState(Lifecycle.State.STARTED);
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter!=null){
            mPresenter.detachView();
        }

        lifecycleRegistry.markState(Lifecycle.State.DESTROYED);
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        if (mDisposables != null) mDisposables.clear();
    }



}
