import {
  ComponentTypeExtension,
  Summary,
  simpleSummaries,
  simpleMeasurements,
  unitMeasurementSummaries,
  AreaChartData,
  GraphFilters,
  simpleAreaChartData,
  LineChartData,
  simpleLineChartData
} 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 AirQuality(subtype: number = 0): ComponentTypeExtension {
  let tvoc = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PPB,
    quack.ComponentType.COMPONENT_TYPE_AIR_QUALITY,
    subtype
  );
  let eco2 = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PPM,
    quack.ComponentType.COMPONENT_TYPE_AIR_QUALITY,
    subtype
  );
  return {
    type: quack.ComponentType.COMPONENT_TYPE_AIR_QUALITY,
    subtypes: [
      {
        key: quack.AirQualitySubtype.AIR_QUALITY_SUBTYPE_SGP30,
        value: "AIR_QUALITY_SUBTYPE_SGP30",
        friendlyName: "Air Quality (SGP30)"
      }
    ],
    friendlyName: "Air Quality",
    description: "Measures the air quality based on metal oxide elements",
    isController: false,
    isSource: true,
    isCalibratable: false,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_I2C],
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(tvoc, eco2),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, tvoc, eco2);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement,
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_AIR_QUALITY,
        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_AIR_QUALITY,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 1000
  };
}
