package kr.co.vividnext.sodalive.live.reservation_status

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.ItemLiveReservationStatusBinding
import kr.co.vividnext.sodalive.extensions.dpToPx

class LiveReservationStatusAdapter(
    private val onClickCancel: (GetLiveReservationResponse) -> Unit
) : RecyclerView.Adapter<LiveReservationStatusAdapter.ViewHolder>() {

    val items = mutableListOf<GetLiveReservationResponse>()

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

        fun bind(item: GetLiveReservationResponse) {
            binding.tvDate.text = item.beginDateTime
            binding.tvNickname.text = item.masterNickname
            binding.tvTitle.text = item.title
            binding.ivProfile.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(4.7f.dpToPx()))
            }

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

            if (item.cancelable) {
                binding.tvCancel.setOnClickListener { onClickCancel(item) }
                binding.tvCancel.visibility = View.VISIBLE
            } else {
                binding.tvCancel.visibility = View.GONE
                binding.tvNonCancellable.visibility = View.VISIBLE
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemLiveReservationStatusBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun getItemCount() = items.count()
}
