package com.zhaohaoting.framework.abs;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource;
import com.zhaohaoting.framework.mvchelper.mvc.MVCHelper;
import com.zhaohaoting.framework.mvchelper.mvc.OnLoadMoreStateChangeListener;
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshEndListener;
import com.zhaohaoting.framework.mvchelper.mvc.OnRefreshStateChangeListener;
import com.zhaohaoting.framework.mvchelper.mvc.OnStateChangeListener;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter;
import com.zhaohaoting.framework.abs.presenter.BasePresenter;
import com.zhaohaoting.framework.ui.helper.MVCCoolHelper;


/**
 * author：Created by ZHT on 2018/1/22 16:44
 * email：526309416@qq.com
 * desc：列表类activity基类 RESULT_DATA为BaseListEntity<DATA>或者List<DATA>（尽量规范）  此类也可不要泛型
 */
public abstract class BaseRecyclerFragment<DATA, RESULT_DATA> extends AbsV4Fragment implements AbsRecyclerAdapter.OnItemClickListener {

    protected MVCHelper<RESULT_DATA> mvcHelper;
    public AbsRecyclerAdapter<DATA, RESULT_DATA> adapter;
    protected boolean isVisibleToUser = false;
    protected boolean loaded;//是否已经加载了数据
    protected IAsyncDataSource<RESULT_DATA> dataSource;
    protected RecyclerView recyclerView;
    protected CoolRefreshView funnyRefreshView;
    protected FrameLayout flTitle;
    protected FrameLayout flbottom;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (!isVisibleToUser || adapter == null || loaded)
            return;//若当前Fragment不在最前面，或者adapter为null或者已经加载过数据了，就直接返回出去
        mvcHelper.refresh();
        loaded = true;
    }

    @Override
    protected void initialize() {
        recyclerView = contentView.findViewById(R.id.recycler_view);
        funnyRefreshView = contentView.findViewById(R.id.funnyRefreshView);
        flTitle = contentView.findViewById(R.id.fl_title);
        flbottom = contentView.findViewById(R.id.fl_bottom);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
//        binding.recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
        funnyRefreshView.setPullHeader(new MaterialHeader(getContext()), true);
        mvcHelper = getMvcHelper(funnyRefreshView);
        getLifecycle().addObserver(mvcHelper);//添加LifecycleObserver
        onInit();
        mvcHelper.setDataSource(dataSource = getDataSource());
        adapter = getAdapter();
        addHeadViews(adapter);
        mvcHelper.setAdapter2(adapter, adapter);
        //https://github.com/LuckyJayce/MVCHelper/blob/master/Info.md
        if (isVisibleToUser) {
            mvcHelper.refresh();
            loaded = true;
        }
        adapter.addOnItemClickListener(this);
    }

    protected  void addHeadViews(AbsRecyclerAdapter<DATA, RESULT_DATA> adapter){};

    protected MVCHelper<RESULT_DATA> getMvcHelper(CoolRefreshView funnyRefreshView) {
        return new MVCCoolHelper<>(funnyRefreshView);
    }

    @Override
    public void onItemClick(AbsRecyclerAdapter.BaseViewHolder vh, int position) {
        itemClicked(vh, position);
    }

    @Override
    protected BasePresenter initPresenter() {
        return null;
    }

    /**
     * 刷新
     */
    public void refresh() {
        mvcHelper.refresh();
    }

    protected abstract void onInit();

    protected abstract AbsRecyclerAdapter<DATA, RESULT_DATA> getAdapter();

    protected abstract IAsyncDataSource<RESULT_DATA> getDataSource();

    protected void itemClicked(AbsRecyclerAdapter.BaseViewHolder vh, int position) {
    }

    /**
     * 设置状态监听，监听开始刷新，刷新成功，开始加载更多，加载更多成功
     *
     * @param onStateChangeListener
     */
    public void setOnStateChangeListener(OnStateChangeListener<RESULT_DATA> onStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onStateChangeListener);
    }

    /**
     * 设置状态监听，监听开始刷新，刷新成功
     *
     * @param onRefreshStateChangeListener
     */
    public void setOnStateChangeListener(OnRefreshStateChangeListener<RESULT_DATA> onRefreshStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onRefreshStateChangeListener);
    }
    /**
     * 设置状态监听，监听开始刷新，刷新成功
     *
     */
    public void setOnStateChangeListener(OnRefreshEndListener<RESULT_DATA> onRefreshEndListener) {
        mvcHelper.setOnStateChangeListener(onRefreshEndListener);
    }

    /**
     * 设置状态监听，监听开始加载更多，加载更多成功
     *
     * @param onLoadMoreStateChangeListener
     */
    public void setOnStateChangeListener(OnLoadMoreStateChangeListener<RESULT_DATA> onLoadMoreStateChangeListener) {
        mvcHelper.setOnStateChangeListener(onLoadMoreStateChangeListener);
    }

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

    @Override
    public int getLayoutResId() {
        return R.layout.widget_recyclerview;
    }
}