package com.idle.wall;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.idle.wall.idleitem.ItemIdlePage;
import java.util.List;

public class ItemPageAdapter extends RecyclerView.Adapter<ItemPageAdapter.ViewHolder> {
    private Context context;
    private List<ItemIdlePage> itemList;
    private OnItemClickListener onItemClickListener;
    private RequestManager glideRequestManager;
    private RequestOptions glideOptions;

    // 新增：占位符标记常量（和Fragment中的local_placeholder对应）
    private static final String LOCAL_PLACEHOLDER = "local_placeholder";

    public interface OnItemClickListener {
        void onItemClick(ItemIdlePage item);
    }

    public ItemPageAdapter(Context context, List<ItemIdlePage> itemList, OnItemClickListener listener) {
        this.context = context;
        this.itemList = itemList;
        this.onItemClickListener = listener;

        // 初始化Glide配置
        glideRequestManager = Glide.with(context);
        // 修改：将占位图/错误图替换为 mo_ren
        glideOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .skipMemoryCache(false)
                .placeholder(R.drawable.mo_ren)    // 核心修改：替换为mo_ren
                .error(R.drawable.mo_ren)          // 核心修改：替换为mo_ren
                .dontAnimate();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_idle_page, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        bindViewHolder(holder, position, null);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (!payloads.isEmpty()) {
            // 仅刷新图片
            String imageUrl = itemList.get(position).getImageUrl();
            loadImage(holder.ivImage, imageUrl);
            return;
        }
        // 完整刷新
        bindViewHolder(holder, position, payloads);
    }

    private void bindViewHolder(@NonNull ViewHolder holder, int position, @Nullable List<Object> payloads) {
        ItemIdlePage item = itemList.get(position);
        holder.tvName.setText(item.getName());
        loadImage(holder.ivImage, item.getImageUrl());
        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(item);
            }
        });
    }

    /**
     * 核心修改：优化图片加载逻辑，识别local_placeholder并加载mo_ren
     */
    private void loadImage(ImageView imageView, String url) {
        // 判断是否是本地占位符标记
        if (LOCAL_PLACEHOLDER.equals(url)) {
            // 直接加载本地mo_ren资源，不走Glide网络加载
            imageView.setImageResource(R.drawable.mo_ren);
        } else {
            glideRequestManager
                    .load(url)
                    .apply(glideOptions)
                    .into(imageView);
        }
    }

    @Override
    public int getItemCount() {
        return itemList != null ? itemList.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImage;
        TextView tvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_category); // 确认布局ID正确
            tvName = itemView.findViewById(R.id.tv_category_name); // 确认布局ID正确
        }
    }
}