package com.joyful.euphoria.board.recyclerview;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
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 RecyclerviewBoardPager extends RecyclerView.Adapter<RecyclerviewBoardPager.ViewHolder> {
    private List<BeanDetails> mBeans;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(BeanDetails bean);
    }

    public RecyclerviewBoardPager(List<BeanDetails> beans) {
        mBeans = beans;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BeanDetails bean = mBeans.get(position);
        Glide.with(holder.itemView.getContext())
                .load(bean.getImgPath())
                .placeholder(R.drawable.euphoria_place_holder)
                .error(R.drawable.euphoria_place_holder)
                .into(holder.ivKeyboard);

        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(bean);
            }
        });
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivKeyboard;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivKeyboard = itemView.findViewById(R.id.iv_keyboard);
        }
    }
}