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

export enum DeviceState {
    ONLINE = 0,
    OFFLINE = 1
}

export enum DeviceVerifyState {
    VERIFIED = 0,
    /**
     * Устройство не подтверждено пользователем 
     * через другой девайс
     */
    NOT_VERIFIED = 1
}

export interface DeviceEntry {
    deviceId: string;
    deviceName: string;
    deviceOs: string;
    deviceStatus: DeviceState;
    deviceVerify: DeviceVerifyState;
}

/**
 * Отправляется с сервера клиенту для передачи списка
 * подключенных в данный момент к аккаунту устройств
 */
export class PacketDeviceList extends Packet {

    private devices: DeviceEntry[] = [];

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

    public _receive(stream: Stream): void {
        const deviceCount = stream.readInt16();
        this.devices = [];
        for (let i = 0; i < deviceCount; i++) {
            const device: DeviceEntry = {
                deviceId: stream.readString(),
                deviceName: stream.readString(),
                deviceOs: stream.readString(),
                deviceStatus: stream.readInt8(),
                deviceVerify: stream.readInt8()
            };
            this.devices.push(device);
        }
    }

    public _send(): Promise<Stream> | Stream {
        const stream = new Stream();
        stream.writeInt16(this.getPacketId());
        stream.writeInt16(this.devices.length);
        for (const device of this.devices) {
            stream.writeString(device.deviceId);
            stream.writeString(device.deviceName);
            stream.writeString(device.deviceOs);
            stream.writeInt8(device.deviceStatus);
            stream.writeInt8(device.deviceVerify);
        }
        return stream;
    }

    public getDevices(): DeviceEntry[] {
        return this.devices;
    }

    public setDevices(devices: DeviceEntry[]): void {
        this.devices = devices;
    }
    
}