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 PictureMessage : Message() {
    var imageStorageId: String? = null
    var thumbnail: String? = null
    var thumbnailWidth: Double? = null
    var thumbnailHeight: Double? = null

    init {
        this.msgType = MsgType.Image
    }

    override fun parse(type: MsgType, sendMessage: SendMessage) {
        super.parse(type, sendMessage)
        try {
            val json = sendMessage.message?.let { JSONObject(it) }
            if (json != null) {
                this.imageStorageId = json.optString("imageStorageId")
                this.thumbnail = json.optString("thumbnail")
                this.thumbnailWidth = json.optDouble("thumbnailWidth")
                this.thumbnailHeight = json.optDouble("thumbnailHeight")
            }
        } catch (e: Exception) {
            LogHelper.e("消息组装失败", e)
        }
    }

    override fun createContentJsonStr(): String {

        val json = JSONObject()
        try {
            json.put("imageStorageId", this.imageStorageId)
            json.put("thumbnail", this.thumbnail)
            json.put("thumbnailWidth", this.thumbnailWidth)
            json.put("thumbnailHeight", this.thumbnailHeight)
        } catch (e: JSONException) {
            LogHelper.e("消息组装失败", e)
        }
        return json.toString()
    }
}