package com.bokecc.livemodule.live.chat.adapter;

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.bokecc.dwlivedemo.R;
import com.bumptech.glide.Glide;

public class DefaultEmojiAdapter extends RecyclerView.Adapter<DefaultEmojiAdapter.ViewHolder> {

    private final Integer[] images;
    private DefaultEmojiAdapter.OnClickListener onClickListener;

    public DefaultEmojiAdapter(Integer[] images) {
        this.images = images;
    }


    @NonNull
    @Override
    public DefaultEmojiAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new DefaultEmojiAdapter.ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_emoji_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull DefaultEmojiAdapter.ViewHolder viewHolder, int i) {
        viewHolder.bindData(images[i], i);
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView iv_emoji;

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

        private void bindData(final int img, final int i) {
            Glide.with(itemView.getContext()).load(img).into(iv_emoji);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null) {
                        onClickListener.onItemClick(i);
                    }
                }
            });
        }
    }

    public void setOnClickListener(DefaultEmojiAdapter.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {
        void onItemClick(int position);
    }
}