package cn.org.bjca.trust.push.message.bean

import cn.org.bjca.trust.push.common.LogHelper
import cn.org.bjca.trust.push.enums.MsgType
import cn.org.bjca.trust.push.message.msg.Message
import cn.org.bjca.trust.push.message.msg.SendMessage
import org.json.JSONException
import org.json.JSONObject

class VideoMessage : Message() {
    var videoStorageId: String? = null
    var fileName: String? = null
    var fileSize: Long? = null
    var duration: Long? = null
    var firstFrame: String? = null
    var firstFrameWidth: Int? = null
    var firstFrameHeight: Int? = null

    init {
        this.msgType = MsgType.Video
    }

    override fun parse(type: MsgType, sendMessage: SendMessage) {
        super.parse(type, sendMessage)
        try {
            val json = sendMessage.message?.let { JSONObject(it) }
            if (json != null) {
                this.videoStorageId = json.optString("videoStorageId")
                this.fileName = json.optString("fileName")
                this.firstFrame = json.optString("firstFrame")
                this.fileSize = json.optLong("fileSize")
                this.duration = json.optLong("duration")
                this.firstFrameWidth = json.optInt("firstFrameWidth")
                this.firstFrameHeight = json.optInt("firstFrameHeight")
            }
        } catch (e: Exception) {
            LogHelper.e("消息组装失败", e)
        }
    }

    override fun createContentJsonStr(): String {

        val json = JSONObject()
        try {
            json.put("videoStorageId", this.videoStorageId)
            json.put("fileName", this.fileName)
            json.put("firstFrame", this.firstFrame)
            json.put("fileSize", this.fileSize)
            json.put("duration", this.duration)
            json.put("firstFrameWidth", this.firstFrameWidth)
            json.put("firstFrameHeight", this.firstFrameHeight)
        } catch (e: JSONException) {
            LogHelper.e("消息组装失败", e)
        }
        return json.toString()
    }
}