package com.baoshi.swms.activity.part;

import android.app.Activity;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import androidx.appcompat.app.AppCompatActivity;

import com.baoshi.swms.activity.changeView.ListViewFooterChange;
import com.baoshi.swms.inter.OnRefreshListener;
import com.baoshi.swms.part.RefreshHeader;

/**
 * listview在activity中的简单集成
 *
 * @author Zhendong Zhou
 * @date 2021/12/14 17:49
 */
public class BaseListViewIntegration extends AppCompatActivity implements
        AbsListView.OnScrollListener, OnRefreshListener {
    protected RefreshHeader listView;
    protected int scrollState;
    protected int topIndex;
    protected ListViewFooterChange viewChange; //变更视图对象
    protected boolean isNoneLoad = false;//防重载标记
    protected int paginate = 20; //每次加载大小
    protected int page = 1; //当前页数
    protected int currentLoadSize; //当前加载数据大小
    protected BaseAdapter adapter;//适配器

    protected void registerListViewEvent(Activity activity) {
        viewChange = new ListViewFooterChange(activity, listView);

        listView.setOnScrollListener(this);//滚动
        listView.setOnRefreshListener(this);//下拉
    }

    //初始化listview
    protected void loadListViewItem() {
        if (isNoneLoad)return; //正确做法应该卸载监听器 暂时不知 所以用标记来跳过加载
        viewChange.addLoadFooter();
        isNoneLoad = true;
        requestData(false);
    }

    //数据加载
    protected void requestData(boolean after) {
    }

    protected void afterOperation(boolean after){
        listView.removeFooterView(viewChange.mLoadLayout);
        if (after){//如果是追加数据 重载适配器
            listView.setAdapter(adapter);
            listView.onRefreshComplete();
        }else{//如果是刷新数据 通知容器视图变更
            adapter.notifyDataSetChanged();
        }

        if (currentLoadSize<paginate){
            viewChange.addLoadStopFooter();
            isNoneLoad = true; //如果本次加载数据不足N条 则标记加载完毕
        }else if (currentLoadSize==0){ //如果加载完毕了 数据总量仍为0 那么属于没有数据 显示空数据视图
            viewChange.addLoadNoneFooter();
            isNoneLoad = true;
        }else {
            isNoneLoad = false;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.scrollState = scrollState;
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        topIndex = firstVisibleItem;
        switch (this.scrollState){
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                if (topIndex+visibleItemCount==totalItemCount && !isNoneLoad){
                    page++;
                    loadListViewItem();
                }
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
        }
    }
    @Override
    public void refreshData() {
        if (topIndex==0){
            page = 1;
            requestData(true);
        } else listView.onRefreshComplete();
    }

    /*
    * 使用方式
    1.渲染视图后调用渲染适配器
    private void registerEvent() {
        //视图渲染后必须先设定一次适配器
        adapter = new CheckInventoryItem(list,CheckInventoryActivity.this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);//listview item click
    }
    2. 调用registerListViewEvent
    3. 覆写请求方法
    @Override
    protected void requestData(boolean after) {
        List<Stocktaking> stocktakingList = new ArrayList<>();
        Stocktaking stock = new Stocktaking();
        stock.setId(1L);
        stocktakingList.add(stock);

        //重载覆盖，续载追加
        if (after){
            list = stocktakingList;
            adapter = new CheckInventoryItem(list,CheckInventoryActivity.this);
        }else{
            list.addAll(stocktakingList);
        }
        currentLoadSize = list.size();
        afterOperation(after);
     }
    */
}
