package cn.org.bjca.trust.push.common

import cn.org.bjca.trust.push.enums.MsgType
import cn.org.bjca.trust.push.message.bean.TextMessage
import cn.org.bjca.trust.push.message.bean.UnknownMessage
import cn.org.bjca.trust.push.message.msg.Message
import cn.org.bjca.trust.push.message.msg.SendMessage
import org.json.JSONException

object MessageHelper {

    @JvmStatic
    fun parseMessage(msgType: MsgType, sendMessage: SendMessage): Message? {

        val clazz: Class<*> = when (msgType) {
            MsgType.Text -> {
                TextMessage::class.java
            }
            else -> {
                UnknownMessage::class.java
            }
        }
        var message: Message? = null

        try {
            message = clazz.newInstance() as Message
            message.parse(msgType, sendMessage)
        } catch (e: InstantiationException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        } catch (e: JSONException) {
            e.printStackTrace()
        }

        return message
    }
}