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.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 java.util.List;

public class AirHornRecyclerViewAdapter extends RecyclerView.Adapter<AirHornRecyclerViewAdapter.AirhonrViewHolder> {

    private final Context context;
    private final List<SoundItem> soundItemList;
    private final int color;

    public AirHornRecyclerViewAdapter(Context context, List<SoundItem> soundItemList,int color) {
        this.context = context;
        this.soundItemList = soundItemList;
        this.color = color;
    }

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

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

        SoundItem soundItem = soundItemList.get(position);

        holder.imageView.setBackgroundColor(color);

        String originalName = soundItem.getTitle();
        // 检查名称是否为空，以避免空指针异常
        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("");
        }

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

        holder.imageView.setOnClickListener(v -> {
            Intent intent = new Intent(context, PlaySoundActivity.class);
            intent.putExtra("1234", soundItem);
            context.startActivity(intent);
        });

    }

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

    public static class AirhonrViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;

        public AirhonrViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_imageview_airhorn);
            textView = itemView.findViewById(R.id.item_textview_airhorn);
        }
    }

}
