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

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

    val items = mutableListOf<OtherContentResponse>()

    inner class ViewHolder(
        private val binding: ItemAudioOtherContentBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: OtherContentResponse) {
            binding.ivCover.load(item.coverUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(RoundedCornersTransformation(2.7f.dpToPx()))
            }

            binding.tvTitle.text = item.title
            binding.root.setOnClickListener { onClickItem(item.contentId) }
        }
    }

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

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

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