package com.wall.exquisite.wallpaper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.List;

public class AnimalsImageAdapter extends RecyclerView.Adapter<AnimalsImageAdapter.ImageViewHolder> {

    private final Context mContext;
    private final List<Wallpaper_Item> mWallpaperList;
    private OnItemClickListener mOnItemClickListener;

    // 关键修改：移除Glide的圆角变换，保留尺寸和其他配置
    private final RequestOptions glideOptions = new RequestOptions()
            .centerCrop()
            .placeholder(R.drawable.load_ing)
            .error(R.drawable.load_ing)
            .override(400, 600); // 保持统一尺寸，配合布局2:3比例

    public interface OnItemClickListener {
        void onItemClick(int position, Wallpaper_Item wallpaperItem);
    }

    public AnimalsImageAdapter(Context context, List<Wallpaper_Item> wallpaperList) {
        this.mContext = context;
        this.mWallpaperList = wallpaperList;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView ivItem4d;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            ivItem4d = itemView.findViewById(R.id.iv_item_4d);
        }
    }

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

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        Wallpaper_Item wallpaperItem = mWallpaperList.get(position);

        Glide.with(mContext)
                .load(wallpaperItem.getUrls().getSmall())
                .apply(glideOptions)
                .into(holder.ivItem4d);

        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position, wallpaperItem);
            }
        });
    }

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