package com.rosetta.messenger.network

/**
 * Chunk packet (ID: 0x09)
 * Для разбиения больших пакетов на части (как в Desktop)
 * ВАЖНО: chunkIndex и totalChunks - Int16, не Int32!
 */
class PacketChunk : Packet() {
    var chunkId: String = ""
    var chunkIndex: Int = 0
    var totalChunks: Int = 0
    var data: ByteArray = ByteArray(0)

    override fun getPacketId(): Int = 0x09

    override fun receive(stream: Stream) {
        // В Desktop: readInt16 для index и total
        chunkIndex = stream.readInt16()
        totalChunks = stream.readInt16()
        chunkId = stream.readString()
        data = stream.readBytes()
    }

    override fun send(): Stream {
        val stream = Stream()
        stream.writeInt16(getPacketId())
        // В Desktop: writeInt16 для index и total
        stream.writeInt16(chunkIndex)
        stream.writeInt16(totalChunks)
        stream.writeString(chunkId)
        stream.writeBytes(data)
        return stream
    }
}
