package com.casic.dcms.ui.fragment.dept;

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

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.casic.dcms.adapter.DeptDelayedOrPostAdapter;
import com.casic.dcms.base.AndroidxBaseFragment;
import com.casic.dcms.bean.DelayedOrPostBean;
import com.casic.dcms.databinding.FragmentDeptCaseBinding;
import com.casic.dcms.mvp.presenter.impl.DeptPostponePresenterImpl;
import com.casic.dcms.mvp.view.IDeptPostOrDelayedView;
import com.casic.dcms.ui.CaseDetailActivity;
import com.casic.dcms.utils.ToastHelper;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class DeptCasePostponeFragment extends AndroidxBaseFragment<FragmentDeptCaseBinding> implements IDeptPostOrDelayedView {

    private Context context;
    private int pageIndex = 1;
    private boolean isRefresh, isLoadMore = false;
    private DeptPostponePresenterImpl deptPresenter;
    private List<DelayedOrPostBean.DataBean.RowsBean> dataBeans = new ArrayList<>();
    private DeptDelayedOrPostAdapter adapter;
    private static WeakReferenceHandler weakReferenceHandler;

    @Override
    protected void setupTopBarLayout() {
        context = getContext();
    }

    @Override
    protected void initData() {
        deptPresenter = new DeptPostponePresenterImpl(this);
        deptPresenter.onReadyRetrofitRequest(pageIndex);

        weakReferenceHandler = new WeakReferenceHandler(this);
    }

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

    private static class WeakReferenceHandler extends Handler {
        private final WeakReference<DeptCasePostponeFragment> reference;

        private WeakReferenceHandler(DeptCasePostponeFragment fragment) {
            reference = new WeakReference<>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            DeptCasePostponeFragment deptCaseFragment = reference.get();
            if (msg.what == 2021062103) {
                if (deptCaseFragment.isRefresh || deptCaseFragment.isLoadMore) {
                    deptCaseFragment.adapter.notifyDataSetChanged();
                } else {
                    //首次加载数据
                    if (deptCaseFragment.dataBeans.size() == 0) {
                        deptCaseFragment.viewBinding.emptyView.show("您暂时没有待办案卷", null);
                    } else {
                        deptCaseFragment.viewBinding.emptyView.hide();
                        deptCaseFragment.adapter = new DeptDelayedOrPostAdapter(deptCaseFragment.context, deptCaseFragment.dataBeans);
                        deptCaseFragment.viewBinding.caseRecyclerView.setLayoutManager(new LinearLayoutManager(deptCaseFragment.context));
                        deptCaseFragment.viewBinding.caseRecyclerView.addItemDecoration(new DividerItemDecoration(deptCaseFragment.context, DividerItemDecoration.VERTICAL));
                        deptCaseFragment.viewBinding.caseRecyclerView.setAdapter(deptCaseFragment.adapter);
                        deptCaseFragment.adapter.setOnItemClickListener(new DeptDelayedOrPostAdapter.OnItemClickListener() {
                            @Override
                            public void onClick(int position) {
                                Intent intent = new Intent(deptCaseFragment.context, CaseDetailActivity.class);
                                intent.putExtra("id", deptCaseFragment.dataBeans.get(position).getId());
                                deptCaseFragment.startActivity(intent);
                            }
                        });
                    }
                }
            }
        }
    }

    @Override
    public void obtainResult(DelayedOrPostBean resultBean) {
        if (resultBean.isSuccess()) {
            List<DelayedOrPostBean.DataBean.RowsBean> dataRows = resultBean.getData().getRows();
            if (isRefresh) {
                dataBeans.clear();//下拉刷新必须先清空之前的List，不然会出现数据重复的问题
                dataBeans = dataRows;
                viewBinding.refreshLayout.finishRefresh();
                isRefresh = false;
            } else if (isLoadMore) {
                if (dataRows.size() == 0) {
                    ToastHelper.showToast("到底了，别拉了", ToastHelper.INFO);
                }
                dataBeans.addAll(dataRows);
                viewBinding.refreshLayout.finishLoadMore();
                isLoadMore = false;
            } else {
                dataBeans = dataRows;
            }
            weakReferenceHandler.sendEmptyMessage(2021062103);
        }
    }

    @Override
    public void onDestroyView() {
        if (deptPresenter != null) {
            deptPresenter.disposeRetrofitRequest();
        }
        super.onDestroyView();
    }
}
