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 Calcium(subtype: number = 0): ComponentTypeExtension {
  let calcium = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PPB,
    quack.ComponentType.COMPONENT_TYPE_CALCIUM,
    subtype
  );
  return {
    type: quack.ComponentType.COMPONENT_TYPE_CALCIUM,
    subtypes: [],
    friendlyName: "Calcium",
    description: "Measures concentration of calcium",
    isController: false,
    isSource: true,
    isCalibratable: true,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_I2C_WITH_CHANNELS],
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(calcium),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, calcium);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_CALCIUM,
        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_CALCIUM,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 1000
  };
}
