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

export function CO2(subtype: number = 0): ComponentTypeExtension {
  let co2 = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PPM,
    quack.ComponentType.COMPONENT_TYPE_CO2,
    subtype
  );
  return {
    type: quack.ComponentType.COMPONENT_TYPE_CO2,
    subtypes: [
      {
        key: quack.Co2Subtype.CO2_SUBTYPE_NONE,
        value: "CO2_SUBTYPE_NONE",
        friendlyName: "CO2 Only"
      },
      {
        key: quack.Co2Subtype.CO2_SUBTYPE_SCD30,
        value: "CO2_SUBTYPE_SCD30",
        friendlyName: "SCD30 (CO2)"
      },
    ],
    friendlyName: "CO2",
    description: "Measures the parts per million of CO2",
    isController: false,
    isSource: true,
    isCalibratable: true,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_I2C],
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(co2),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, co2);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_CO2,
        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_CO2,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 5000,
    icon: (theme?: "light" | "dark"): string | undefined => {
      return theme === "light" ? Co2SensorDarkIcon : Co2SensorLightIcon;
    },
    secondaryComponentSettings: () => {
      let secondaryComps: pond.ComponentSettings[] = []
      switch (subtype){ //doesn't have a default because it would just do nothing
        case quack.Co2Subtype.CO2_SUBTYPE_SCD30:
          let tempHumComp: pond.ComponentSettings = pond.ComponentSettings.create()
          //set the defaults for the component
          tempHumComp.type = quack.ComponentType.COMPONENT_TYPE_DHT
          tempHumComp.subtype = quack.DHTSubtype.DHT_SUBTYPE_SCD30
          tempHumComp.addressType = quack.AddressType.ADDRESS_TYPE_I2C
          tempHumComp.address = 0x40
          tempHumComp.name = "SCD30 (Temp/Humidity)"
          //add it to the array to be returned
          secondaryComps.push(tempHumComp)
        break
      }
      //switch statement here that uses the subtype to determine whaat the secondary component is
      return secondaryComps
    }
  };
}
