package kr.co.vividnext.sodalive.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.databinding.ItemAudioContentBinding
import kr.co.vividnext.sodalive.explorer.profile.GetAudioContentListItem
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

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

    val items = mutableListOf<GetAudioContentListItem>()

    inner class ViewHolder(
        private val binding: ItemAudioContentBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentListItem) {
            binding.ivPin.visibility = if (item.isPin) {
                View.VISIBLE
            } else {
                View.GONE
            }

            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                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()

            binding.tvPrice.visibility = View.GONE
            binding.tvOwned.visibility = View.GONE
            binding.tvRented.visibility = View.GONE
            binding.tvSoldOut.visibility = View.GONE

            binding.tvScheduledToOpen.visibility = if (item.isScheduledToOpen) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.isOwned) {
                binding.tvOwned.visibility = View.VISIBLE
            } else if (item.isRented) {
                binding.tvRented.visibility = View.VISIBLE
            } else if (item.isSoldOut) {
                binding.tvSoldOut.visibility = View.VISIBLE
            } else {
                binding.tvPrice.visibility = View.VISIBLE
                if (item.price < 1) {
                    binding.tvPrice.text = "무료"
                    binding.tvPrice.setCompoundDrawables(null, null, null, null)
                } else {
                    binding.tvPrice.text = item.price.moneyFormat()
                    binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(
                        R.drawable.ic_can,
                        0,
                        0,
                        0
                    )
                }
            }

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

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

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

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