package com.casic.dcms.fragment.urgent;

import android.content.Intent;
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.R;
import com.casic.dcms.databinding.FragmentUrgentCaseBinding;
import com.casic.dcms.model.UrgentCaseBean;
import com.casic.dcms.view.CaseTodoDetailActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.adapter.NormalRecyclerAdapter;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
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 CaseTodoFragment extends AndroidxBaseFragment<FragmentUrgentCaseBinding> {

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

    @Override
    public void onResume() {
        super.onResume();
        pageIndex = 1;
        obtainUrgentCaseList();
    }

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

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

    private final Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 202104281) {
                if (dataBeans.size() == 0) {
                    viewBinding.emptyView.show(false, "没有紧急案卷", null, "刷新", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pageIndex = 1;
                            obtainUrgentCaseList();
                        }
                    });
                } else {
                    viewBinding.emptyView.hide();
                    adapter = new NormalRecyclerAdapter<UrgentCaseBean.DataBean.RowsBean>(R.layout.item_case_rv_l, dataBeans) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, UrgentCaseBean.DataBean.RowsBean item) {
                            viewHolder.setText(R.id.caseTitleView, StringHub.filterSpaceOrEnter(item.getDescription()))
                                    .setText(R.id.casePlaceView, "发生地点：" + StringHub.filterSpaceOrEnter(item.getFieldintro()))
                                    .setText(R.id.caseTimeView, "上报时间：" + item.getReportTime());
                        }
                    };
                    viewBinding.caseRecyclerView.setAdapter(adapter);
                    adapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<UrgentCaseBean.DataBean.RowsBean>() {
                        @Override
                        public void onItemClicked(int position, UrgentCaseBean.DataBean.RowsBean rowsBean) {
                            Intent intent = new Intent(requireContext(), CaseTodoDetailActivity.class);
                            intent.putExtra("title", "紧急要件");
                            intent.putExtra("id", rowsBean.getId());
                            startActivity(intent);
                        }
                    });
                }
            }
            return true;
        }
    };
}
