package com.rosetta.messenger.network

enum class WebRTCSignalType(val value: Int) {
    OFFER(0),
    ANSWER(1),
    ICE_CANDIDATE(2);

    companion object {
        fun fromValue(value: Int): WebRTCSignalType =
            entries.firstOrNull { it.value == value }
                ?: throw IllegalArgumentException("Unknown WebRTCSignalType code: $value")
    }
}

/**
 * WebRTC exchange packet (ID: 0x1B / 27).
 * Wire format mirrors desktop packet.webrtc.ts.
 */
class PacketWebRTC : Packet() {
    var signalType: WebRTCSignalType = WebRTCSignalType.OFFER
    var sdpOrCandidate: String = ""

    override fun getPacketId(): Int = 0x1B

    override fun receive(stream: Stream) {
        signalType = WebRTCSignalType.fromValue(stream.readInt8())
        sdpOrCandidate = stream.readString()
    }

    override fun send(): Stream {
        val stream = Stream()
        stream.writeInt16(getPacketId())
        stream.writeInt8(signalType.value)
        stream.writeString(sdpOrCandidate)
        return stream
    }
}
