package com.hi.music.player.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

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

import com.bumptech.glide.Glide; // 确保你添加了 Glide 依赖
import com.hi.music.player.R;
import com.hi.music.player.helper.AudioItemDiffCallback;
import com.hi.music.player.javabean.A_data.AudioItem;

import java.util.ArrayList;
import java.util.List;

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

    private Context context;
    private List<AudioItem> audioItems = new ArrayList<>();

    // 构造函数
    public A_InstrumentAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AudioItem audioItem = audioItems.get(position);

        Log.d("------","--------"+audioItem.getImage());


        // 加载图片，确保不使用 file:// 前缀，并将路径传递给 Glide
        Glide.with(context)
                .load("file:///android_asset/" + audioItem.getImage()) // 正确的路径
                .into(holder.imageView);

        // 设置其他数据（如音频名称、描述等）如果有的话
    }

    @Override
    public int getItemCount() {
        return audioItems.size(); // 返回实际的项目数量
    }

    public void updateData(List<AudioItem> newAudioItems) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AudioItemDiffCallback(this.audioItems, newAudioItems));

        this.audioItems.clear();
        this.audioItems.addAll(newAudioItems);
        diffResult.dispatchUpdatesTo(this); // 只更新发生变化的部分
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.background_image);
        }
    }
}
