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.JSONArray
import org.json.JSONException
import org.json.JSONObject

class AtMessage : Message() {
    var text: String? = null
    var atNos: List<String>? = null

    init {
        this.msgType = MsgType.At
    }

    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")
                val arr = json.optJSONArray("atNos")
                val atNos = arrayListOf<String>()
                if (arr != null) {
                    for (index in 0 until arr.length()) {
                        atNos.add(arr.optString(index))
                    }
                }
                this.atNos = atNos
            }
        } catch (e: Exception) {
            LogHelper.e("消息组装失败", e)
        }
    }

    override fun createContentJsonStr(): String {
        val json = JSONObject()
        try {
            json.put("text", this.text)
            val jsonArray = JSONArray(this.atNos)
            json.put("atNos", jsonArray)
        } catch (e: JSONException) {
            LogHelper.e("消息组装失败", e)
        }
        return json.toString()
    }
}