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

export class PacketOnlineSubscribe extends Packet {

    private privateKey : string = "";
    private publicKeys: string[] = [];

    public getPacketId(): number {
        return 0x04;
    }

    public _receive(stream: Stream): void {
        this.privateKey = stream.readString();
        const keysCount = stream.readInt16();
        for (let i = 0; i < keysCount; i++) {
            this.publicKeys.push(stream.readString());
        }
    }

    public _send(): Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeString(this.privateKey);
        stream.writeInt16(this.publicKeys.length);
        for (const key of this.publicKeys) {
            stream.writeString(key);
        }
        return stream;
    }
    
    public getPrivateKey(): string {
        return this.privateKey;
    }

    public setPrivateKey(privateKey: string): void {
        this.privateKey = privateKey;
    }

    public getPublicKeys(): string[] {
        return this.publicKeys;
    }

    public setPublicKeys(publicKeys: string[]): void {
        this.publicKeys = publicKeys;
    }

    public addPublicKey(publicKey: string): void {
        this.publicKeys.push(publicKey);
    }

    public removePublicKey(publicKey: string): void {
        const index = this.publicKeys.indexOf(publicKey);
        if (index > -1) {
            this.publicKeys.splice(index, 1);
        }
    }

}