import {
  ComponentTypeExtension,
  Summary,
  // simpleMeasurements,
  // simpleSummaries,
  unitMeasurementSummaries,
  AreaChartData,
  GraphFilters,
  simpleAreaChartData,
  LineChartData,
  simpleLineChartData,
  ComponentMeasurement
} from "pbHelpers/ComponentType";
import PressureDarkIcon from "assets/components/pressureDark.png";
import PressureLightIcon from "assets/components/pressureLight.png";
import { quack } from "protobuf-ts/quack";
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
import { convertedUnitMeasurement } from "models/UnitMeasurement";
import { pond } from "protobuf-ts/pond";

export function Airflow(subtype: number = 0): ComponentTypeExtension {
  let cfm = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_CFM,
    quack.ComponentType.COMPONENT_TYPE_AIRFLOW,
    subtype
  );

  let velocity = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_CFM,
    quack.ComponentType.COMPONENT_TYPE_AIRFLOW,
    subtype
  );

  let measurementTypes = [
      {
        measurementType: quack.MeasurementType.MEASUREMENT_TYPE_PPM,
        label: cfm.label(),
        colour: cfm.colour(),
        graphType: cfm.graph()
      } as ComponentMeasurement,
      {
        measurementType: quack.MeasurementType.MEASUREMENT_TYPE_VOLTAGE,
        label: velocity.label(),
        colour: velocity.colour(),
        graphType: velocity.graph()
      } as ComponentMeasurement
    ];
  return {
    type: quack.ComponentType.COMPONENT_TYPE_AIRFLOW,
    subtypes: [
      {
        key: quack.AirFlowSubtype.AIR_FLOW_SUBTYPE_PROSENSE,
        value: "AIR_FLOW_SUBTYPE_PROSENSE",
        friendlyName: "Prosense"
      },
      {
        key: quack.AirFlowSubtype.AIR_FLOW_SUBTYPE_ULTRASONIC,
        value: "AIR_FLOW_SUBTYPE_ULTRASONIC",
        friendlyName: "Ultrasonic"
      },
    ],
    friendlyName: "Airflow",
    description: "Measure the flow of air though a specified area",
    isController: false,
    isSource: true,
    isCalibratable: true,
    hasDimensions: true,
    multiSensor: true,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_I2C],
    interactionResultTypes: [],
    states: [],
    measurements: measurementTypes,
    // measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
    //   return simpleSummaries(measurement, airflow);
    // },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement,
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_AIRFLOW,
        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_AIRFLOW,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 1000,
    icon: (theme?: "light" | "dark"): string | undefined => {
      return theme === "light" ? PressureDarkIcon : PressureLightIcon;
    }
  };
}
