package kr.co.vividnext.sodalive.live.reservation

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.recyclerview.widget.RecyclerView
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.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ItemLiveReservationBinding
import kr.co.vividnext.sodalive.databinding.ItemMyLiveReservationBinding
import kr.co.vividnext.sodalive.extensions.dpToPx
import kr.co.vividnext.sodalive.extensions.moneyFormat
import kr.co.vividnext.sodalive.live.GetRoomListResponse

class LiveReservationAdapter(
    private val isMain: Boolean = false,
    private val onClick: (GetRoomListResponse) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    var items = mutableListOf<GetRoomListResponse>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == 1) {
            MyLiveViewHolder(
                parent.context,
                ItemMyLiveReservationBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        } else {
            ViewHolder(
                parent.context,
                ItemLiveReservationBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = items[position]
        if (isMyLive(item)) {
            (holder as MyLiveViewHolder).bind(item, position)
        } else {
            (holder as ViewHolder).bind(item)
        }
    }

    override fun getItemCount() = items.count()

    override fun getItemViewType(position: Int): Int {
        return if (isMyLive(items[position])) {
            1
        } else {
            0
        }
    }

    private fun isMyLive(item: GetRoomListResponse) =
        item.creatorId == SharedPreferenceManager.userId && isMain

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

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemLiveReservationBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(item: GetRoomListResponse) {
            val lp = binding.ivCover.layoutParams
            lp.width = 80f.dpToPx().toInt()
            lp.height = 116.7f.dpToPx().toInt()

            Glide
                .with(context)
                .asBitmap()
                .transform(CenterCrop(), RoundedCorners(16f.dpToPx().toInt()))
                .load(item.coverImageUrl)
                .into(object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        binding.ivCover.setImageBitmap(resource)
                        binding.ivCover.layoutParams = lp
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                    }
                })
            binding.tvDate.text = item.beginDateTime
            binding.tvNickname.text = item.creatorNickname
            binding.tvTitle.text = item.title
            binding.root.setOnClickListener { onClick(item) }
            binding.ivLock.visibility = if (item.isPrivateRoom) {
                View.VISIBLE
            } else {
                View.GONE
            }

            if (item.isReservation) {
                binding.tvPrice.visibility = View.GONE
                binding.tvCompleteReservation.visibility = View.VISIBLE
            } else {
                binding.tvPrice.visibility = View.VISIBLE
                binding.tvCompleteReservation.visibility = View.GONE

                binding.tvPrice.text = if (item.price <= 0) {
                    "무료"
                } else {
                    "${item.price.moneyFormat()}캔"
                }
            }
        }
    }

    inner class MyLiveViewHolder(
        private val context: Context,
        private val binding: ItemMyLiveReservationBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: GetRoomListResponse, position: Int) {
            binding.tvMyLive.visibility = if (position == 0) {
                View.VISIBLE
            } else {
                View.GONE
            }

            val lp = binding.ivCover.layoutParams
            lp.width = 80f.dpToPx().toInt()
            lp.height = 116.7f.dpToPx().toInt()

            Glide
                .with(context)
                .asBitmap()
                .load(item.coverImageUrl)
                .transform(CenterCrop(), RoundedCorners(16f.dpToPx().toInt()))
                .into(object : CustomTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        binding.ivCover.setImageBitmap(resource)
                        binding.ivCover.layoutParams = lp
                    }

                    override fun onLoadCleared(placeholder: Drawable?) {
                    }
                })
            binding.tvDate.text = item.beginDateTime
            binding.tvNickname.text = item.creatorNickname
            binding.tvTitle.text = item.title
            binding.root.setOnClickListener { onClick(item) }

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