package com.yidianling.course.courseNew.courseList;

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 butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseLazyFragment extends Fragment {

    protected boolean isVisible;
    protected boolean isUseLazyMode=true;
    private Unbinder mUnBinder;
    protected View parentView;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parentView = inflater.inflate(getLayoutId(), container, false);
        return parentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mUnBinder = ButterKnife.bind(this, view);
        afterViewCreated(view,savedInstanceState);
    }

    public abstract void afterViewCreated(View view,Bundle savedInstanceState);

    public abstract int getLayoutId();

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (!isUseLazyMode){
            return;
        }
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    public BaseLazyFragment setIsUseLazyMode(boolean isUseLazyMode){
        this.isUseLazyMode=isUseLazyMode;
        return this;
    }

    protected void onInvisible() {
    }

    protected void onVisible() {
        lazyLoad();
    };

    public abstract void lazyLoad();

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

    public View getParentView(){
        return parentView;
    }
}