package kr.co.vividnext.sodalive.audio_content.main

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemAudioContentMainNewContentCreatorBinding

class AudioContentMainNewContentCreatorAdapter(
    private val onClickItem: (Long) -> Unit
) : RecyclerView.Adapter<AudioContentMainNewContentCreatorAdapter.ViewHolder>() {

    private val items = mutableListOf<GetNewContentUploadCreator>()

    inner class ViewHolder(
        private val binding: ItemAudioContentMainNewContentCreatorBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetNewContentUploadCreator) {
            binding.tvNewContentCreator.text = item.creatorNickname
            binding.ivNewContentCreator.load(item.creatorProfileImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_logo)
                transformations(CircleCropTransformation())
            }
            binding.root.setOnClickListener { onClickItem(item.creatorId) }
        }
    }

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

    override fun getItemCount() = items.size

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

    @SuppressLint("NotifyDataSetChanged")
    fun addItems(items: List<GetNewContentUploadCreator>) {
        this.items.clear()
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}
