package com.joyful.euphoria.board.recyclerview;

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.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.joyful.euphoria.board.R;
import com.joyful.euphoria.board.beaned.BeanDetails;
import java.util.List;

public class RecyclerviewCategory extends RecyclerView.Adapter<RecyclerviewCategory.ViewHolder> {
    private Context mContext;
    private List<BeanDetails> mKeyboardList;
    private OnCategoryKeyboardClickListener mKeyboardClickListener;

    // 仅保留键盘点击回调接口
    public interface OnCategoryKeyboardClickListener {
        void onKeyboardClick(BeanDetails bean);
    }

    public RecyclerviewCategory(Context context, List<BeanDetails> keyboardList) {
        mContext = context;
        mKeyboardList = keyboardList;
    }

    public void setOnCategoryKeyboardClickListener(OnCategoryKeyboardClickListener listener) {
        mKeyboardClickListener = listener;
    }


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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (mKeyboardList == null || mKeyboardList.isEmpty() || position >= mKeyboardList.size()) {
            return;
        }
        BeanDetails bean = mKeyboardList.get(position);
        if (bean == null) {
            return;
        }

        // 绑定键盘名称
        holder.tvKeyboardName.setText(bean.getTitleName() != null ? bean.getTitleName() : "未知键盘");

        // 加载键盘图片
        Glide.with(mContext)
                .load(bean.getImgPath())
                .placeholder(R.drawable.euphoria_place_holder)
                .error(R.drawable.euphoria_place_holder)
                .into(holder.ivKeyboardImg);

        // 键盘项点击跳转Set界面
        holder.itemView.setOnClickListener(v -> {
            if (mKeyboardClickListener != null) {
                mKeyboardClickListener.onKeyboardClick(bean);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivKeyboardImg;
        TextView tvKeyboardName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivKeyboardImg = itemView.findViewById(R.id.iv_cate_keyboard_img);
            tvKeyboardName = itemView.findViewById(R.id.tv_cate_keyboard_name);
        }
    }
}