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

export enum SignalType {
    CALL = 0,
    KEY_EXCHANGE = 1,
    ACTIVE_CALL = 2,
    END_CALL = 3,
    CREATE_ROOM = 4,
    END_CALL_BECAUSE_PEER_DISCONNECTED = 5,
    END_CALL_BECAUSE_BUSY = 6
}

/**
 * Пакет сигналинга, для сигналов WebRTC используется отдельный пакет 27 PacketWebRTCExchange
 */
export class PacketSignalPeer extends Packet {

    private src: string = "";
    /**
     * Назначение
     */
    private dst: string = "";
    /**
     * Используется если SignalType == KEY_EXCHANGE, для идентификации сессии обмена ключами
     */
    private sharedPublic: string = "";

    private signalType: SignalType = SignalType.CALL;

    /** 
     * Используется если SignalType == CREATE_ROOM, 
     * для идентификации комнаты на SFU сервере, в которой будет происходить обмен сигналами 
     * WebRTC для установления P2P соединения между участниками звонка
     */
    private roomId: string = "";


    public getPacketId(): number {
        return 26;
    }

    public _receive(stream: Stream): void {
        this.signalType = stream.readInt8();
        if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY || this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){
            return;
        }
        this.src = stream.readString();
        this.dst = stream.readString();
        if(this.signalType == SignalType.KEY_EXCHANGE){
            this.sharedPublic = stream.readString();
        }
        if(this.signalType == SignalType.CREATE_ROOM){
            this.roomId = stream.readString();
        }
    }

    public _send(): Promise<Stream> | Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeInt8(this.signalType);
        if(this.signalType == SignalType.END_CALL_BECAUSE_BUSY || this.signalType == SignalType.END_CALL_BECAUSE_PEER_DISCONNECTED){
            return stream;
        }
        stream.writeString(this.src);
        stream.writeString(this.dst);
        if(this.signalType == SignalType.KEY_EXCHANGE){
            stream.writeString(this.sharedPublic);
        }
        if(this.signalType == SignalType.CREATE_ROOM){
            stream.writeString(this.roomId);
        }
        return stream;
    }

    public setDst(dst: string) {
        this.dst = dst;
    }

    public setSharedPublic(sharedPublic: string) {
        this.sharedPublic = sharedPublic;
    }

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

    public getDst(): string {
        return this.dst;
    }

    public getSharedPublic(): string {
        return this.sharedPublic;
    }

    public getSignalType(): SignalType {
        return this.signalType;
    }

    public getSrc(): string {
        return this.src;
    }

    public setSrc(src: string) {
        this.src = src;
    }

    public getRoomId(): string {
        return this.roomId;
    }
    
    public setRoomId(roomId: string) {
        this.roomId = roomId;
    }
    
}