package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.model.SanBaoCaseBean;
import com.casic.dcms.utils.StringHelper;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

public class SanBaoCaseAdapter extends RecyclerView.Adapter<SanBaoCaseAdapter.ItemViewHolder> {

    private static final String TAG = "SanBaoCaseAdapter";
    private final Context context;
    private final List<SanBaoCaseBean.DataBean.RowsBean> dataRows;
    private final LayoutInflater layoutInflater;

    public SanBaoCaseAdapter(Context context, List<SanBaoCaseBean.DataBean.RowsBean> dataRows) {
        this.context = context;
        this.dataRows = dataRows;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(layoutInflater.inflate(R.layout.item_sanbao_rv_l, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindView(dataRows.get(position));
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return dataRows.size();
    }

    public void refresh(List<SanBaoCaseBean.DataBean.RowsBean> newRows) {
        DiffUtil.Callback diffCallback = new DiffUtil.Callback() {

            @Override
            public int getOldListSize() {
                return dataRows.size();
            }

            @Override
            public int getNewListSize() {
                return newRows.size();
            }

            @Override
            public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                if (dataRows == null || newRows == null) {
                    return false;
                }

                // 检查索引是否在有效范围内
                if (oldItemPosition < 0 || oldItemPosition >= dataRows.size() ||
                        newItemPosition < 0 || newItemPosition >= newRows.size()) {
                    return false;
                }

                // 获取元素并进行比较
                Object oldItem = dataRows.get(oldItemPosition);
                Object newItem = newRows.get(newItemPosition);

                // 处理空指针情况
                if (oldItem == null && newItem == null) {
                    return true;
                }
                if (oldItem == null || newItem == null) {
                    return false;
                }

                return oldItem.equals(newItem);
            }

            @Override
            public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                return dataRows.get(oldItemPosition) == newRows.get(newItemPosition);
            }
        };

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);

        dataRows.clear();
        dataRows.addAll(newRows);

        diffResult.dispatchUpdatesTo(this);
    }

    public void loadMore(List<SanBaoCaseBean.DataBean.RowsBean> newRows) {
        if (newRows.isEmpty()) {
            return;
        }
        int startPosition = this.dataRows.size();
        this.dataRows.addAll(newRows);
        notifyItemRangeInserted(startPosition, newRows.size());
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private final QMUIRadiusImageView caseImageView;
        private final TextView caseTitleView;
        private final TextView caseDateView;
        private final RecyclerView casePunishView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            caseImageView = itemView.findViewById(R.id.caseImageView);
            caseTitleView = itemView.findViewById(R.id.caseTitleView);
            caseDateView = itemView.findViewById(R.id.caseDateView);
            casePunishView = itemView.findViewById(R.id.casePunishView);
        }

        void bindView(SanBaoCaseBean.DataBean.RowsBean rowsBean) {
            //三包核实图片
            String fileIdVerify = rowsBean.getFileIdVerify();
            if (!TextUtils.isEmpty(fileIdVerify)) {
                String[] split = fileIdVerify.split(",");
                String firstImage = split[0];
                if (firstImage.endsWith(".jpg") || firstImage.contains(".jpeg") || firstImage.contains(".png")) {
                    Glide.with(context).load(StringHelper.appendCompleteURL(firstImage)).into(caseImageView);
                }
            }
            caseTitleView.setText(rowsBean.getDescription());
            caseDateView.setText("上报时间：" + rowsBean.getReportTime());
        }
    }

    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.clickListener = onItemClickListener;
    }
}
