package com.casic.dcms.fragment.department;

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.FragmentDeptCaseBinding;
import com.casic.dcms.model.dept.HandleCaseModel;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.view.department.DeptTodoDetailActivity;
import com.casic.dcms.vm.DeptCaseViewModel;
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 DeptCaseProcessFragment extends AndroidxBaseFragment<FragmentDeptCaseBinding> {

    private WeakReferenceHandler weakReferenceHandler;
    private DeptCaseViewModel handleViewModel;
    private NormalRecyclerAdapter<HandleCaseModel.DataModel.RowsModel> adapter;
    private List<HandleCaseModel.DataModel.RowsModel> 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);
        handleViewModel = new ViewModelProvider(this).get(DeptCaseViewModel.class);
        handleViewModel.deptHandleCaseListModel.observe(this, new Observer<HandleCaseModel>() {
            @Override
            public void onChanged(HandleCaseModel resultBean) {
                if (resultBean.isSuccess()) {
                    List<HandleCaseModel.DataModel.RowsModel> 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(2021062102);
                    }
                }
            }
        });
    }

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

    private void obtainDeptStatusCaseList() {
        handleViewModel.obtainDeptStatusCaseList(pageIndex, "process");
    }

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

    private final Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 2021062102) {
                if (dataBeans.size() == 0) {
                    viewBinding.emptyView.show(false, "您暂时没有待处理案卷", null, "刷新", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            pageIndex = 1;
                            obtainDeptStatusCaseList();
                        }
                    });
                } else {
                    viewBinding.emptyView.hide();
                    adapter = new NormalRecyclerAdapter<HandleCaseModel.DataModel.RowsModel>(R.layout.item_case_rv_l, dataBeans) {
                        @Override
                        public void convertView(ViewHolder viewHolder, int position, HandleCaseModel.DataModel.RowsModel 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<HandleCaseModel.DataModel.RowsModel>() {
                        @Override
                        public void onItemClicked(int position, HandleCaseModel.DataModel.RowsModel rowsModel) {
                            Intent intent = new Intent(requireContext(), DeptTodoDetailActivity.class);
                            intent.putExtra("title", LocaleConstant.DEPT_PAGE_TITLES[1]);
                            intent.putExtra("id", rowsModel.getId());
                            startActivity(intent);
                        }
                    });
                }
            }
            return true;
        }
    };
}