package com.app.input.personalised.board.listhelpers;

import android.content.Context;
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.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.app.input.personalised.board.R;
import com.app.input.personalised.board.databean.KeyboardTheme;
import com.app.input.personalised.board.databean.ThemeCategory;
import com.app.input.personalised.board.helpers.AppUtils;
import com.bumptech.glide.Glide;

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

public class CategoryGridAdapter extends RecyclerView.Adapter<CategoryGridAdapter.CategoryCardViewHolder> {

    private Context mContext;
    private List<ThemeCategory> mList = new ArrayList<>();
    private OnCategoryCardClickListener mListener;

    public interface OnCategoryCardClickListener {
        void onCategoryCardClick(ThemeCategory beanWrapper);
    }

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

    public void setOnCategoryCardClickListener(OnCategoryCardClickListener listener) {
        mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull CategoryCardViewHolder holder, int position) {
        ThemeCategory beanWrapper = mList.get(position);
        String categoryName = beanWrapper.getParentName();
        
        holder.tvCategoryName.setText(categoryName != null ? categoryName : "");

        // 显示该分类的第一张图
        List<KeyboardTheme> keyboardList = beanWrapper.getKeyboardList();
        if (keyboardList != null && !keyboardList.isEmpty()) {
            KeyboardTheme firstItem = keyboardList.get(0);
            String thumbGif = firstItem.getThumbGif();
            String thumb = firstItem.getThumbUrl();
            
            if (thumbGif != null && !thumbGif.isEmpty()) {
                AppUtils.INSTANCE.loadWepJif(mContext, thumbGif, holder.ivCategoryImage);
            } else if (thumb != null && !thumb.isEmpty()) {
                Glide.with(mContext)
                        .load(thumb)
                        .placeholder(R.drawable.placeholder)
                        .error(R.drawable.placeholder)
                        .centerCrop()
                        .into(holder.ivCategoryImage);
            } else {
                holder.ivCategoryImage.setImageResource(R.drawable.placeholder);
            }
        } else {
            holder.ivCategoryImage.setImageResource(R.drawable.placeholder);
        }

        holder.cardView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onCategoryCardClick(beanWrapper);
            }
        });
    }

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

    public static class CategoryCardViewHolder extends RecyclerView.ViewHolder {
        CardView cardView;
        ImageView ivCategoryImage;
        TextView tvCategoryName;

        public CategoryCardViewHolder(@NonNull View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.card_view);
            ivCategoryImage = itemView.findViewById(R.id.iv_category_image);
            tvCategoryName = itemView.findViewById(R.id.tv_category_name);
        }
    }
}

