package com.app.brush.guitar.ink.gallery;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

import com.app.brush.guitar.ink.R;
import com.app.brush.guitar.ink.drama.DichotomyDetails;
import com.app.brush.guitar.ink.databinding.WideItemCardRecommendedBinding;
import com.app.brush.guitar.ink.eraser.SetKeyboardAmplify;
import com.app.brush.guitar.ink.iguana.UbiquitousSerene;
import com.bumptech.glide.Glide;

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

public class RecommendedCardNurture extends RecyclerView.Adapter<RecommendedCardNurture.RecommendedCardViewHolder> {

    private Context mContext;
    private List<DichotomyDetails> mList = new ArrayList<>();

    public RecommendedCardNurture(Context context, List<DichotomyDetails> list) {
        mContext = context;
        this.mList = list;
    }

    @NonNull
    @Override
    public RecommendedCardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        WideItemCardRecommendedBinding binding = WideItemCardRecommendedBinding.inflate(
            LayoutInflater.from(parent.getContext()), parent, false);
        return new RecommendedCardViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendedCardViewHolder holder, int position) {
        DichotomyDetails beanDetails = mList.get(position);

        String thumbGif = beanDetails.getThumbGif();
        String thumb = beanDetails.getThumbUrl();
        
        // 加载图片
        if (thumbGif != null && !thumbGif.isEmpty()) {
            UbiquitousSerene.INSTANCE.loadWepJif(mContext, thumbGif, holder.binding.imageView);
        } else {
            Glide.with(mContext).load(thumb)
                    .error(R.drawable.place_holder)
                    .placeholder(R.drawable.place_holder)
                    .into(holder.binding.imageView);
        }

        // 设置标签（可以根据需要动态设置）
        String tag = "HOT";
        if (position == 0) {
            tag = "HOT";
        } else if (position == 1) {
            tag = "Editor's Pick";
        } else {
            tag = "Popular Today";
        }
        holder.binding.tvTag.setText(tag);
        holder.binding.layoutTag.setVisibility(View.VISIBLE);

        // 点击事件
        holder.binding.cardRecommended.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentApply = new Intent(mContext, SetKeyboardAmplify.class);
                intentApply.putExtra(SetKeyboardAmplify.SOURCE_KEY, beanDetails);
                intentApply.putExtra(SetKeyboardAmplify.DISPLAY_URL_KEY, beanDetails.getImgPath());
                intentApply.putExtra(SetKeyboardAmplify.ZIP_URL_KEY, beanDetails.getZipPath());
                intentApply.putExtra(SetKeyboardAmplify.NAME_KEY, beanDetails.getTitleName());
                intentApply.putExtra(SetKeyboardAmplify.GIF_KEY, beanDetails.getImgGif());
                String intent_thumb;
                if (thumbGif != null && !thumbGif.isEmpty()) {
                    intent_thumb = thumbGif;
                } else {
                    intent_thumb = thumb;
                }
                intentApply.putExtra(SetKeyboardAmplify.THUMB_KEY, intent_thumb);
                mContext.startActivity(intentApply);
            }
        });
    }

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

    public static class RecommendedCardViewHolder extends RecyclerView.ViewHolder {
        private WideItemCardRecommendedBinding binding;
        
        public RecommendedCardViewHolder(@NonNull WideItemCardRecommendedBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

