package com.sheep.jiuyan.samllsheep.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.util.ActionUtil;
import com.sheep.gamegroup.view.dialog.DialogLoading;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

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

public abstract class BaseFragment extends RxFragment {

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

    protected DialogLoading dialogLoading;
    protected Unbinder unbinder;


    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) {
        View mView = getView();
        if (getView() == null) {
            mIsFirstInit = true;
            mView = inflater.inflate(getLayoutId(), null);
        } else {
            mIsFirstInit = false;
        }
        return mView;
    }

    //绑定view
    public void butterKnifeBindView() {
        if(userButterKnife() && getView() != null)
            unbinder = ButterKnife.bind(this, getView());
    }

    public abstract int getLayoutId();

    public abstract void onViewCreated();

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

    protected boolean firstVisibleToUser = true;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && getView() != null) {
            if (firstVisibleToUser) {
                initViewOnVisibleToUser();
            }
            initDataOnVisibleToUser();
            firstVisibleToUser = false;
        }
    }

    protected void initViewOnVisibleToUser() {

    }

    protected void initDataOnVisibleToUser() {

    }

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

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

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (userButterKnife())
            unbinder.unbind();
    }

    public boolean userButterKnife() {
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        checkNextAction(this.getClass().getSimpleName());
    }

    protected boolean checkNextAction(String key) {
        final Object action = ActionUtil.getInstance().getAction(key);
        if (action != null) {
            //延时一会儿，不然有可能操作会无效
            Observable.just(1).delay(50L, TimeUnit.MILLISECONDS)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new AbsObserver<Integer>() {
                        @Override
                        public void onNext(Integer integer) {
                            doNextAction(action);
                        }
                    });
            return true;
        }
        return false;
    }
    public void doNextAction(Object action) {

    }
}
