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

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.im.R
import com.adealink.weparty.im.databinding.LayoutSessionMessageBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageTextBinding
import com.adealink.weparty.util.formatTimeTo
import com.tencent.qcloud.tuikit.timcommon.component.face.FaceManager
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener
import com.tencent.qcloud.tuikit.timcommon.util.TextUtil
import com.tencent.qcloud.tuikit.tuichat.bean.message.TextMessageBean

/**
 * 对照: TextMessageHolder
 */
class TextMessageViewHolder(
    rootBinding: LayoutSessionMessageBaseBinding,
    binding: LayoutSessionMessageTextBinding
) : MessageViewHolder<TextMessageBean, LayoutSessionMessageTextBinding>(rootBinding, binding) {

    override fun initView(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean,
        onItemClickListener: OnItemClickListener?
    ) {
        binding.root.isClickable = true
        binding.root.setOnLongClickListener {
            onItemClickListener?.onMessageLongClick(binding.root, msg)
            true
        }
        binding.tvText.isClickable = true
        binding.tvText.setOnLongClickListener {
            onItemClickListener?.onMessageLongClick(binding.root, msg)
            true
        }
        binding.tvText.onClick {
            onItemClickListener?.onMessageClick(binding.root, msg)
        }
    }

    override fun onBindSelfMessage(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean
    ) {
        setText(binding, msg)
        setMessageTime(binding, msg)
        setReadStatus(binding, msg, true)
    }

    override fun onBindOtherMessage(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean
    ) {
        setText(binding, msg)
        setMessageTime(binding, msg)
        setReadStatus(binding, msg, false)
    }

    private fun setText(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean
    ) {
        if (msg.text != null) {
            FaceManager.handlerEmojiText(
                binding.tvText,
                msg.text,
                false
            )
        } else if (!msg.extra.isNullOrEmpty()) {
            FaceManager.handlerEmojiText(
                binding.tvText,
                msg.extra,
                false
            )
        } else {
            FaceManager.handlerEmojiText(
                binding.tvText,
                getCompatString(R.string.im_unsupported_message),
                false
            )
        }
    }

    private fun setMessageTime(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean
    ) {
        binding.tvTime.text = formatTimeTo((msg.messageTime * 1000), "HH:mm")
    }

    private fun setReadStatus(
        binding: LayoutSessionMessageTextBinding,
        msg: TextMessageBean,
        isSelf: Boolean
    ) {
        if (!isSelf) {
            binding.ivReadStatus.gone()
            return
        }
        binding.ivReadStatus.show()
        if (msg.isPeerRead) {
            binding.ivReadStatus.setImageResource(R.drawable.im_session_message_read_ic)
        } else {
            binding.ivReadStatus.setImageResource(R.drawable.im_session_message_unread_ic)
        }
    }

}

class TextMessageViewBinder(
    onItemClickListener: OnItemClickListener
) : BaseMessageViewBinder<TextMessageBean, LayoutSessionMessageTextBinding, TextMessageViewHolder>(
    onItemClickListener
) {

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

}