package kr.co.vividnext.sodalive.live.now.all

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
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.databinding.ItemLiveNowAllBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.live.GetRoomListResponse

class LiveNowAllAdapter(
    private val onClick: (GetRoomListResponse) -> Unit
) : RecyclerView.Adapter<LiveNowAllAdapter.ViewHolder>() {

    var items = mutableListOf<GetRoomListResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemLiveNowAllBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(item: GetRoomListResponse) {
            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.bg_placeholder)
                transformations(RoundedCornersTransformation(4.7f.dpToPx()))
            }
            binding.tvNickname.text = item.creatorNickname
            binding.tvTitle.text = item.title
            binding.tvTotal.text = "/${item.numberOfPeople}"
            binding.tvNumberOfParticipants.text = item.numberOfParticipate.toString()
            binding.ivLock.visibility = if (item.isPrivateRoom) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.numberOfPeople > item.numberOfParticipate) {
                binding.tvAvailableParticipate.text = "참여가능"
                binding.tvAvailableParticipate.setTextColor(
                    ContextCompat.getColor(
                        context,
                        R.color.color_9970ff
                    )
                )
            } else {
                binding.tvAvailableParticipate.text = "Sold out"
                binding.tvAvailableParticipate.setTextColor(
                    ContextCompat.getColor(
                        context,
                        R.color.color_ffd300
                    )
                )
            }

            if (item.price < 1) {
                binding.tvPrice.text = "무료"
                binding.tvPrice.setCompoundDrawables(null, null, null, null)
            } else {
                binding.tvPrice.text = item.price.moneyFormat()
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(
                    0,
                    0,
                    R.drawable.ic_can,
                    0
                )
            }

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

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

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

    override fun getItemCount() = items.count()

    fun clear() {
        items.clear()
    }
}
