package kr.co.vividnext.sodalive.audio_content.all

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
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.main.GetAudioContentRankingItem
import kr.co.vividnext.sodalive.databinding.ItemAudioContentRankingAllBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat

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

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemAudioContentRankingAllBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetAudioContentRankingItem, index: Int) {
            binding.root.setOnClickListener { onItemClick(item.contentId) }
            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(RoundedCornersTransformation(5.3f.dpToPx()))
            }

            binding.tvTitle.text = item.title
            binding.tvRank.text = index.plus(1).toString()
            binding.tvTheme.text = item.themeStr
            binding.tvDuration.text = item.duration
            binding.tvNickname.text = item.creatorNickname

            if (item.price < 1) {
                binding.tvPrice.text = "무료"
                binding.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.white))
                binding.tvPrice.setCompoundDrawables(null, null, null, null)
                binding.tvPrice.setPadding(
                    5.3f.dpToPx().toInt(),
                    2.7f.dpToPx().toInt(),
                    5.3f.dpToPx().toInt(),
                    2.7f.dpToPx().toInt()
                )
                binding.tvPrice.setBackgroundResource(R.drawable.bg_round_corner_2_6_cf5c37)
            } else {
                binding.tvPrice.text = item.price.moneyFormat()
                binding.tvPrice.setTextColor(ContextCompat.getColor(context, R.color.color_909090))
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.ic_can,
                    0,
                    0,
                    0
                )
                binding.tvPrice.setPadding(0, 0, 0, 0)
                binding.tvPrice.setBackgroundResource(0)
            }
        }
    }

    val items = mutableListOf<GetAudioContentRankingItem>()

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

    override fun getItemCount() = items.size

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