package kr.co.vividnext.sodalive.audio_content.playlist.detail

import android.annotation.SuppressLint
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.ItemPlaylistContentBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class AudioContentPlaylistDetailAdapter :
    RecyclerView.Adapter<AudioContentPlaylistDetailAdapter.ViewHolder>() {

    private val items = mutableListOf<AudioContentPlaylistContent>()

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

            binding.tvTitle.text = item.title
            binding.tvTheme.text = item.category
            binding.tvDuration.text = item.duration
            binding.tvCreatorNickname.text = item.creatorNickname
        }
    }

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

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

    override fun getItemCount() = items.count()

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