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 NatureImageAdapter extends RecyclerView.Adapter<NatureImageAdapter.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);

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

    public NatureImageAdapter(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 ivItem4k;

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

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_nature_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.ivItem4k);

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

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