import {
  ComponentTypeExtension,
  Summary,
  simpleMeasurements,
  simpleSummaries,
  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 Weight(subtype: number = 0): ComponentTypeExtension {
  let weight = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_WEIGHT,
    quack.ComponentType.COMPONENT_TYPE_WEIGHT,
    subtype
  );
  return {
    type: quack.ComponentType.COMPONENT_TYPE_WEIGHT,
    subtypes: [],
    friendlyName: "Weight",
    description: "Measures weight in grams",
    isController: false,
    isSource: true,
    isCalibratable: false,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_INCREMENTAL],
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(weight),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, weight);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement,
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_WEIGHT,
        subtype,
      );
    },
    lineChartData: (
      measurement: pond.UnitMeasurementsForComponent,
      smoothingAverages?: number,
      filters?: GraphFilters
    ): LineChartData => {
      return simpleLineChartData(
        quack.ComponentType.COMPONENT_TYPE_WEIGHT,
        measurement,
        smoothingAverages,
        filters
      );
    },
    areaChartData: (
      measurement: pond.UnitMeasurementsForComponent,
      smoothingAverages?: number,
      filters?: GraphFilters
    ): AreaChartData => {
      return simpleAreaChartData(measurement, smoothingAverages, filters);
    },
    minMeasurementPeriodMs: 1000
  };
}
