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

export interface G365IceServer {
    url: string;
    username: string;
    credential: string;
    transport: string;
}

export class PacketIceServers extends Packet {
    private iceServers: G365IceServer[] = [];

    public getPacketId(): number {
        return 28;
    }

    public _receive(stream: Stream): void {
        const serversCount = stream.readInt16();
        this.iceServers = [];
        for(let i = 0; i < serversCount; i++){
            const url = stream.readString();
            const username = stream.readString();
            const credential = stream.readString();
            const transport = stream.readString();
            this.iceServers.push({
                url,
                username,
                credential,
                transport
            });
        }
    }

    public _send(): Promise<Stream> | Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeInt16(this.iceServers.length);
        for(let i = 0; i < this.iceServers.length; i++){
            const server = this.iceServers[i];
            stream.writeString(server.url);
            stream.writeString(server.username || "");
            stream.writeString(server.credential || "");
            stream.writeString(server.transport || "");
        }
        return stream;
    }

    public getIceServers(): G365IceServer[] {
        return this.iceServers;
    }

    public setIceServers(servers: G365IceServer[]) {
        this.iceServers = servers;
    }

}