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

import cn.org.bjca.trust.push.db.DbHelper
import cn.org.bjca.trust.push.db.message.SzyxMessage
import cn.org.bjca.trust.push.enums.MsgType
import cn.org.bjca.trust.push.message.bean.*
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 getMessage(): List<SzyxMessage> = DbHelper.getDataBase().messageDao().getAll()

    @JvmStatic
    fun getMessageForPage(start: Int, limit: Int): List<Message> =
        DbHelper.getDataBase().messageDao().getMessageForPage(start, limit).map {
            parseMessage(it)?:UnknownMessage()
        }

    @JvmStatic
    fun parseMessage(sendMessage: SendMessage): Message? {
        val msgType = MsgType.getMsgType(sendMessage.messageType)

        val clazz: Class<*> = when (msgType) {
            MsgType.Text -> {
                TextMessage::class.java
            }
            MsgType.Voice -> {
                VoiceMessage::class.java
            }
            MsgType.Image -> {
                PictureMessage::class.java
            }
            MsgType.Location -> {
                LocationMessage::class.java
            }
            MsgType.Video -> {
                VideoMessage::class.java
            }
            MsgType.File -> {
                FileMessage::class.java
            }
            MsgType.At -> {
                AtMessage::class.java
            }
            MsgType.Other -> {
                OtherMessage::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
    }

    @JvmStatic
    fun parseMessage(msg: SzyxMessage): Message? {
        val sendMessage = SendMessage().apply {
            messageId = msg.messageId
            timestamp = msg.timestamp
            fromClientId = msg.fromClientId
            toClientId = msg.toClientId
            target = msg.target
            messageType = msg.messageType
            message = msg.message
            tenantNo = msg.tenantNo
        }
        return parseMessage(sendMessage)
    }

    fun changeStatus(msgId: String, status: Boolean): Message? {
        val m = DbHelper.getDataBase().messageDao().getMessage(msgId)
        if (m.isNotEmpty()) {
            val ms = m[0]
            if (ms.status == 0) {
                ms.status = if (status) 1 else 2
                DbHelper.getDataBase().messageDao().update(ms)
                parseMessage(ms)?.also { ite ->
                    return ite
                }
            }
        }
        return null
    }
}