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

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
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.loadUrl
import kr.co.vividnext.sodalive.live.GetRoomListResponse

class LiveNowAllAdapter(
    private val itemWidth: Int,
    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) {
            Glide
                .with(context)
                .load(item.coverImageUrl)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundedCorners(14)
                    )
                )
                .into(binding.ivCover)

            val layoutParams = binding.ivCover
                .layoutParams as ConstraintLayout.LayoutParams
            layoutParams.width = itemWidth
            layoutParams.height = itemWidth * 144 / 102

            binding.ivLock.visibility = if (item.isPrivateRoom) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.price > 0) {
                binding.tvPrice.text = "${item.price}"
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.ic_can_white,
                    0,
                    0,
                    0
                )
                binding.tvPrice.setBackgroundResource(R.drawable.bg_round_corner_13_3_dd4500)
            } else {
                binding.tvPrice.text = "무료"
                binding.tvPrice.setCompoundDrawables(null, null, null, null)
                binding.tvPrice.setBackgroundResource(R.drawable.bg_round_corner_13_3_111111)
            }

            if (item.tags.isNotEmpty()) {
                binding.tvTags.visibility = View.VISIBLE
                binding.tvTags.text = item.tags.joinToString(" ") { "#$it" }
            } else {
                binding.tvTags.visibility = View.GONE
            }

            binding.tvTitle.text = item.title
            binding.tvNickname.text = item.creatorNickname
            binding.ivProfile.loadUrl(item.creatorProfileImage) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(CircleCropTransformation())
            }

            if (item.numberOfPeople - item.numberOfParticipate <= 2) {
                binding.llRemainingParticipant.visibility = View.VISIBLE
                if (item.numberOfPeople > item.numberOfParticipate) {
                    binding.tvRemainingParticipantNumber.visibility = View.VISIBLE
                    binding.tvRemainingParticipant.text = "잔여"
                    binding.tvRemainingParticipantNumber.text =
                        "${item.numberOfPeople - item.numberOfParticipate}"
                } else {
                    binding.tvRemainingParticipantNumber.visibility = View.GONE
                    binding.tvRemainingParticipant.text = "Sold out"
                    binding.tvRemainingParticipantNumber.text = ""
                }
            } else {
                binding.llRemainingParticipant.visibility = View.GONE
            }

            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()
    }
}
