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

export enum OnlineState {
    ONLINE,
    OFFLINE
}

export interface PublicKeyOnlineState {
    publicKey: string;
    state: OnlineState;
}

export class PacketOnlineState extends Packet {

    private publicKeysState: PublicKeyOnlineState[] = [];


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

    public _receive(stream: Stream): void {
        const publicKeyCount = stream.readInt8();
        for (let i = 0; i < publicKeyCount; i++) {
            const publicKey = stream.readString();
            const state = stream.readBoolean() ? OnlineState.ONLINE : OnlineState.OFFLINE;
            this.publicKeysState.push({ publicKey, state });
        }
    }

    public _send(): Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeInt8(this.publicKeysState.length);
        for (const publicKeyState of this.publicKeysState) {
            stream.writeString(publicKeyState.publicKey);
            stream.writeBoolean(publicKeyState.state === OnlineState.ONLINE);
        }
        return stream;
    }

    public addPublicKeyState(publicKey: string, state: OnlineState): void {
        this.publicKeysState.push({ publicKey, state });
    }

    public getPublicKeysState(): PublicKeyOnlineState[] {
        return this.publicKeysState;
    }

    public setPublicKeysState(publicKeysState: PublicKeyOnlineState[]): void {
        this.publicKeysState = publicKeysState;
    }

    

    
}