package com.wisewoods.base.page;

import android.os.Bundle;

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

import com.blankj.utilcode.util.Utils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.wisewoods.base.base.BaseFragment;

import java.util.List;

public abstract class BaseListFragment<VB extends ViewBinding> extends BaseFragment<VB>
        implements SwipeRefreshLayout.OnRefreshListener, OnLoadMoreListener {
    protected RecyclerView rv;
    protected SwipeRefreshLayout srf;
    protected PageInfo pageInfo;

    @Override
    public Object getLayout() {
        srf = new SwipeRefreshLayout(Utils.getApp());
        rv = new RecyclerView(Utils.getApp());
        srf.addView(rv);
        return srf;
    }

    protected abstract void initListData();

    protected RecyclerView.LayoutManager initListLayoutManager() {
        return new LinearLayoutManager(Utils.getApp());
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        initListData();
        pageInfo = new PageInfo();
        srf.setOnRefreshListener(this);
        rv.setLayoutManager(initListLayoutManager());
        rv.setAdapter(getAdapter());
        getAdapter().getLoadMoreModule().setOnLoadMoreListener(this);

        srf.setRefreshing(true);
        getData(pageInfo.page, pageInfo.size);
    }

    public abstract BaseQuickAdapter getAdapter();

    protected abstract void getData(long page, long size);

    protected <T extends List> void getDataSuccess(T result, long total) {
        srf.setRefreshing(false);
        getAdapter().getLoadMoreModule().setEnableLoadMore(true);

        if (pageInfo.isFirstPage()) {
            //如果是加载的第一页数据，用 setData()
            getAdapter().setList(result);
            pageInfo.setData(result);
        } else {
            //不是第一页，则用add
            getAdapter().addData(result);
            pageInfo.addData(result);
        }

        if (result.size() < pageInfo.size || total <= pageInfo.currentSize()) {
            //如果不够一页,显示没有更多数据布局
            getAdapter().getLoadMoreModule().loadMoreEnd();
        } else {
            getAdapter().getLoadMoreModule().loadMoreComplete();
        }

        // page加一
        pageInfo.nextPage();
    }

    protected void getDataFail() {
        srf.setRefreshing(false);
        getAdapter().getLoadMoreModule().setEnableLoadMore(true);
        getAdapter().getLoadMoreModule().loadMoreFail();
    }

    @Override
    public void onRefresh() {
        getAdapter().setNewInstance(null);
        getAdapter().getLoadMoreModule().setEnableLoadMore(false);
        // 下拉刷新，需要重置页数
        pageInfo.reset();
        getData(pageInfo.page, pageInfo.size);
    }

    @Override
    public void onLoadMore() {
        getData(pageInfo.page, pageInfo.size);
    }
}
