import Packet from "../packet";
import Stream from "../stream";

export enum WebRTCSignalType {
    OFFER = 0,
    ANSWER = 1,
    ICE_CANDIDATE = 2
}

/**
 * Пакет для обмена сигналами WebRTC, такими как оффер, ответ и ICE кандидаты. 
 * Используется на стадии WEB_RTC_EXCHANGE в сигналинге звонков.
 */
export class PacketWebRTC extends Packet {

    private signalType: WebRTCSignalType = WebRTCSignalType.OFFER;
    private sdpOrCandidate: string = "";

    public getPacketId(): number {
        return 27;
    }

    public _receive(stream: Stream): void {
        this.signalType = stream.readInt8();
        this.sdpOrCandidate = stream.readString();
    }

    public _send(): Promise<Stream> | Stream {
        let stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeInt8(this.signalType);
        stream.writeString(this.sdpOrCandidate);
        return stream;
    }

    public setSignalType(type: WebRTCSignalType) {
        this.signalType = type;
    }

    public getSignalType(): WebRTCSignalType {
        return this.signalType;
    }
    
    public setSdpOrCandidate(data: string) {
        this.sdpOrCandidate = data;
    }

    public getSdpOrCandidate(): string {
        return this.sdpOrCandidate;
    }

}