package com.offline.music.player.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
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.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.offline.music.player.R;
import com.offline.music.player.helper.AudioItemDiffCallback;
import com.offline.music.player.javabean.A_data.AudioItem;
import com.offline.music.player.max.MaxManager;
import com.offline.music.player.max.onAdAfterAction;
import com.offline.music.player.ui.activity.A_PlayActivity;

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

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

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

    public A_WhiteAdapter(Activity context) {
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_home_2, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        AudioItem audioItem = audioItems.get(position);

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

        holder.textView1.setText(audioItem.getName());
        holder.textView2.setText(audioItem.getName());

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

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaxManager.startShowMaxAd(context, new onAdAfterAction() {
                    @Override
                    public void onAction() {
                        Intent intent = new Intent(context, A_PlayActivity.class);
                        intent.putExtra("Path", audioItem);
                        context.startActivity(intent);
                    }
                });
            }
        });

    }

    @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 {

       ImageView imageView;
       TextView textView1;
       TextView textView2;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image);
            textView1 = itemView.findViewById(R.id.title);
            textView2 = itemView.findViewById(R.id.time);

        }

    }

}
