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