package com.casic.dcms.fragment.check;

import android.os.Bundle;
import android.text.TextUtils;

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.FragmentCheckCaseBinding;
import com.casic.dcms.model.CaseCheckModel;
import com.casic.dcms.utils.LocaleConstant;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.view.check.CheckDetailActivity;
import com.casic.dcms.vm.CaseViewModel;
import com.pengxh.androidx.lite.adapter.ViewHolder;
import com.pengxh.androidx.lite.base.AndroidxBaseFragment;
import com.pengxh.androidx.lite.kit.ContextKit;
import com.pengxh.androidx.lite.kit.StringKit;
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 CaseCheckFragment extends AndroidxBaseFragment<FragmentCheckCaseBinding> {

    private static final String TAG = "CaseCheckFragment";
    private CaseViewModel caseViewModel;
    private NormalRecyclerAdapter<CaseCheckModel.DataModel.RowsModel> adapter;
    private int page = 1;
    private boolean isRefresh, isLoadMore = false;

    @Override
    protected void setupTopBarLayout() {

    }

    @Override
    protected void observeRequestState() {

    }

    @Override
    protected void initOnCreate(@Nullable Bundle savedInstanceState) {
        caseViewModel = new ViewModelProvider(this).get(CaseViewModel.class);
        caseViewModel.caseCheckModel.observe(this, new Observer<CaseCheckModel>() {
            @Override
            public void onChanged(CaseCheckModel resultBean) {
                if (resultBean.isSuccess()) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        getCheckCaseList();
    }

    private void bindRecyclerView(CaseCheckModel resultBean) {
        List<CaseCheckModel.DataModel.RowsModel> dataRows = resultBean.getData().getRows();
        if (isRefresh) {
            getBinding().refreshLayout.finishRefresh();
            isRefresh = false;
            adapter.refresh(dataRows);
        } else if (isLoadMore) {
            getBinding().refreshLayout.finishLoadMore();
            isLoadMore = false;
            if (dataRows.size() == 0) {
                StringKit.show(requireContext(), "到底了，别拉了");
            }
            adapter.loadMore(dataRows);
        } else {
            if (dataRows.size() == 0) {
                getBinding().emptyView.show(null, "您暂时没有待办案卷");
            } else {
                getBinding().emptyView.hide();
                adapter = new NormalRecyclerAdapter<CaseCheckModel.DataModel.RowsModel>(R.layout.item_case_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, CaseCheckModel.DataModel.RowsModel item) {
                        String fileIdVerify = item.getFileIdVerify();
                        if (!TextUtils.isEmpty(fileIdVerify)) {
                            String[] split = fileIdVerify.split(",");
                            String firstImage = split[0];
                            if (firstImage.endsWith(".jpg") || firstImage.contains(".jpeg") || firstImage.contains(".png")) {
                                viewHolder.setImageResource(R.id.caseImageView, StringHelper.appendCompleteURL(firstImage));
                            }
                        }
                        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<CaseCheckModel.DataModel.RowsModel>() {
                    @Override
                    public void onItemClicked(int position, CaseCheckModel.DataModel.RowsModel rowsModel) {
                        ContextKit.navigatePageTo(requireContext(), CheckDetailActivity.class, rowsModel.getId());
                    }
                });
            }
        }
    }

    private void getCheckCaseList() {
        caseViewModel.getCheckCaseList(requireContext(), LocaleConstant.PAGE_LIMIT, page, false);
    }

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