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 LocationMessage : Message() {
    var address: String? = null
    var title: String? = null
    var thumbnail: String? = null
    var longitude: Double? = null
    var latitude: Double? = null
    var scale: Int? = null

    init {
        this.msgType = MsgType.Location
    }

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

    override fun createContentJsonStr(): String {

        val json = JSONObject()
        try {
            json.put("address", this.address)
            json.put("title", this.title)
            json.put("thumbnail", this.thumbnail)
            json.put("longitude", this.longitude)
            json.put("latitude", this.latitude)
            json.put("scale", this.scale)
        } catch (e: JSONException) {
            LogHelper.e("消息组装失败", e)
        }
        return json.toString()
    }
}