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

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.frame.dot.NumDot
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.im.databinding.LayoutSessionListOfficialItemBinding
import com.adealink.weparty.im.list.adapter.data.OfficialSessionListItem
import com.adealink.weparty.util.formatTimeStr
import com.tencent.qcloud.tuikit.tuiconversation.bean.DraftInfo
import com.tencent.qcloud.tuikit.tuiconversation.minimalistui.interfaces.OnConversationAdapterListener
import com.tencent.qcloud.tuikit.tuiconversation.presenter.ConversationPresenter


class OfficialListItemViewBinder(
    val listener: OnConversationAdapterListener
) :
    ItemViewBinder<OfficialSessionListItem, BindingViewHolder<LayoutSessionListOfficialItemBinding>>() {

    override fun onBindViewHolder(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        item: OfficialSessionListItem,
    ) {
        holder.binding.root.onClick {
            listener.onItemClick(it, item.data.type, item.data)
        }
        holder.binding.tvTitle.text = item.data.title
        setLastMessageAndStatus(holder, item)
    }

    private fun setLastMessageAndStatus(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        item: OfficialSessionListItem,
    ) {
        val conversation = item.data

        val draftInfo: DraftInfo? = conversation.draft
        if (draftInfo != null) {
            //草稿
            val draftText =
                (froJsonErrorNull<HashMap<*, *>>(draftInfo.draftText)?.get("content") as? String)
                    ?: draftInfo.draftText

            holder.binding.tvDesc.text = draftText
            setLastMessageTime(holder, draftInfo.draftTime * 1000)
        } else {
            //上一条消息
            val lasTUIMessageBean = conversation.lastTUIMessageBean
            if (lasTUIMessageBean != null) {
                val displayString = ConversationPresenter.getMessageDisplayString(lasTUIMessageBean)
                holder.binding.tvDesc.text = displayString
            }
            if (conversation.lastMessage != null) {
                setLastMessageTime(holder, conversation.lastMessageTime * 1000)
            }
        }

        holder.binding.vDot.show(NumDot(conversation.unRead))
    }

    private fun setLastMessageTime(
        holder: BindingViewHolder<LayoutSessionListOfficialItemBinding>,
        timeTs: Long
    ) {

        holder.binding.tvTime.text = timeTs.formatTimeStr()
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): BindingViewHolder<LayoutSessionListOfficialItemBinding> {
        return BindingViewHolder(
            LayoutSessionListOfficialItemBinding.inflate(
                inflater,
                parent,
                false
            )
        )
    }
}