package com.rosetta.messenger.network

enum class SyncStatus(val value: Int) {
    NOT_NEEDED(0),
    BATCH_START(1),
    BATCH_END(2);

    companion object {
        fun fromValue(value: Int): SyncStatus {
            return entries.firstOrNull { it.value == value } ?: NOT_NEEDED
        }
    }
}

/**
 * Sync packet (ID: 0x19)
 * Используется для батчевой синхронизации сообщений и read-статусов
 */
class PacketSync : Packet() {
    var status: SyncStatus = SyncStatus.NOT_NEEDED
    var timestamp: Long = 0

    override fun getPacketId(): Int = 0x19

    override fun receive(stream: Stream) {
        status = SyncStatus.fromValue(stream.readInt8())
        timestamp = stream.readInt64()
    }

    override fun send(): Stream {
        val stream = Stream()
        stream.writeInt16(getPacketId())
        stream.writeInt8(status.value)
        stream.writeInt64(timestamp)
        return stream
    }
}
