package com.rosetta.messenger.network

/**
 * Online Subscribe packet (ID: 0x04)
 * Subscribe to user online status updates
 * Формат как в React Native: privateKey + array of publicKeys
 */
class PacketOnlineSubscribe : Packet() {
    var privateKey: String = ""
    var publicKeys: MutableList<String> = mutableListOf()

    override fun getPacketId(): Int = 0x04

    override fun receive(stream: Stream) {
        privateKey = stream.readString()
        val keysCount = stream.readInt16()
        publicKeys.clear()
        repeat(keysCount) {
            publicKeys.add(stream.readString())
        }
    }

    override fun send(): Stream {
        val stream = Stream()
        stream.writeInt16(getPacketId())
        stream.writeString(privateKey)
        stream.writeInt16(publicKeys.size)
        publicKeys.forEach { key ->
            stream.writeString(key)
        }
        return stream
    }

    fun addPublicKey(publicKey: String) {
        publicKeys.add(publicKey)
    }
}
