package com.adealink.weparty.im.session.adapter.viewbinder

import android.text.SpannableStringBuilder
import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatDrawable
import com.adealink.frame.ext.findAndSetSpan
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.widget.CenterImageSpan
import com.adealink.weparty.im.databinding.LayoutSessionMessageBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageCallingBinding
import com.adealink.weparty.util.formatTimeTo
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener
import com.tencent.qcloud.tuikit.tuichat.bean.message.CallingMessageBean

/**
 * 对照: TipsMessageHolder
 */

class CallingMessageViewHolder(
    rootBinding: LayoutSessionMessageBaseBinding,
    binding: LayoutSessionMessageCallingBinding
) : MessageViewHolder<CallingMessageBean, LayoutSessionMessageCallingBinding>(
    rootBinding,
    binding
) {
    override fun onBindSelfMessage(
        binding: LayoutSessionMessageCallingBinding,
        msg: CallingMessageBean
    ) {
        setCallingResult(binding, msg)
        setMessageTime(binding, msg)
    }

    override fun onBindOtherMessage(
        binding: LayoutSessionMessageCallingBinding,
        msg: CallingMessageBean
    ) {
        setCallingResult(binding, msg)
        setMessageTime(binding, msg)
    }

    private fun setCallingResult(
        binding: LayoutSessionMessageCallingBinding,
        item: CallingMessageBean
    ) {
        binding.tvText.text = SpannableStringBuilder("[icon]  ${item.text}").apply {
            findAndSetSpan(
                CenterImageSpan(getCompatDrawable(com.adealink.weparty.im.R.drawable.im_calling_result_ic).apply {
                    setBounds(0, 0, 30.dp(), 11.dp())
                }),
                "[icon]"
            )
        }
    }

    private fun setMessageTime(
        binding: LayoutSessionMessageCallingBinding,
        item: CallingMessageBean
    ) {
        binding.tvTime.text = if (item.messageTime % 1000L == 0L) {
            formatTimeTo((item.messageTime * 1000), "HH:mm")
        } else {
            formatTimeTo(((item.messageTime + 1000) * 1000), "HH:mm")
        }
    }
}

class CallingMessageViewBinder(
    onItemClickListener: OnItemClickListener
) : BaseMessageViewBinder<CallingMessageBean, LayoutSessionMessageCallingBinding, CallingMessageViewHolder>(
    onItemClickListener
) {

    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): CallingMessageViewHolder {
        return CallingMessageViewHolder(
            rootBinder,
            LayoutSessionMessageCallingBinding.inflate(
                inflater,
                parent,
                true
            )
        )
    }

}