import TemperatureHumidityDarkIcon from "assets/components/temperatureHumidityDark.png";
import TemperatureHumidityLightIcon from "assets/components/temperatureHumidityLight.png";
import { convertedUnitMeasurement } from "models/UnitMeasurement";
import {
  AreaChartData,
  ComponentTypeExtension,
  GraphFilters,
  LineChartData,
  simpleAreaChartData,
  simpleLineChartData,
  simpleMeasurements,
  simpleSummaries,
  Summary,
  unitMeasurementSummaries
} from "pbHelpers/ComponentType";
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
import { pond } from "protobuf-ts/pond";
import { quack } from "protobuf-ts/quack";

export function VPD(subtype: number = 0): ComponentTypeExtension {
  let pressure = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PRESSURE,
    quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
    subtype
  );
  let temperature = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE,
    quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
    subtype
  );
  let humidity = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PERCENT,
    quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
    subtype
  );
  let addressTypes = [quack.AddressType.ADDRESS_TYPE_CONFIGURABLE_PIN_ARRAY];
  if (subtype === quack.DHTSubtype.DHT_SUBTYPE_SHT20) {
    addressTypes = [quack.AddressType.ADDRESS_TYPE_I2C];
  }
  return {
    type: quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
    subtypes: [
      {
        key: quack.DHTSubtype.DHT_SUBTYPE_NONE,
        value: "DHT_SUBTYPE_NONE",
        friendlyName: "DHT11"
      },
      {
        key: quack.DHTSubtype.DHT_SUBTYPE_22,
        value: "DHT_SUBTYPE_22",
        friendlyName: "DHT22"
      },
      {
        key: quack.DHTSubtype.DHT_SUBTYPE_SHT20,
        value: "DHT_SUBTYPE_SHT20",
        friendlyName: "SHT20"
      },
      {
        key: quack.DHTSubtype.DHT_SUBTYPE_FROG,
        value: "DHT_SUBTYPE_FROG",
        friendlyName: "BinPlenum"
      }
    ],
    friendlyName: "VPD",
    description: "Measures the vapour-pressure deficit using temperature and humidity",
    isController: false,
    isSource: true,
    isCalibratable: true,
    addressTypes: addressTypes,
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(pressure, temperature, humidity),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, pressure, temperature, humidity);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement,
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
        subtype,
      );
    },
    areaChartData: (
      measurement: pond.UnitMeasurementsForComponent,
      smoothingAverages?: number,
      filters?: GraphFilters
    ): AreaChartData => {
      return simpleAreaChartData(measurement, smoothingAverages, filters);
    },
    lineChartData: (
      measurement: pond.UnitMeasurementsForComponent,
      smoothingAverages?: number,
      filters?: GraphFilters
    ): LineChartData => {
      return simpleLineChartData(
        quack.ComponentType.COMPONENT_TYPE_VAPOUR_PRESSURE_DEFICIT,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 1000,
    icon: (theme?: "light" | "dark"): string | undefined => {
      return theme === "light" ? TemperatureHumidityDarkIcon : TemperatureHumidityLightIcon;
    }
  };
}

export default VPD;
