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

import cn.org.bjca.trust.push.common.LogHelper
import cn.org.bjca.trust.push.common.json.GsonImplHelp
import cn.org.bjca.trust.push.db.device.Device
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 LogMessage() : Message() {
    var text: String? = null
    var versionName: String? = null
    var versionCode: Int? = null
    var device: Device? = null

    init {
        this.msgType = MsgType.Log
    }

    override fun parse(type: MsgType, sendMessage: SendMessage) {
        super.parse(type, sendMessage)
        try {
            val json = sendMessage.message?.let { JSONObject(it) }
            if (json != null) {
                this.text = json.optString("text")
                this.versionName = json.optString("versionName")
                this.versionCode = json.optInt("versionCode")
                this.device = json.optString("device")?.let {
                    GsonImplHelp.get().toObject(it, Device::class.java)
                }
            }
        } catch (e: Exception) {
            LogHelper.e("消息组装失败", e)
        }
    }

    override fun createContentJsonStr(): String {

        val json = JSONObject()
        try {
            json.put("text", this.text)
            json.put("versionName", this.versionName)
            json.put("versionCode", this.versionCode)
            json.put("device", GsonImplHelp.get().toJson(this.device))
        } catch (e: JSONException) {
            LogHelper.e("消息组装失败", e)
        }
        return json.toString()
    }
}