package kr.co.vividnext.sodalive.message

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemSelectRecipientBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.live.room.detail.GetRoomDetailUser

class SelectMessageRecipientAdapter(
    private val onClickItem: (GetRoomDetailUser) -> Unit
) : RecyclerView.Adapter<SelectMessageRecipientAdapter.ViewHolder>() {
    inner class ViewHolder(
        private val binding: ItemSelectRecipientBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: GetRoomDetailUser) {
            binding.ivProfile.load(item.profileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(23.4f.dpToPx()))
            }

            binding.tvNickname.text = item.nickname

            binding.root.setOnClickListener { onClickItem(item) }
        }
    }

    val items: MutableList<GetRoomDetailUser> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        ItemSelectRecipientBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

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

    override fun getItemCount() = items.size
}
