package com.sound.prankparty.Adapter;

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.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sound.prankparty.JSON.SoundItem;
import com.sound.prankparty.R;

import java.util.List;

public class SubSoundItemRecyclerViewAdapter extends RecyclerView.Adapter<SubSoundItemRecyclerViewAdapter.SoundItemViewHolder> {

    private Context context;
    private List<SoundItem> soundItemList;
    private String categoryName;  // 传递CategoryName

    public SubSoundItemRecyclerViewAdapter(Context context, List<SoundItem> soundItemList, String categoryName) {
        this.context = context;
        this.soundItemList = soundItemList;
        this.categoryName = categoryName;
    }

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

    @Override
    public void onBindViewHolder(@NonNull SoundItemViewHolder holder, int position) {
        SoundItem soundItem = soundItemList.get(position);

        // 设置SoundItem的图片和标题
        holder.soundTitle.setText(soundItem.getTitle());
        holder.categoryName.setText(categoryName);  // 显示Category的名字

        Glide.with(context)
                .load(soundItem.getPreUrl())
                .into(holder.imageView);
    }

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

    static class SoundItemViewHolder extends RecyclerView.ViewHolder {

        TextView soundTitle;
        TextView categoryName;
        ImageView imageView;

        public SoundItemViewHolder(@NonNull View itemView) {
            super(itemView);
            soundTitle = itemView.findViewById(R.id.sound_title);
            categoryName = itemView.findViewById(R.id.category_name);
            imageView = itemView.findViewById(R.id.sound_image);
        }
    }
}
