package kr.co.vividnext.sodalive.live.room.detail

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.ItemLiveRoomDetailUserBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveRoomDetailAdapter(
    private val onClick: (GetRoomDetailUser) -> Unit
) : RecyclerView.Adapter<LiveRoomDetailAdapter.ViewHolder>() {

    val items = mutableListOf<GetRoomDetailUser>()

    inner class ViewHolder(
        private val binding: ItemLiveRoomDetailUserBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetRoomDetailUser) {
            binding.tvNickname.text = item.nickname
            binding.ivProfile.load(item.profileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(23.4f.dpToPx()))
            }
            binding.root.setOnClickListener { onClick(item) }
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(
            ItemLiveRoomDetailUserBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun getItemCount() = items.count()
}
