package com.adealink.weparty.im.stat

import com.adealink.frame.statistics.CommonEventValue
import com.adealink.frame.statistics.IEventValue
import com.adealink.weparty.module.profile.ProfileModule
import com.adealink.weparty.stat.HttpEvent
import com.adealink.weparty.util.takeLastIf
import com.tencent.qcloud.tuikit.timcommon.bean.TUIMessageBean
import com.tencent.qcloud.tuikit.tuichat.bean.message.isUserMessageBean

/**
 * 存在任意连续10分钟内，陪玩师向同一用户发送消息数量≥5条的去重用户数
 */
class DeepChatEvent : HttpEvent("deep_chat_hit") {

    override val action: IEventValue = CommonEventValue.Action.EMPTY
    override fun toString(): String {
        return "DeepChatEvent(targetUid:${targetUid.value}, scene:${scene.value})"
    }

    companion object {
        @JvmStatic
        fun checkDeepChat(messageList: List<TUIMessageBean>?) {
            val lastMessageList = messageList?.takeLastIf(5) {
                it.sender == ProfileModule.getMyUid()
                        && it.isUserMessageBean()
            }
            if (lastMessageList.isNullOrEmpty() || lastMessageList.size != 5) {
                return
            }
            val firstMessage = lastMessageList.firstOrNull()
            val lastMessage = lastMessageList.lastOrNull()
            if (firstMessage == null || lastMessage == null) {
                return
            }
            val timeOffset = lastMessage.messageTime - firstMessage.messageTime //单位:秒
            if (timeOffset < 600) {
                DeepChatEvent().apply {
                    scene to "playmate_deep_chat_report"
                    targetUid to firstMessage.userId
                }.send()
            }
        }
    }
}