import { createContext, useState } from "react";
import { DeviceEntry, PacketDeviceList } from "../ProtocolProvider/protocol/packets/packet.device.list";
import { usePacket } from "../ProtocolProvider/usePacket";

interface DeviceProviderContextValue {
    devices: DeviceEntry[];
}

export const DeviceProviderContext = createContext<DeviceProviderContextValue|null>(null);

interface DeviceProviderProps {
    children?: React.ReactNode;
}

export function DeviceProvider(props: DeviceProviderProps) {
    /**
     * Подключенные устройства
     */
    const [devices, setDevices] = useState<DeviceEntry[]>([]);

    usePacket(0x17, (packet : PacketDeviceList) => {
        console.info("Device list update", packet.getDevices());
        setDevices(packet.getDevices());
    }, []);

    return (
        <DeviceProviderContext.Provider value={{
            devices: devices
        }}>
            {props.children}
        </DeviceProviderContext.Provider>
    )
}