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

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bokecc.livemodule.R;
import com.bokecc.sdk.mobile.live.pojo.CustomEmoji;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

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

    private final ArrayList<CustomEmoji> customEmojis;
    private OnClickListener onClickListener;

    public CustomEmojiAdapter(ArrayList<CustomEmoji> customEmojis) {
        this.customEmojis = customEmojis;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.bindData(customEmojis.get(i), i);
    }

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

    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 CustomEmoji customEmoji, final int i) {
            Glide.with(itemView.getContext()).load(customEmoji.getImg()).error(R.drawable.ic_emoji_fail).into(iv_emoji);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onClickListener != null) {
                        onClickListener.onItemClick(customEmoji.getName(), i);
                    }
                }
            });
        }
    }

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

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