package com.sheep.jiuyan.samllsheep.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
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.sheep.gamegroup.view.dialog.DialogLoading;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  BaseFragment</p>
 * @ <p>Description:方便统计数据</p>
 * @ date:  2017/11/6 18:20
 * @ QQ:    315096953
 */

public abstract class BaseFragment extends Fragment {

    protected View mView;
    /**
     * 是否是第一次初始化，防止重复加载些东西用
     */
    protected boolean mIsFirstInit = true;

    protected DialogLoading dialogLoading;



    protected void  showProgress(){
        hideProgress();
        dialogLoading = DialogLoading.showDialog(getActivity());
    }

    protected void  showProgress(boolean cancel){
        hideProgress();
        dialogLoading = DialogLoading.showDialog(getActivity(), cancel);
    }

    public boolean isShowing(){
        return dialogLoading != null && dialogLoading.getAlertDialog() != null && dialogLoading.getAlertDialog().isShowing();
    }

    protected void  hideProgress(){
        if(isShowing()){
            try {
                dialogLoading.getAlertDialog().dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }




    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }





    /**
     * 注意用 mIsFirstInit 判断，防止重复加载
     *
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mIsFirstInit = true;
            mView = inflater.inflate(getLayoutId(), null);
        } else {
            mIsFirstInit = false;
        }
        return mView;
    }

    public abstract int getLayoutId();

    public abstract void onViewCreated();

    /**
     * 一般要继承这个方法
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onViewCreated();
    }

    /**
     * 万能的找View类
     *
     * @param id
     * @param <T>
     * @return
     */
    public <T extends View> T findViewById(@IdRes int id) {
        return (T)mView.findViewById(id);
    }

    /**
     * 万能的找View类
     *
     * @param id
     * @param tClass
     * @param <T>
     * @return
     */
    public <T extends View> T findViewById(@IdRes int id, Class<T> tClass) {
        return (T)mView.findViewById(id);
    }

}
