package com.casic.dcms.fragment.urgent;

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.FragmentUrgentCaseBinding;
import com.casic.dcms.model.UrgentCaseBean;
import com.casic.dcms.view.CaseTodoHandledDetailActivity;
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<FragmentUrgentCaseBinding> {

    private static final String TAG = "CaseHandledFragment";
    private NormalRecyclerAdapter<UrgentCaseBean.DataBean.RowsBean> adapter;
    private CaseViewModel caseViewModel;
    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.caseUrgentModel.observe(this, new Observer<UrgentCaseBean>() {
            @Override
            public void onChanged(UrgentCaseBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        getUrgentCaseList();
    }

    private void bindRecyclerView(UrgentCaseBean resultBean) {
        List<UrgentCaseBean.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 NormalRecyclerAdapter<UrgentCaseBean.DataBean.RowsBean>(R.layout.item_case_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, UrgentCaseBean.DataBean.RowsBean 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<UrgentCaseBean.DataBean.RowsBean>() {
                    @Override
                    public void onItemClicked(int position, UrgentCaseBean.DataBean.RowsBean rowsBean) {
                        ContextKit.navigatePageTo(requireContext(), CaseTodoHandledDetailActivity.class, dataRows.get(position).getId());
                    }
                });
            }
        }
    }

    private void getUrgentCaseList() {
        //isSupervise	否	string	是否督办,0-未督办，1-已督办
        caseViewModel.getUrgentCaseList(requireContext(), "1", page);
    }

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