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

import android.annotation.SuppressLint
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.ItemLiveNowBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.live.GetRoomListResponse

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

    var items = mutableListOf<GetRoomListResponse>()

    inner class ViewHolder(
        private val binding: ItemLiveNowBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: GetRoomListResponse) {
            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_logo)
                transformations(RoundedCornersTransformation(4.7f.dpToPx()))
            }
            binding.tvManager.text = item.managerNickname
            binding.tvNumberOfMembers.text = "${item.numberOfParticipate}"
            binding.ivLock.visibility = if (item.isPrivateRoom) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.price > 0) {
                binding.tvPrice.text = "유료"
                binding.tvPrice.setBackgroundResource(R.drawable.bg_round_corner_10_881609)
            } else {
                binding.tvPrice.text = "무료"
                binding.tvPrice.setBackgroundResource(R.drawable.bg_round_corner_10_643bc8)
            }

            binding.iv19.visibility = if (item.isAdult) {
                View.VISIBLE
            } else {
                View.GONE
            }

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

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

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

    override fun getItemCount() = items.count()

    @SuppressLint("NotifyDataSetChanged")
    fun clear() {
        items.clear()
        notifyDataSetChanged()
    }
}
