package com.unis.sxzcl.base;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewbinding.ViewBinding;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.unis.sxzcl.widget.MyLoadMoreView;

/**
 * 包名: com.unis.sxzcl.base
 * 类名: BaseListFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/11/9 14:05
 * 更新人:
 * 更新时间: 2020/11/9 14:05
 * 更新说明:
 * 版本号: 1.0
 */
public abstract class BaseListFragment<T extends ViewBinding> extends BaseFragment<T> {

    protected BaseQuickAdapter mBaseAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private boolean mPaging;
    private boolean firstAnimation = true;

    /************************ 对外提供方法 ************************/

    /**
     * 刷新数据
     */
    public abstract void onRefreshData();

    /**
     * 加载更多
     */
    public void onMoreData() {

    }

    /**
     * 刷新数据
     * 显示刷新按钮
     */
    public void onRefresh() {
        refreshView(true);
        onRefreshData();
    }

    /**
     * 加载完成
     */
    protected void loadComplete() {
        loadFinish();
        if (mPaging)
            mBaseAdapter.getLoadMoreModule().loadMoreComplete();//完成
        // 第一次加载动画
        if (firstAnimation) {
            mRecyclerView.scheduleLayoutAnimation();
            firstAnimation = false;
        }
    }

    /**
     * 已加载全部
     */
    protected void loadEnd() {
        loadFinish();
        if (mPaging)
            mBaseAdapter.getLoadMoreModule().loadMoreEnd();//已加载所有
    }

    /**
     * 加载失败
     */
    protected void loadError() {
        loadFinish();
        if (mPaging)
            mBaseAdapter.getLoadMoreModule().loadMoreFail();
        mBaseAdapter.setEmptyView(getErrorView(mClickListener));
    }


    /************************ 具体实现内容 ************************/
    /**
     * 初始化列表控件
     *
     * @param refreshLayout 下拉刷新控件
     * @param recyclerView  列表控件
     * @param adapter       列表适配器
     * @param paging        是否分页加载
     */
    public void initBaseListView(SwipeRefreshLayout refreshLayout,
                                 RecyclerView recyclerView,
                                 BaseQuickAdapter adapter,
                                 boolean paging) {
        initBaseListView(refreshLayout, recyclerView, adapter, paging, new LinearLayoutManager(getContext()));
    }

    public void initBaseListView(SwipeRefreshLayout refreshLayout,
                                 RecyclerView recyclerView,
                                 BaseQuickAdapter adapter,
                                 boolean paging, RecyclerView.LayoutManager layoutManager) {
        mRefreshLayout = refreshLayout;
        mRecyclerView = recyclerView;
        mPaging = paging;
        //下拉刷新
        initRefreshView(refreshLayout, () -> {
            refreshView(true);
            onRefreshData();
        });
        recyclerView.setLayoutManager(layoutManager);
        //列表适配器
        mBaseAdapter = adapter;
        //空布局
        mBaseAdapter.setEmptyView(getEmptyView(mClickListener));
        //加载更多
        if (mPaging) {
            //这里报错时 请在适配器类中实现接口 implements LoadMoreModule
            BaseLoadMoreModule moreModule = mBaseAdapter.getLoadMoreModule();
            moreModule.setLoadMoreView(new MyLoadMoreView());
            moreModule.setOnLoadMoreListener(() -> {
                refreshView(false);
                onMoreData();
            });
        }
        recyclerView.setAdapter(mBaseAdapter);
    }

    /**
     * 点击 空布局\错误布局 刷新数据
     */
    View.OnClickListener mClickListener = v -> {
        refreshView(true);
        onRefreshData();
    };

    /**
     * 设置刷新控件
     */
    private void refreshView(boolean isRefresh) {
        if (isRefresh) {
            mBaseAdapter.getLoadMoreModule().setEnableLoadMore(false);
            mRefreshLayout.setRefreshing(true);
            firstAnimation = true;
        } else {
            mRefreshLayout.setEnabled(false);
        }
        mBaseAdapter.setEmptyView(getLoadingView());
    }

    /**
     * 加载完成时修改各控件状态
     */
    private void loadFinish() {
        //可以下拉刷新
        mRefreshLayout.setEnabled(true);
        //停止刷新动画
        mRefreshLayout.setRefreshing(false);
        //可以加载更多
        if (mPaging)
            mBaseAdapter.getLoadMoreModule().setEnableLoadMore(true);
        //空页面
        mBaseAdapter.setEmptyView(getEmptyView(mClickListener));

    }


}
