package com.sound.prankparty.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
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.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.AirHornActivity;
import com.sound.prankparty.JSON.Category;
import com.sound.prankparty.R;

import java.util.List;

public class SoundRecyclerViewAdapter extends RecyclerView.Adapter<SoundRecyclerViewAdapter.SoundViewHolder> {

    private Context context;
    private List<Category> categoryList;
    private final int[] colors;

    public SoundRecyclerViewAdapter(Context context, List<Category> categoryList) {
        this.context = context;
        this.categoryList = categoryList;

        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 SoundViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sound, parent, false);
        return new SoundViewHolder(view);
    }

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

        Category category = categoryList.get(position);

        // 使用取模运算来循环使用颜色
        holder.imageView.setBackgroundColor(colors[position % colors.length]);
        // 假设 `colors[position % colors.length]` 是你要传递的颜色值
        int currentColor = colors[position % colors.length];

        String originalName = category.getCategoryName();
        // 检查名称是否为空，以避免空指针异常
        if (originalName != null && !originalName.isEmpty()) {
//            // 将首字母大写
//            String capitalizedName = originalName.substring(0, 1).toUpperCase() + originalName.substring(1);
            // 设置文本字体
            holder.textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "FrancoisOne-Regular.ttf"));
            //设置文本
            holder.textView.setText(originalName);
        } else {
            // 如果名称为空，可以设置一个默认值或者不设置
            holder.textView.setText("");
        }

        if (category != null) {

            Glide.with(MainApplication.getContext())
                    //指定加载的资源类型为Drawable
                    .asDrawable()
                    //设置跳过内存缓存，即加载的图片不会存储在内存缓存中
                    .skipMemoryCache(true)
                    //设置磁盘缓存策略为DiskCacheStrategy.ALL，即缓存原始图片和转换后的图片
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .load(category.getCategoryUrl())
                    .transform(new CenterCrop())
                    .into(holder.imageView);

            holder.imageView.setOnClickListener(v -> {
                Intent intent = new Intent(context, AirHornActivity.class);
                intent.putExtra("color", currentColor); // 添加颜色值到 Intent
                intent.putExtra("123",category);
                context.startActivity(intent);
            });
        }

    }

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

    static class SoundViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public SoundViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_imageview_sound);
            textView = itemView.findViewById(R.id.item_textview_sound);
        }
    }


}
