package com.mayi.fastdevelop.base;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.mayi.fastdevelop.util.ToastUtil;
import com.mayi.fastdevelop.view.LoadingDialog;

public abstract class BaseActivity<P extends BasePresenter, M extends BaseAPI> extends AppCompatActivity {


    private Dialog dialog;
    //p 层实例
    private BasePresenter mPresenter;
    //model 层实例
    private BaseAPI mModel;


    //获取 Presenter 实例
    public abstract P getPresenterInstance();

    //获取 Model 实例
    public abstract M getModelInstance();

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (mModel == null) {
            //实例化 Model
            mModel = getModelInstance();
        }

        if (mPresenter == null) {
            mPresenter = getPresenterInstance();
        }
        if (mPresenter != null) {
            mPresenter.setVM(this, mModel);
        }
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//强制竖屏
    }

    @SuppressWarnings("unchecked")
    protected P getPresenter() {
        if (mPresenter == null) {
            return null;
        }
        return (P) mPresenter;
    }

    @SuppressWarnings("unchecked")
    protected M getModel() {
        if (mModel == null) {
            return null;
        }
        return (M) mModel;
    }

    public void showLoadingDialog() {
        if (dialog == null) {
            dialog = LoadingDialog.init(this).setCanTouchOutsideCancel(true).show();
        } else {
            dialog.show();
        }
    }

    public void dismssLoadingDialog() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    /**
     * 跳转页面并结束当前页面
     *
     * @param activity
     */
    public void gotoActivityAndFinish(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转页面
     *
     * @param activity
     */
    public void gotoActivity(Class activity) {
        Intent intent = new Intent(this, activity);
        startActivity(intent);
    }

    /**
     * 跳转页面并结束当前页面
     *
     * @param activity
     */
    public void gotoActivityAndFinish(Class activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }

    /**
     * 跳转页面
     *
     * @param activity
     */
    public void gotoActivity(Class activity, Bundle bundle) {
        Intent intent = new Intent(this, activity);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    public void showToast(String msg) {
        ToastUtil.show(this, msg);
    }

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