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

import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.weparty.im.R
import com.adealink.weparty.im.constant.SUPPORT_MESSAGE_TYPES
import com.tencent.qcloud.tuikit.tuiconversation.bean.ConversationInfo
import com.tencent.qcloud.tuikit.tuiconversation.bean.DraftInfo
import com.tencent.qcloud.tuikit.tuiconversation.presenter.ConversationPresenter

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

        return draftText
    } else {
        //上一条消息
        val lasTUIMessageBean = lastTUIMessageBean
        if (lasTUIMessageBean != null) {
            val displayString = if (SUPPORT_MESSAGE_TYPES.contains(lasTUIMessageBean.javaClass)) {
                ConversationPresenter.getMessageDisplayString(lasTUIMessageBean)
            } else {
                getCompatString(R.string.im_unsupported_message)
            }
            return displayString
        } else {
            return null
        }
    }
}

/**
 * @return 毫秒时间戳
 */
fun ConversationInfo.getMessageDisplayTime(): Long? {
    val draftInfo: DraftInfo? = draft
    if (draftInfo != null) {
        return draftInfo.draftTime
    } else {
        //上一条消息
        val lasTUIMessageBean = lastTUIMessageBean
        if (lasTUIMessageBean != null) {
            return lastMessageTime * 1000
        }
        return null
    }
}