package de.kif.common

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule

@Serializable
data class MessageBox(
    val timestamp: Long,
    val signature: String,
    val valid: Boolean,
    val messages: List<Message>
)

@Serializable
data class Message(
    val type: MessageType,
    val repository: RepositoryType,
    val id: Long
) {
    companion object {
        val empty = Message(
            MessageType.UPDATE,
            RepositoryType.ANNOUNCEMENT,
            0L
        )
    }
}

enum class MessageType {
    CREATE, UPDATE, DELETE
}

enum class RepositoryType {
    ROOM, SCHEDULE, TRACK, USER, WORK_GROUP, POST, ANNOUNCEMENT
}

object Serialization {
    private val jsonContext = SerializersModule {}

    val json = Json(context = jsonContext)

    fun <T> stringify(serializer: SerializationStrategy<T>, obj: T) = json.stringify(serializer, obj)
    fun <T> parse(serializer: DeserializationStrategy<T>, str: String) = json.parse(serializer, str)
}
