package com.casic.dcms.fragment.sanbao;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.casic.dcms.adapter.SanBaoCaseAdapter;
import com.casic.dcms.databinding.FragmentSanbaoCaseBinding;
import com.casic.dcms.model.SanBaoCaseBean;
import com.casic.dcms.view.sanbao.SanBaoHandledDetailActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class SanBaoHandledFragment extends AndroidxBaseFragment<FragmentSanbaoCaseBinding> {
    private CaseViewModel caseViewModel;
    private SanBaoCaseAdapter adapter;
    private int page = 1;
    private boolean isRefresh, isLoadMore = false;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        caseViewModel = new ViewModelProvider(this).get(CaseViewModel.class);
        caseViewModel.shopCaseModel.observe(this, new Observer<SanBaoCaseBean>() {
            @Override
            public void onChanged(SanBaoCaseBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        getPackageCaseList();
    }

    private void bindRecyclerView(SanBaoCaseBean resultBean) {
        List<SanBaoCaseBean.DataBean.RowsBean> dataRows = resultBean.getData().getRows();
        if (isRefresh) {
            getBinding().refreshLayout.finishRefresh();
            isRefresh = false;
            adapter.refresh(dataRows);
        } else if (isLoadMore) {
            getBinding().refreshLayout.finishLoadMore();
            isLoadMore = false;
            if (dataRows.size() == 0) {
                StringKit.show(requireContext(), "到底了，别拉了");
            }
            adapter.loadMore(dataRows);
        } else {
            if (dataRows.size() == 0) {
                getBinding().emptyView.show(null, "没有已处理的案卷");
            } else {
                getBinding().emptyView.hide();
                adapter = new SanBaoCaseAdapter(requireContext(), dataRows);
                getBinding().recyclerView.setAdapter(adapter);
                adapter.setOnItemClickListener(new SanBaoCaseAdapter.OnItemClickListener() {
                    @Override
                    public void onClick(int position) {
                        ContextKit.navigatePageTo(requireContext(), SanBaoHandledDetailActivity.class, dataRows.get(position).getId());
                    }
                });
            }
        }
    }

    public void getPackageCaseList() {
        caseViewModel.getShopCaseList(requireContext(), "1", page, true);
    }

    @Override
    protected void initEvent() {
        getBinding().refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                isRefresh = true;
                //刷新之后页码重置
                page = 1;
                getPackageCaseList();
            }
        });

        getBinding().refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                getPackageCaseList();
            }
        });
    }
}
