package com.rosetta.messenger.network

data class IceServer(
    val url: String,
    val username: String,
    val credential: String,
    val transport: String
)

/**
 * ICE servers packet (ID: 0x1C / 28).
 * Wire format mirrors desktop packet.ice.servers.ts.
 */
class PacketIceServers : Packet() {
    var iceServers: List<IceServer> = emptyList()

    override fun getPacketId(): Int = 0x1C

    override fun receive(stream: Stream) {
        val count = stream.readInt16()
        val servers = ArrayList<IceServer>(count.coerceAtLeast(0))
        for (i in 0 until count) {
            servers.add(
                IceServer(
                    url = stream.readString(),
                    username = stream.readString(),
                    credential = stream.readString(),
                    transport = stream.readString()
                )
            )
        }
        iceServers = servers
    }

    override fun send(): Stream {
        val stream = Stream()
        stream.writeInt16(getPacketId())
        stream.writeInt16(iceServers.size)
        for (server in iceServers) {
            stream.writeString(server.url)
            stream.writeString(server.username)
            stream.writeString(server.credential)
            stream.writeString(server.transport)
        }
        return stream
    }
}
