package com.cygnusa.hyperm.training.chat;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.siyamed.shapeimageview.CircularImageView;
import com.cygnusa.hyperm.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> {

    private List<Chat> CHATS;
    private LayoutInflater inflater;
    private Context mContext;

    public MessageAdapter(Context context, List<Chat> chatList) {
        inflater = ((Activity) context).getLayoutInflater();
        this.CHATS = chatList;
        mContext = context;
    }

    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.msg_item_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NotNull MyViewHolder holder, int position) {
        setupView(holder, position);
    }

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


    private void setupView(MyViewHolder holder, int position) {
        Chat bean = CHATS.get(position);
        holder.userName.setText(bean.getFullName());
        holder.message.setText(bean.getMessage());
        Glide.with(mContext).load(bean.getProfile())
                .thumbnail(Glide.with(mContext).load(R.drawable.gif_loader))
                .into(holder.userIcon);
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        private CircularImageView userIcon;
        private TextView userName;
        private TextView message;

        MyViewHolder(View itemView) {
            super(itemView);
            userIcon = itemView.findViewById(R.id.userIcon);
            userName = itemView.findViewById(R.id.userName);
            message = itemView.findViewById(R.id.message);
        }
    }
}
