package kr.co.vividnext.sodalive.mypage.alarm.select_audio_content

import android.view.LayoutInflater
import android.view.View
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.audio_content.order.GetAudioContentOrderListItem
import kr.co.vividnext.sodalive.audio_content.order.OrderType
import kr.co.vividnext.sodalive.databinding.ItemAudioContentOrderListBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

class AlarmSelectAudioContentAdapter(
    private val onItemClick: (GetAudioContentOrderListItem) -> Unit
) : RecyclerView.Adapter<AlarmSelectAudioContentAdapter.ViewHolder>() {

    var items = mutableSetOf<GetAudioContentOrderListItem>()

    inner class ViewHolder(
        private val binding: ItemAudioContentOrderListBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentOrderListItem) {
            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(5.3f.dpToPx()))
            }

            binding.tvTitle.text = item.title
            binding.tvTheme.text = item.themeStr
            binding.tvDuration.text = item.duration
            binding.tvCreatorNickname.text = item.creatorNickname
            binding.tvLikeCount.text = item.likeCount.moneyFormat()
            binding.tvCommentCount.text = item.commentCount.moneyFormat()

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

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

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

    override fun getItemCount() = items.size
}
