package com.casic.dcms.fragment.dept;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

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.FragmentDeptCaseBinding;
import com.casic.dcms.model.dept.HandleCaseModel;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.view.dept.DeptTodoDetailActivity;
import com.casic.dcms.vm.DeptCaseViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.StringKit;
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 deptCaseViewModel;
    private NormalRecyclerAdapter<HandleCaseModel.DataModel.RowsModel> adapter;
    private List<HandleCaseModel.DataModel.RowsModel> dataBeans = new ArrayList<>();
    private int page = 1;
    private boolean isRefresh, isLoadMore = false;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        weakReferenceHandler = new WeakReferenceHandler(callback);
        deptCaseViewModel = new ViewModelProvider(this).get(DeptCaseViewModel.class);
        deptCaseViewModel.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.refresh(dataRows);
                        getBinding().refreshLayout.finishRefresh();
                        isRefresh = false;
                    } else if (isLoadMore) {
                        if (dataRows.size() == 0) {
                            StringKit.show(requireContext(), "到底了，别拉了");
                        }
                        adapter.loadMore(dataRows);
                        getBinding().refreshLayout.finishLoadMore();
                        isLoadMore = false;
                    } else {
                        dataBeans = dataRows;
                        weakReferenceHandler.sendEmptyMessage(2021062102);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        getDeptStatusCaseList();
    }

    private void getDeptStatusCaseList() {
        deptCaseViewModel.getDeptStatusCaseList(requireContext(), page, "process");
    }

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

    private final Handler.Callback callback = new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            if (msg.what == 2021062102) {
                if (dataBeans.size() == 0) {
                    getBinding().emptyView.show(null, "您暂时没有待处理案卷");
                } else {
                    getBinding().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, item.getDescription())
                                    .setText(R.id.casePlaceView, "发生地点：" + item.getFieldintro())
                                    .setText(R.id.caseTimeView, "上报时间：" + item.getReportTime());
                        }
                    };
                    getBinding().recyclerView.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;
        }
    };
}