package kr.co.vividnext.sodalive.explorer.profile

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

class UserProfileLiveAdapter(
    private val onClickParticipant: (LiveRoomResponse) -> Unit,
    private val onClickReservation: (LiveRoomResponse) -> Unit
) : RecyclerView.Adapter<UserProfileLiveAdapter.ViewHolder>() {

    val items = mutableListOf<LiveRoomResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemUserProfileLiveBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        @SuppressLint("SetTextI18n")
        fun bind(item: LiveRoomResponse) {
            binding.ivCover.load(item.coverImageUrl) {
                crossfade(true)
                placeholder(R.drawable.ic_place_holder)
                transformations(RoundedCornersTransformation(4.7f.dpToPx()))
            }

            binding.tvDate.text = item.beginDateTime
            binding.tvNickname.text = item.managerNickname
            binding.tvTitle.text = item.title

            if (item.isActive && !item.channelName.isNullOrBlank()) {
                binding.bgCover.visibility = View.GONE
                binding.tvStatus.text = "LIVE"
                binding.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.color_ff5c49))
                binding.tvStatus.setBackgroundResource(
                    R.drawable.bg_round_corner_3_3_transparent_ff5c49
                )

                binding.tvParticipate.text = if (!item.isPaid && item.price > 0) {
                    "${item.price}캔으로 지금 참여하기"
                } else {
                    "지금 참여하기"
                }

                binding.tvParticipate.setOnClickListener { onClickParticipant(item) }

                binding.tvParticipate.setTextColor(
                    ContextCompat.getColor(
                        context,
                        R.color.white
                    )
                )
                binding.tvParticipate.setBackgroundResource(
                    R.drawable.bg_round_corner_5_3_dd4500
                )
            } else if (item.isActive && item.channelName.isNullOrBlank()) {
                binding.bgCover.visibility = View.GONE
                binding.tvStatus.text = "예정"
                binding.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.color_fdca2f))
                binding.tvStatus.setBackgroundResource(
                    R.drawable.bg_round_corner_3_3_transparent_fdca2f
                )

                if (item.isReservation) {
                    binding.tvParticipate.text = "예약완료"
                    binding.tvParticipate.setTextColor(
                        ContextCompat.getColor(context, R.color.color_777777)
                    )
                    binding.tvParticipate.setBackgroundResource(
                        R.drawable.bg_round_corner_5_3_525252
                    )
                } else {
                    binding.tvParticipate.text = if (item.price > 0) {
                        "${item.price}캔으로 예약하기"
                    } else {
                        "예약하기"
                    }

                    binding.tvParticipate.setOnClickListener { onClickReservation(item) }

                    binding.tvParticipate.setTextColor(
                        ContextCompat.getColor(
                            context,
                            R.color.black
                        )
                    )
                    binding.tvParticipate.setBackgroundResource(
                        R.drawable.bg_round_corner_5_3_fdca2f
                    )
                }
            } else {
                binding.bgCover.visibility = View.VISIBLE
                binding.tvStatus.text = "종료"
                binding.tvStatus.setTextColor(ContextCompat.getColor(context, R.color.color_777777))
                binding.tvStatus.setBackgroundResource(
                    R.drawable.bg_round_corner_3_3_transparent_777777
                )

                binding.tvParticipate.text = "다시듣기를 지원하지 않습니다"

                binding.tvParticipate.setTextColor(
                    ContextCompat.getColor(context, R.color.color_777777)
                )
                binding.tvParticipate.setBackgroundResource(
                    R.drawable.bg_round_corner_5_3_525252
                )
            }
        }
    }

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

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

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