package com.adealink.weparty.im.session.mesasge

import com.adealink.frame.data.json.froJsonErrorNull
import com.adealink.frame.oss.data.UploadFile
import com.adealink.weparty.module.playmate.data.SayWelcomeData
import com.adealink.weparty.storage.file.FilePath.audioPath
import com.adealink.weparty.util.getFilePathBy
import com.tencent.imsdk.v2.V2TIMMessage
import com.tencent.qcloud.tuikit.timcommon.bean.CustomSoundV2TIMElem
import com.tencent.qcloud.tuikit.timcommon.bean.TUIReplyQuoteBean
import com.tencent.qcloud.tuikit.tuichat.bean.message.SoundMessageBean
import com.tencent.qcloud.tuikit.tuichat.bean.message.reply.SoundReplyQuoteBean
import com.tencent.qcloud.tuikit.tuichat.presenter.ChatFileDownloadPresenter

class PlaymateSayWelcomeVoiceMessageBean : SoundMessageBean() {

    var data: SayWelcomeData? = null
    override fun onProcessMessage(v2TIMMessage: V2TIMMessage) {
        super.onProcessMessage(v2TIMMessage)
        val byte = v2TIMMessage.customElem?.data ?: return
        val dataStr = String(byte)
        val playmateData = froJsonErrorNull<SayWelcomeData>(dataStr)
        //填充数据
        this.data = playmateData
        val soundElem = CustomSoundV2TIMElem(
            System.currentTimeMillis().toInt(),
            playmateData?.voice,
            playmateData?.voiceDuration ?: 0,
            playmateData?.voice?.getFilePathBy(audioPath, UploadFile.FileType.AUDIO) ?: ""
        )
        this.soundElem = soundElem
        this.cusElem = soundElem
        val soundFile = ChatFileDownloadPresenter.getSoundMessageFile(this)
        if (soundFile == null) {
            ChatFileDownloadPresenter.downloadSound(this, null)
        }
    }

    override fun getReplyQuoteBeanClass(): Class<out TUIReplyQuoteBean<*>?> {
        return SoundReplyQuoteBean::class.java
    }

}