package com.tencent.qcloud.tuikit.timcommon.bean

import com.tencent.imsdk.common.IMCallback
import com.tencent.imsdk.message.DownloadParam
import com.tencent.imsdk.message.DownloadProgressInfo
import com.tencent.imsdk.message.MessageBaseElement
import com.tencent.imsdk.message.MessageCenter
import com.tencent.imsdk.v2.V2TIMCallback
import com.tencent.imsdk.v2.V2TIMDownloadCallback
import com.tencent.imsdk.v2.V2TIMSoundElem
import com.tencent.imsdk.v2.V2TIMValueCallback

class CustomSoundV2TIMElem(
    val businessId: Int,
    val voiceUrl: String?,
    val durationTs: Int, //单位:秒
    val voicePath: String
) : V2TIMSoundElem() {

    private val ts = System.currentTimeMillis()


    override fun getPath(): String? {
        return voicePath
    }

    override fun getUUID(): String? {
        return "$voiceUrl $duration $ts"
    }

    override fun getDuration(): Int {
        return durationTs
    }

    override fun downloadSound(path: String?, v2TIMDownloadCallback: V2TIMDownloadCallback?) {
        val downloadParam = DownloadParam()
        downloadParam.setDownloadUrl(voiceUrl)
        downloadParam.setUuid(this.uuid)
        downloadParam.setUuidType(MessageBaseElement.UUID_TYPE_AUDIO)
        downloadParam.setBusinessID(businessId)
        downloadParam.setFileSavePath(path)
        val v2TIMValueProgressCallback: V2TIMValueCallback<DownloadProgressInfo?> =
            object : V2TIMValueCallback<DownloadProgressInfo?> {
                override fun onSuccess(downloadProgressInfo: DownloadProgressInfo?) {
                    val v2ProgressInfo = V2ProgressInfo(
                        downloadProgressInfo?.currentSize ?: 0,
                        downloadProgressInfo?.totalSize ?: 0
                    )
                    v2TIMDownloadCallback?.onProgress(v2ProgressInfo)
                }

                override fun onError(code: Int, desc: String?) {
                    v2TIMDownloadCallback?.onError(code, desc)
                }
            }
        val v2TIMCallback: V2TIMCallback = object : V2TIMCallback {
            override fun onSuccess() {
                v2TIMDownloadCallback?.onSuccess()
            }

            override fun onError(code: Int, desc: String?) {
                v2TIMDownloadCallback?.onError(code, desc)
            }
        }
        MessageCenter.getInstance().downloadMessageElement(
            downloadParam,
            object : IMCallback<DownloadProgressInfo?>(v2TIMValueProgressCallback) {
                override fun success(data: DownloadProgressInfo?) {
                    super.success(data)
                }

                override fun fail(code: Int, errorMessage: String?) {
                    super.fail(code, errorMessage)
                }
            },
            object : IMCallback<Any?>(v2TIMCallback) {
                override fun success(data: Any?) {
                    super.success(data)
                }

                override fun fail(code: Int, errorMessage: String?) {
                    super.fail(code, errorMessage)
                }
            })
    }

    override fun getUrl(callback: V2TIMValueCallback<String?>?) {
        if (callback != null) {
            if (voiceUrl.isNullOrEmpty()) {
                callback.onError(6017, "voice url is null")
            } else {
                callback.onSuccess(voiceUrl)
            }
        }
    }

    override fun convertVoiceToText(language: String?, callback: V2TIMValueCallback<String?>?) {
        if (callback != null) {
            val voiceUrl = voiceUrl
            if (voiceUrl.isNullOrEmpty()) {
                callback.onError(6017, "voice url is null")
                return
            }
            MessageCenter.getInstance()
                .convertVoiceToText(voiceUrl, language, null)
        }
    }

    override fun toString(): String {
        val stringBuilder = StringBuilder()
        stringBuilder.append("CustomSoundV2TIMElem--->")
            .append("uuid:").append(this.uuid)
            .append(", url:").append(this.voiceUrl)
            .append(", sender local path:").append(this.path)
            .append(", duration:").append(this.duration)
        return stringBuilder.toString()
    }
}
