import { useEffect, useState } from "react";

export function useDeviceInfo(isDeviceAPIAvailable: boolean) {
  const [deviceInfo, setDeviceInfo] = useState<{
    firmwareVersion: string;
    model: string;
    ipAddress: string;
    serialNumber: string;
    encryptionStatus: {
      encrypted: boolean;
      prerequisitesMet: boolean;
    };
  } | null>(null);

  useEffect(() => {
    const fetchDeviceInfo = async () => {
      if (isDeviceAPIAvailable) {
        const [
          firmwareVersion,
          model,
          ipAddress,
          serialNumber,
          encryptionStatus,
        ] = await Promise.all([
          window.parent.window.DeviceAPI.getFirmwareVersion(),
          window.parent.window.DeviceAPI.getModel(),
          window.parent.window.DeviceAPI.getIPAddress(),
          window.parent.window.DeviceAPI.getSerialNumber(),
          window.parent.window.DeviceAPI.getStorageEncryptionStatus(),
        ]);
        setDeviceInfo({
          firmwareVersion,
          model,
          ipAddress,
          serialNumber,
          encryptionStatus,
        });
      }
    };

    fetchDeviceInfo();
  }, [isDeviceAPIAvailable]);

  return deviceInfo;
}
