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.widget.FrameLayout;

import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.header.MaterialHeader;
import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter;
import com.zhaohaoting.framework.abs.presenter.BasePresenter;
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource;
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.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 BaseRecyclerActivity<DATA, RESULT_DATA> extends BaseActivity implements AbsRecyclerAdapter.OnItemClickListener, AbsRecyclerAdapter.OnItemLongClickListener {

    protected MVCCoolHelper<RESULT_DATA> mvcHelper;
    protected CoolRefreshView coolRefreshView;
    public AbsRecyclerAdapter<DATA, RESULT_DATA> adapter;
    protected IAsyncDataSource<RESULT_DATA> dataSource;
    protected RecyclerView recyclerView;
    protected FrameLayout flTitle;
    protected FrameLayout flBottom;

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

    @Override
    protected void initialize() {
        coolRefreshView = findViewById(R.id.funnyRefreshView);
        recyclerView = findViewById(R.id.recycler_view);
        flTitle = findViewById(R.id.fl_title);
        flBottom = findViewById(R.id.fl_bottom);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(getLayoutManager());
//        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        coolRefreshView.setPullHeader(new MaterialHeader(this), true);
        mvcHelper = getMvcHelper(coolRefreshView);
        getLifecycle().addObserver(mvcHelper);//添加LifecycleObserver
        onInit();
        mvcHelper.setDataSource(dataSource = getDataSource());
        adapter = getAdapter();
        addHeadViews(adapter);
        mvcHelper.setAdapter2(adapter, adapter);
        mvcHelper.refresh();
        //https://github.com/LuckyJayce/MVCHelper/blob/master/Info.md
        adapter.addOnItemClickListener(this);
        adapter.addOnItemLongClickListener(this);
    }

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

    protected RecyclerView.LayoutManager getLayoutManager() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        return layoutManager;
    }

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

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

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

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

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

    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) {
    }

    protected void itemLongClicked(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();
    }
}