package chatbot

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.archemistrylab.R
import kotlinx.android.synthetic.main.listitem_chat.view.*


class AdapterChatBot : RecyclerView.Adapter<AdapterChatBot.MyViewHolder>() {
    private val list = ArrayList<ChatModel>()

    inner class MyViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.listitem_chat, parent, false)


    ) {
        fun bind(chat: ChatModel) = with(itemView) {
            if(!chat.isBot) {
                txtChat.text = chat.chat
                val textView1 = findViewById(R.id.txtChat) as TextView
                val textView2 = findViewById(R.id.user_message) as TextView
                textView1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_man,0,0,0)
                textView2.setVisibility(View.GONE)
            }else{
                val textView1 = findViewById(R.id.txtChat) as TextView
                val textView2 = findViewById(R.id.user_message) as TextView
                textView2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bot,0,0,0)
                textView1.setVisibility(View.GONE)
                user_message.text = chat.chat
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MyViewHolder(parent)

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(list[position])
    }

    override fun getItemCount() = list.size

    fun addChatToList(chat: ChatModel) {
        list.add(chat)
        notifyDataSetChanged()
    }

}