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

import android.view.LayoutInflater
import android.view.ViewGroup
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.im.session.mesasge.PlaymateSayWelcomeTextMessageBean
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

/**
 * 陪玩打招呼消息-文本消息
 */
class PlaymateSayWelcomeTextMessageViewHolder(
    rootBinding: LayoutSessionMessageBaseBinding,
    binding: LayoutSessionMessageTextBinding
) : MessageViewHolder<PlaymateSayWelcomeTextMessageBean, LayoutSessionMessageTextBinding>(
    rootBinding,
    binding
) {
    override fun initView(
        binding: LayoutSessionMessageTextBinding,
        msg: PlaymateSayWelcomeTextMessageBean,
        onItemClickListener: OnItemClickListener?
    ) {
        binding.tvText.onClick {
            onItemClickListener?.onMessageClick(binding.root, msg)
        }
    }

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

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

    private fun setText(
        binding: LayoutSessionMessageTextBinding,
        msg: PlaymateSayWelcomeTextMessageBean
    ) {
        val msgText = msg.data?.text
        FaceManager.handlerEmojiText(
            binding.tvText,
            msgText ?: "",
            false
        )
    }

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

    private fun setReadStatus(
        binding: LayoutSessionMessageTextBinding,
        msg: PlaymateSayWelcomeTextMessageBean,
        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 PlaymateSayWelcomeTextMessageViewBinder(
    onItemClickListener: OnItemClickListener,
) : BaseMessageViewBinder<PlaymateSayWelcomeTextMessageBean, LayoutSessionMessageTextBinding, PlaymateSayWelcomeTextMessageViewHolder>(
    onItemClickListener
) {
    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): PlaymateSayWelcomeTextMessageViewHolder {
        return PlaymateSayWelcomeTextMessageViewHolder(
            rootBinder,
            LayoutSessionMessageTextBinding.inflate(
                inflater,
                parent,
                true
            )
        )
    }

}