package com.casic.dcms.fragment.handle;

import android.os.Bundle;

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

import com.casic.dcms.R;
import com.casic.dcms.adapter.NormalRecyclerAdapter;
import com.casic.dcms.databinding.FragmentHandleCaseBinding;
import com.casic.dcms.model.CaseHandledBean;
import com.casic.dcms.view.handle.CaseHandledDetailActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
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 CaseHandledFragment extends AndroidxBaseFragment<FragmentHandleCaseBinding> {

    private static final String TAG = "CaseHandledFragment";
    private CaseViewModel caseViewModel;
    private NormalRecyclerAdapter<CaseHandledBean.DataModel.RowsModel> 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.caseHandledModel.observe(this, new Observer<CaseHandledBean>() {
            @Override
            public void onChanged(CaseHandledBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        getHandleCaseList();
    }

    private void bindRecyclerView(CaseHandledBean resultBean) {
        List<CaseHandledBean.DataModel.RowsModel> 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 NormalRecyclerAdapter<CaseHandledBean.DataModel.RowsModel>(R.layout.item_case_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, CaseHandledBean.DataModel.RowsModel item) {
                        viewHolder.setText(R.id.caseTitleView, item.getDescription())
                                .setText(R.id.casePlaceView, "发生地点：" + item.getFieldintro())
                                .setText(R.id.caseTimeView, "上报时间：" + item.getReportTime());
                    }
                };
                getBinding().caseRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<CaseHandledBean.DataModel.RowsModel>() {
                    @Override
                    public void onItemClicked(int position, CaseHandledBean.DataModel.RowsModel rowsModel) {
                        ContextKit.navigatePageTo(requireContext(), CaseHandledDetailActivity.class, rowsModel.getId());
                    }
                });
            }
        }
    }

    private void getHandleCaseList() {
        caseViewModel.getHandledCaseList(requireContext(), page);
    }

    @Override
    protected void initEvent() {
        getBinding().refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                getHandleCaseList();
            }
        });
        getBinding().refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                getHandleCaseList();
            }
        });
    }
}
