import { pond } from "protobuf-ts/pond";
import { or } from "utils/types";
import { cloneDeep } from "lodash";
import { MarkerData } from "maps/mapMarkers/Markers";

interface FeatureVersionByPlatform {
  photon: string;
  electron: string;
  v2Wifi: string;
  v2Cell: string;
  v2WifiS3: string;
  v2CellS3: string;
  v2CellBlack: string;
  v2CellGreen: string;
  v2WifiBlue: string;
  v2CellBlue: string;
  v2EthBlue: string;
}

const featureVersions: Map<string, FeatureVersionByPlatform> = new Map([
  [
    "individualCalibrations",
    {
      photon: "1.19.64",
      electron: "1.19.64",
      v2Cell: "2.0.44",
      v2Wifi: "2.0.44",
      v2WifiS3: "2.0.44",
      v2CellS3: "2.0.44",
      v2CellBlack: "2.0.44",
      v2CellGreen: "2.0.44",
      v2WifiBlue: "2.0.44",
      v2CellBlue: "2.0.44",
      v2EthBlue: "2.0.44"
    }
  ],[
    "detectI2C",
    {
      photon: "N/A",
      electron: "N/A",
      v2Wifi: "N/A",
      v2Cell: "N/A",
      v2WifiS3: "N/A",
      v2CellS3: "N/A",
      v2CellBlack: "2.1.6",
      v2CellGreen: "N/A",
      v2WifiBlue: "2.1.6",
      v2CellBlue: "2.1.6",
      v2EthBlue: "2.1.6"
    }
  ]
]);
export class Device {
  public settings: pond.DeviceSettings = pond.DeviceSettings.create();
  public status: pond.DeviceStatus = pond.DeviceStatus.create();

  public static create(pb?: pond.Device): Device {
    let my = new Device();
    if (pb) {
      my.settings = pond.DeviceSettings.fromObject(or(pb.settings, {}));
      my.status = pond.DeviceStatus.fromObject(or(pb.status, {}));
    }
    return my;
  }

  public static any(data: any): Device {
    if (data.device) return Device.create(pond.Device.fromObject(cloneDeep(data.device)));
    return Device.create(pond.Device.fromObject(cloneDeep(data)));
  }

  public static clone(other: Device): Device {
    let my = new Device();
    my.settings = pond.DeviceSettings.fromObject(cloneDeep(other.settings));
    my.status = pond.DeviceStatus.fromObject(cloneDeep(other.status));
    return my;
  }

  public id(): number {
    return Number(this.settings.deviceId);
  }

  public name(): string {
    return this.settings.name !== ""
      ? this.settings.name
      : this.id() > 0
      ? "Device " + this.id()
      : "";
  }

  public location(): pond.Location {
    let loc = pond.Location.create();
    loc.longitude = this.settings.longitude;
    loc.latitude = this.settings.latitude;
    return loc;
  }

  public getMarkerData(
    clickFunc?: (event: React.PointerEvent<HTMLElement>, index: number, isMobile: boolean) => void,
    updateFunc?: (location: pond.Location) => void
  ): MarkerData {
    let m: MarkerData = {
      longitude: this.location()?.longitude ?? 0,
      latitude: this.location()?.latitude ?? 0,
      title: this.name(),
      colour: this.settings.theme?.color ?? "blue",
      clickFunc: clickFunc,
      updateFunc: updateFunc
    };
    return m;
  }

  public featureSupported(feature: string): boolean {
    let versions = featureVersions.get(feature);
    if (!versions) {
      return false;
    }
    switch (this.settings.platform) {
      case pond.DevicePlatform.DEVICE_PLATFORM_PHOTON:
        return this.status.firmwareVersion >= versions.photon;
      case pond.DevicePlatform.DEVICE_PLATFORM_ELECTRON:
        return this.status.firmwareVersion >= versions.electron;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_CELLULAR:
        return this.status.firmwareVersion >= versions.v2Cell;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_WIFI:
        return this.status.firmwareVersion >= versions.v2Wifi;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_WIFI_S3:
        return this.status.firmwareVersion >= versions.v2WifiS3;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_CELLULAR_S3:
        return this.status.firmwareVersion >= versions.v2CellS3;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_CELLULAR_BLACK:
        return this.status.firmwareVersion >= versions.v2CellBlack;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_CELLULAR_GREEN:
        return this.status.firmwareVersion >= versions.v2CellGreen;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_WIFI_BLUE:
        return this.status.firmwareVersion >= versions.v2WifiBlue;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_CELLULAR_BLUE:
        return this.status.firmwareVersion >= versions.v2CellBlue;
      case pond.DevicePlatform.DEVICE_PLATFORM_V2_ETHERNET_BLUE:
        return this.status.firmwareVersion >= versions.v2EthBlue;
      default:
        return false;
    }
  }
}
