package kr.co.vividnext.sodalive.live.room.donation

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.databinding.ItemLiveRoomDonationMessageBinding
import kr.co.vividnext.sodalive.settings.notification.MemberRole

class LiveRoomDonationMessageAdapter(
    private val creatorId: Long,
    private val onClickDeleteMessage: (String) -> Unit,
    private val copyMessage: (String) -> Unit
) : RecyclerView.Adapter<LiveRoomDonationMessageAdapter.ViewHolder>() {

    private var items = mutableListOf<LiveRoomDonationMessage>()

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

        @SuppressLint("SetTextI18n")
        fun bind(item: LiveRoomDonationMessage) {
            if (item.canMessage.isNotBlank()) {
                binding.tvNickname.text = "${item.nickname}님이"
                binding.tvCanMessage.text = item.canMessage
                binding.tvCanMessage.visibility = View.VISIBLE
                binding.root.setBackgroundResource(
                    if (item.memberId == SharedPreferenceManager.userId) {
                        R.drawable.bg_round_corner_5_3_59548f
                    } else {
                        R.drawable.bg_round_corner_5_3_333333
                    }
                )
            } else {
                binding.tvNickname.text = "${item.nickname}님의 룰렛 결과?"
                binding.tvCanMessage.visibility = View.GONE
                binding.root.setBackgroundResource(R.drawable.bg_round_corner_5_3_ccc25264)
            }
            binding.tvDonationMessage.text = "\"${item.donationMessage}\""

            if (
                creatorId == SharedPreferenceManager.userId &&
                SharedPreferenceManager.role == MemberRole.CREATOR.name
            ) {
                binding.ivDelete.visibility = View.VISIBLE
                binding.ivDelete.setOnClickListener { onClickDeleteMessage(item.uuid) }
            } else {
                binding.ivDelete.visibility = View.GONE
            }

            binding.root.setOnClickListener { copyMessage(item.donationMessage) }
        }
    }

    fun update(items: List<LiveRoomDonationMessage>) {
        items.let {
            val diffCallback = LiveRoomDonationMessageDiffUtilCallback(this.items, items)
            val diffResult = DiffUtil.calculateDiff(diffCallback)

            this.items.run {
                clear()
                addAll(it)
                diffResult.dispatchUpdatesTo(this@LiveRoomDonationMessageAdapter)
            }
        }
    }

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

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

    override fun getItemCount(): Int {
        return items.size
    }
}
