package com.wall.turing.wallpapers.adapteres;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.bumptech.glide.Glide;
import com.wall.turing.wallpapers.databinding.RecommendAdapterBinding;
import com.wall.turing.wallpapers.myselfbean.Wallpaper;

import java.util.List;

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> {

    private Context context;
    private List<Wallpaper> dataList;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Wallpaper data);
        void onLikeClick(Wallpaper data);
    }

    public RecommendAdapter(Context context, List<Wallpaper> dataList, OnItemClickListener listener) {
        this.context = context;
        this.dataList = dataList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecommendAdapterBinding binding = RecommendAdapterBinding.inflate(
                LayoutInflater.from(context), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Wallpaper data = dataList.get(position);
        
        // 加载图片
        Glide.with(context)
                .load(data.getPreviewUrl400())
                .centerCrop()
                .into(holder.binding.wallpaper);
        
        // 设置分类标签
        holder.binding.tvCategory.setText(data.getCategoryName());
        
        // 设置收藏状态
        holder.binding.imageFavorite.setSelected(data.isLike());
        
        // 点击事件
        holder.binding.layout.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(data);
            }
        });
        
        holder.binding.imageFavorite.setOnClickListener(v -> {
            if (listener != null) {
                listener.onLikeClick(data);
            }
        });
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder {
        RecommendAdapterBinding binding;

        ViewHolder(RecommendAdapterBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

