package com.casic.dcms.fragment.business;

import android.os.Handler;
import android.os.Message;
import android.view.View;

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

import com.casic.dcms.adapter.PackageCaseListAdapter;
import com.casic.dcms.databinding.FragmentPackageCaseBinding;
import com.casic.dcms.model.PackageCaseBean;
import com.casic.dcms.view.business.PackageHandledDetailActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.hub.ContextHub;
import com.pengxh.androidx.lite.hub.StringHub;
import com.pengxh.androidx.lite.utils.WeakReferenceHandler;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class PackageHandledFragment extends AndroidxBaseFragment<FragmentPackageCaseBinding> {

    private WeakReferenceHandler weakReferenceHandler;
    private CaseViewModel caseViewModel;
    private PackageCaseListAdapter adapter;
    private List<PackageCaseBean.DataDTO.RowsDTO> dataBeans = new ArrayList<>();
    private int pageIndex = 1;
    private boolean isRefresh, isLoadMore = false;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initData() {
        weakReferenceHandler = new WeakReferenceHandler(callback);
        caseViewModel = new ViewModelProvider(this).get(CaseViewModel.class);
    }

    @Override
    public void onResume() {
        super.onResume();
        obtainPackageCaseList();
    }

    public void obtainPackageCaseList() {
        caseViewModel.obtainShopCaseList("1", pageIndex, true);
    }

    @Override
    protected void initEvent() {
        viewBinding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                isRefresh = true;
                //刷新之后页码重置
                pageIndex = 1;
                obtainPackageCaseList();
            }
        });
        viewBinding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                isLoadMore = true;
                pageIndex++;
                obtainPackageCaseList();
            }
        });

        caseViewModel.shopCaseModel.observe(this, new Observer<PackageCaseBean>() {
            @Override
            public void onChanged(PackageCaseBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<PackageCaseBean.DataDTO.RowsDTO> dataRows = resultBean.getData().getRows();
                    if (isRefresh) {
                        adapter.setRefreshData(dataRows);
                        viewBinding.refreshLayout.finishRefresh();
                        isRefresh = false;
                    } else if (isLoadMore) {
                        if (dataRows.size() == 0) {
                            StringHub.show(requireContext(), "到底了，别拉了");
                        }
                        adapter.setLoadMoreData(dataRows);
                        viewBinding.refreshLayout.finishLoadMore();
                        isLoadMore = false;
                    } else {
                        dataBeans = dataRows;
                        weakReferenceHandler.sendEmptyMessage(2021060301);
                    }
                }
            }
        });
    }

    private final Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 2021060301) {
                if (dataBeans.size() == 0) {
                    viewBinding.emptyView.show(false, "没有案卷需要处理", null, "刷新", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pageIndex = 1;
                            obtainPackageCaseList();
                        }
                    });
                } else {
                    viewBinding.emptyView.hide();
                    adapter = new PackageCaseListAdapter(requireContext(), dataBeans);
                    viewBinding.caseRecyclerView.setAdapter(adapter);
                    adapter.setOnItemClickListener(new PackageCaseListAdapter.OnItemClickListener() {
                        @Override
                        public void onClick(int position) {
                            ContextHub.navigatePageTo(requireContext(), PackageHandledDetailActivity.class, dataBeans.get(position).getId());
                        }
                    });
                }
            }
            return true;
        }
    };
}
