package kr.co.vividnext.sodalive.audio_content.order

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

class AudioContentOrderListAdapter(
    private val onItemClick: (Long) -> Unit
) : RecyclerView.Adapter<AudioContentOrderListAdapter.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.tvLikeCount.text = item.likeCount.moneyFormat()
            binding.tvCommentCount.text = item.commentCount.moneyFormat()

            if (item.orderType == OrderType.RENTAL) {
                binding.tvRental.visibility = View.VISIBLE
                binding.tvPurchased.visibility = View.GONE
            } else {
                binding.tvPurchased.visibility = View.VISIBLE
                binding.tvRental.visibility = View.GONE
            }

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

    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
}
