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 RecyclerviewDialogCollect extends RecyclerView.Adapter<RecyclerviewDialogCollect.ViewHolder> {
    private Context mContext;
    private List<BeanDetails> mKeyboardList;
    private OnKeyboardClickListener mListener;

    public interface OnKeyboardClickListener {
        void onKeyboardClick(BeanDetails bean); // 跳转Download
    }

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

    public void setOnKeyboardClickListener(OnKeyboardClickListener listener) {
        mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BeanDetails bean = mKeyboardList.get(position);
        // 绑定键盘名称
        holder.tvName.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.ivImg);
        // 点击跳转Download
        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onKeyboardClick(bean);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivImg;
        TextView tvName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImg = itemView.findViewById(R.id.iv_dialog_keyboard_img);
            tvName = itemView.findViewById(R.id.tv_dialog_keyboard_name);
        }
    }
}