package com.sound.prankparty.Adapter;

import android.content.Context;
import android.content.Intent;
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.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.sound.MainApplication;
import com.sound.prankparty.Activity.PlaySoundActivity;
import com.sound.prankparty.JSON.SoundItem;
import com.sound.prankparty.R;
import com.sound.prankparty.Room.FavoriteSounds;
import com.sound.prankparty.Utils.FavoriteSoundsDiffCallback;

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

public class FavoriteRecyclerViewAdapter extends RecyclerView.Adapter<FavoriteRecyclerViewAdapter.FavoriteRecyclerViewHolder> {

    private List<FavoriteSounds> favoriteSoundsList = new ArrayList<>();
    private Context context;
    private Fragment fragment;
    private final int[] colors;

    public FavoriteRecyclerViewAdapter(Context context, Fragment fragment) {
        this.context = context;
        this.fragment = fragment;
        colors = new int[]{
                ContextCompat.getColor(context, R.color.color2),
                ContextCompat.getColor(context, R.color.color3),
                ContextCompat.getColor(context, R.color.color4),
                ContextCompat.getColor(context, R.color.color5),
                ContextCompat.getColor(context, R.color.color6),
                ContextCompat.getColor(context, R.color.color7)
                // 添加更多颜色
        };
    }

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

    @Override
    public void onBindViewHolder(@NonNull FavoriteRecyclerViewHolder holder, int position) {
        FavoriteSounds favoriteSounds = favoriteSoundsList.get(position);
        holder.textView.setText(favoriteSounds.getName());
        // 使用取模运算来循环使用颜色
        holder.imageView.setBackgroundColor(colors[position % colors.length]);

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建 SoundItem 对象并传递你想存入的值
                SoundItem soundItem = new SoundItem(favoriteSounds.getPath(), favoriteSounds.getImagePath(), favoriteSounds.getName());

                Intent intent = new Intent(fragment.getContext(), PlaySoundActivity.class);
                intent.putExtra("1234", soundItem);
                fragment.startActivity(intent);
            }
        });

        Glide.with(MainApplication.getContext())
                .asDrawable()
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .load(favoriteSounds.getImagePath())
                .placeholder(R.mipmap.default_image)
                .error(R.mipmap.default_image)
                .transform(new CenterCrop())
                .into(holder.imageView);
    }

    public void updateData(List<FavoriteSounds> newFavoriteSoundsList) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new FavoriteSoundsDiffCallback(favoriteSoundsList, newFavoriteSoundsList));
        favoriteSoundsList.clear();
        favoriteSoundsList.addAll(newFavoriteSoundsList);
        diffResult.dispatchUpdatesTo(this);
    }

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

    public static class FavoriteRecyclerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public FavoriteRecyclerViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_imageview_favorite);
            textView = itemView.findViewById(R.id.item_imageview_text);
        }
    }
}

