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.getActivity
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.im.databinding.LayoutSessionMessageOfficialBaseBinding
import com.adealink.weparty.im.databinding.LayoutSessionMessageOfficialImageTextBinding
import com.adealink.weparty.im.session.mesasge.OfficialImageTextMessageBean
import com.adealink.weparty.util.goLocalLinkPage
import com.tencent.qcloud.tuikit.timcommon.component.face.FaceManager
import com.tencent.qcloud.tuikit.timcommon.interfaces.OnItemClickListener

/**
 * 对照: TextMessageHolder
 */
class OfficialImageTextMessageViewHolder(
    rootBinding: LayoutSessionMessageOfficialBaseBinding,
    binding: LayoutSessionMessageOfficialImageTextBinding
) : CenterMessageViewHolder<OfficialImageTextMessageBean, LayoutSessionMessageOfficialImageTextBinding>(
    rootBinding,
    binding
) {

    override fun onBindMessage(
        binding: LayoutSessionMessageOfficialImageTextBinding,
        msg: OfficialImageTextMessageBean
    ) {

        if (!msg.image.isNullOrEmpty()) {
            binding.ivImg.show()
            binding.ivImg.setImageUrl(msg.image)
        } else {
            binding.ivImg.gone()
        }

        if (!msg.title.isNullOrEmpty()) {
            binding.tvTitle.show()
            FaceManager.handlerEmojiText(
                binding.tvTitle,
                msg.title,
                false
            )
        } else {
            binding.tvTitle.gone()
        }

        if (!msg.content.isNullOrEmpty()) {
            binding.tvContent.show()
            FaceManager.handlerEmojiText(
                binding.tvContent,
                msg.content,
                false
            )
        } else {
            binding.tvContent.gone()
        }

        if (!msg.link.isNullOrEmpty()) {
            binding.vLink.show()
            binding.vLink.onClick {
                val activity = it.context.getActivity() ?: return@onClick
                goLocalLinkPage(activity, msg.link)
            }
        } else {
            binding.vLink.gone()
        }
    }

}

class OfficialImageTextMessageViewBinder(
    onItemClickListener: OnItemClickListener
) : BaseCenterMessageViewBinder<OfficialImageTextMessageBean, LayoutSessionMessageOfficialImageTextBinding, OfficialImageTextMessageViewHolder>(
    onItemClickListener
) {
    override fun onCreateMessageHolder(
        rootBinder: LayoutSessionMessageOfficialBaseBinding,
        inflater: LayoutInflater,
        parent: ViewGroup
    ): OfficialImageTextMessageViewHolder {
        return OfficialImageTextMessageViewHolder(
            rootBinder,
            LayoutSessionMessageOfficialImageTextBinding.inflate(
                inflater,
                parent,
                true
            )
        )
    }

}