package org.DUCodeWars.framework.server.net.packets.notifications

import com.google.gson.JsonObject
import org.DUCodeWars.framework.server.net.packets.JsonSerialiser
import org.DUCodeWars.framework.server.net.packets.PacketSer
import org.DUCodeWars.framework.server.net.packets.Request

abstract class NotificationPacketSer<I : NotificationRequest>(action: String) : PacketSer<I, NotificationResponse>(
        action) {
    final override val resSer = NotificationResSer()
}

class NotificationResSer : JsonSerialiser<NotificationResponse>() {
    override fun ser(packet: NotificationResponse): JsonObject {
        val json = JsonObject()
        json.addProperty("received", 1)
        return json
    }

    override fun deserialise(json: JsonObject): NotificationResponse {
        val received = json["received"].asInt
        return if (received == 1) {
            val action = json["action"].asString!!
            NotificationResponse(action)
        }
        else {
            throw InvalidResponseException("Notification Response contained received = $received")
        }
    }
}