package com.casic.dcms.view;

import android.content.Context;
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.ActivityCaseListBinding;
import com.casic.dcms.model.CaseSearchResultBean;
import com.casic.dcms.utils.ViewGroupKit;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseActivity;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.StringKit;
import com.pengxh.androidx.lite.utils.LoadState;
import com.pengxh.androidx.lite.utils.LoadingDialog;
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 CaseSearchResultActivity extends AndroidxBaseActivity<ActivityCaseListBinding> {

    private final Context context = this;
    private CaseViewModel caseViewModel;
    private NormalRecyclerAdapter<CaseSearchResultBean.DataBean.RowsBean> adapter;
    private String typeCode;
    private String typeDetailCode;
    private String caseState;
    private String startTime;
    private String endTime;
    private String eorc;
    private int page = 1;
    private boolean isRefresh = false;
    private boolean isLoadMore = false;

    @Override
    protected void setupTopBarLayout() {
        ViewGroupKit.initImmersionBar(binding.rootView, this, true, R.color.white);

        binding.titleBarLayout.titleView.setText("查询结果");
        binding.titleBarLayout.leftBackView.setOnClickListener(v -> finish());
    }

    @Override
    protected void observeRequestState() {
        caseViewModel.loadState.observe(this, new Observer<LoadState>() {
            @Override
            public void onChanged(LoadState loadState) {
                if (!isRefresh && !isLoadMore) {
                    if (loadState == LoadState.Loading) {
                        LoadingDialog.show(CaseSearchResultActivity.this, "案卷查询中，请稍后");
                    } else {
                        LoadingDialog.dismiss();
                    }
                }
            }
        });
    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        caseViewModel = new ViewModelProvider(this).get(CaseViewModel.class);

        eorc = getIntent().getStringExtra("eorc");
        typeCode = getIntent().getStringExtra("typeCode");
        typeDetailCode = getIntent().getStringExtra("typeDetailCode");
        caseState = getIntent().getStringExtra("caseState");
        startTime = getIntent().getStringExtra("startTime");
        endTime = getIntent().getStringExtra("endTime");
        searchCase();
    }

    private void searchCase() {
        //加载第一页
        caseViewModel.searchCase(this, eorc, typeCode, typeDetailCode, caseState, startTime, endTime, "0", page);
    }

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

        caseViewModel.searchResultModel.observe(this, new Observer<CaseSearchResultBean>() {
            @Override
            public void onChanged(CaseSearchResultBean resultBean) {
                if (resultBean.getCode() == 200) {
                    List<CaseSearchResultBean.DataBean.RowsBean> dataRows = resultBean.getData().getRows();
                    if (isRefresh) {
                        binding.refreshLayout.finishRefresh();
                        isRefresh = false;
                        adapter.refresh(dataRows);
                    } else if (isLoadMore) {
                        binding.refreshLayout.finishLoadMore();
                        isLoadMore = false;
                        if (dataRows.size() == 0) {
                            StringKit.show(context, "到底了，别拉了");
                        }
                        adapter.loadMore(dataRows);
                    } else {
                        if (dataRows.size() == 0) {
                            binding.emptyView.show(false, "抱歉，没有查到符合您规定条件的案卷", null, null, null);
                        } else {
                            binding.emptyView.hide();
                            adapter = new NormalRecyclerAdapter<CaseSearchResultBean.DataBean.RowsBean>(R.layout.item_case_rv_l, dataRows) {

                                @Override
                                public void convertView(ViewHolder viewHolder, int position, CaseSearchResultBean.DataBean.RowsBean item) {
                                    viewHolder.setText(R.id.caseTitleView, item.getDescription())
                                            .setText(R.id.casePlaceView, "发生地点：" + item.getFieldintro())
                                            .setText(R.id.caseTimeView, "上报时间：" + item.getReportTime());
                                }
                            };
                            binding.caseRecyclerView.setAdapter(adapter);
                            adapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<CaseSearchResultBean.DataBean.RowsBean>() {
                                @Override
                                public void onItemClicked(int position, CaseSearchResultBean.DataBean.RowsBean rowsBean) {
                                    ContextKit.navigatePageTo(context, CaseDetailActivity.class, rowsBean.getId());
                                }
                            });
                        }
                    }
                }
            }
        });

    }
}