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


/**
 * Push Notification actions
 * SUBSCRIBE - подписаться на push-уведомления
 * UNSUBSCRIBE - отписаться от push-уведомлений
 */
export enum PushNotificationAction {
    SUBSCRIBE = 0,
    UNSUBSCRIBE = 1,
}

/**
 * Push Notification packet
 * ID: 0x10
 * 
 * Этот пакет отправляется клиентом для подписки на push-уведомления.
 * Отправлять можно только в том случае, если пользователь уже прошел
 * рукопожатие и установил соединение.
 */
export class PacketPushNotification extends Packet {
    private notificationsToken: string = "";
    private action: PushNotificationAction = PushNotificationAction.SUBSCRIBE;
    

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

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

    public _send(): Promise<Stream> | Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeString(this.notificationsToken);
        stream.writeInt8(this.action);
        return stream;
    }

    public getNotificationsToken(): string {
        return this.notificationsToken;
    }

    public setNotificationsToken(notificationsToken: string): void {
        this.notificationsToken = notificationsToken;
    }

    public getAction(): PushNotificationAction {
        return this.action;
    }

    public setAction(action: PushNotificationAction): void {
        this.action = action;
    }
}