package com.app.baselibrary.base.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.tbruyelle.rxpermissions.RxPermissions;

import butterknife.ButterKnife;

/**
 * Created by zhaopenglei on 16/9/19.
 */
public abstract class BaseFragment extends Fragment {

    protected View root;
    public RxPermissions mRxPermissions;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        if (root == null)
            root = inflater.inflate(getLayoutResource(), container, false);
        ButterKnife.bind(this, root);
        init();
        mRxPermissions = new RxPermissions(getActivity());
        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

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

    //获取布局文件
    protected abstract int getLayoutResource();

    protected abstract void init();

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(new Intent(getActivity(), cls));
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

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

    /**
     * 通用的页面跳转方法；传递String的数据；通过","进行切分后进行arg的赋值；
     *
     * @param cls
     */
    public void startActivity(Class cls, String... args) {
        Intent intent = new Intent(getActivity(), 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(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    @Override
    public void onResume() {
        super.onResume();
    }


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

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 画页是否存活
     * @return
     */
    public boolean isActive(){
        return this.isAdded() && this.isVisible();
    }
    /**
     * 弹出加载对话框
     */
    public void showLoadingDialog(){

    }

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

    }
}
