package com.casic.dcms.fragment.handle;

import android.content.Intent;
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.FragmentHandleCaseBinding;
import com.casic.dcms.model.CaseHandleBean;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.view.handle.CaseHandleDetailActivity;
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.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 CaseTodoFragment extends AndroidxBaseFragment<FragmentHandleCaseBinding> {

    private static final String TAG = "CaseTodoFragment";
    private CaseViewModel caseViewModel;
    private NormalRecyclerAdapter<CaseHandleBean.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.caseHandleModel.observe(this, new Observer<CaseHandleBean>() {
            @Override
            public void onChanged(CaseHandleBean resultBean) {
                if (resultBean.getCode() == 200) {
                    bindRecyclerView(resultBean);
                }
            }
        });
        getHandleCaseList();
    }

    private void bindRecyclerView(CaseHandleBean resultBean) {
        List<CaseHandleBean.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<CaseHandleBean.DataModel.RowsModel>(R.layout.item_case_rv_l, dataRows) {
                    @Override
                    public void convertView(ViewHolder viewHolder, int position, CaseHandleBean.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().caseRecyclerView.setAdapter(adapter);
                adapter.setOnItemClickedListener(new NormalRecyclerAdapter.OnItemClickedListener<CaseHandleBean.DataModel.RowsModel>() {
                    @Override
                    public void onItemClicked(int position, CaseHandleBean.DataModel.RowsModel rowsModel) {
                        Intent intent = new Intent(requireContext(), CaseHandleDetailActivity.class);
                        intent.putExtra("title", "待办案卷");
                        intent.putExtra("id", rowsModel.getId());
                        startActivity(intent);
                    }
                });
            }
        }
    }

    private void getHandleCaseList() {
        caseViewModel.getHandleCaseList(requireContext(), page, false);
    }

    @Override
    protected void initEvent() {
        getBinding().refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                getHandleCaseList();
            }
        });
        getBinding().refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                getHandleCaseList();
            }
        });
    }
}
