package com.adealink.weparty.room.chat.adapter

import android.graphics.Bitmap
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.Fragment
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.frame.image.imageService
import com.adealink.frame.image.listener.IImageLoadResultListener
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.commonui.widget.ColorClickSpan
import com.adealink.weparty.module.gift.GiftModule
import com.adealink.weparty.module.room.chat.data.SendGiftMessage
import com.adealink.weparty.room.R
import com.adealink.weparty.room.chat.listener.IMessageOperaListener
import com.adealink.weparty.room.databinding.ItemMessageSendGiftBinding
import com.adealink.weparty.util.formatNumberStr
import com.adealink.weparty.R as APP_R

/**
 */
class SendGiftMessageViewBinder(
    val listener: IMessageOperaListener,
    val fragment: Fragment,
) : ItemViewBinder<SendGiftMessage, SendGiftMessageViewBinder.ViewHolder>() {

    companion object {
        @JvmStatic
        private val GIFT_ICON_SIZE = 20.dp()
    }

    override fun onBindViewHolder(holder: ViewHolder, item: SendGiftMessage) {
        holder.update(item)
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        return ViewHolder(ItemMessageSendGiftBinding.inflate(inflater, parent, false))
    }

    inner class ViewHolder(binding: ItemMessageSendGiftBinding) :
        BindingViewHolder<ItemMessageSendGiftBinding>(binding) {

        private var message: SendGiftMessage? = null

        fun update(message: SendGiftMessage) {
            this.message = message
            //消息内容
            val sendGiftContent = message.content
            val giftIconUrl = sendGiftContent?.resId?.let {
                GiftModule.getGiftResource(it)?.icon
            }
            if (giftIconUrl.isNullOrEmpty()) {
                return
            }
            imageService.fetchImage(imageService.getResizeUrl(giftIconUrl, GIFT_ICON_SIZE, GIFT_ICON_SIZE), object : IImageLoadResultListener {
                override fun onSuccess(bitmap: Bitmap) {
                    super.onSuccess(bitmap)
                    setSendGiftMsg(message, bitmap)
                }

                override fun onFailed() {
                    super.onFailed()
                    setSendGiftMsg(message, null)
                }
            })
        }

        private fun setSendGiftMsg(message: SendGiftMessage, giftIcon: Bitmap?) {
            binding.msg.movementMethod = LinkMovementMethod.getInstance()

            val sendGiftContent = message.content
            val senderName = sendGiftContent?.getSenderName() ?: ""
            val receiverName = sendGiftContent?.getReceiverName() ?: ""
            val countStr = getCompatString(APP_R.string.common_count, formatNumberStr(sendGiftContent?.quantity ?: 0))

            val text = getCompatString(R.string.room_send_gift_message, senderName, receiverName, countStr)
            binding.msg.text = SpannableStringBuilder(text).apply {
                findAndSetSpan(
                    object : ColorClickSpan(APP_R.color.color_FF35F3F0) {
                        override fun onClick(widget: View) {
                            sendGiftContent?.sender?.let { uid ->
                                listener.onAvatarClick(uid)
                            }
                        }
                    },
                    senderName
                )
                findAndSetSpan(
                    object : ColorClickSpan(APP_R.color.color_FF35F3F0) {
                        override fun onClick(widget: View) {
                            sendGiftContent?.receiver?.let { uid ->
                                listener.onAvatarClick(uid)
                            }
                        }
                    },
                    receiverName
                )
                if (giftIcon != null) {
                    findAndSetSpan(
                        CenterImageSpan(giftIcon.toDrawable(binding.root.resources).apply {
                            setBounds(0, 0, GIFT_ICON_SIZE, GIFT_ICON_SIZE)
                        }),
                        "[gift]"
                    )
                } else {
                    findAndSetSpan(
                        CenterImageSpan(getCompatDrawable(APP_R.drawable.common_fail_img_ic).apply {
                            setBounds(0, 0, GIFT_ICON_SIZE, GIFT_ICON_SIZE)
                        }),
                        "[gift]"
                    )
                }
                findAndSetSpan(
                    object : ColorClickSpan(APP_R.color.color_FF35F3F0) {
                        override fun onClick(widget: View) {
                            sendGiftContent?.receiver?.let { uid ->
                                listener.onAvatarClick(uid)
                            }
                        }
                    },
                    countStr
                )
            }
        }

    }

}