import {
  AreaChartData,
  ComponentTypeExtension,
  GraphFilters,
  LineChartData,
  simpleAreaChartData,
  simpleLineChartData,
  simpleMeasurements,
  simpleSummaries,
  Subtype,
  Summary,
  unitMeasurementSummaries
} from "pbHelpers/ComponentType";
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
import { quack } from "protobuf-ts/quack";
import PowerDarkIcon from "assets/components/powerDark.png";
import PowerLightIcon from "assets/components/powerLight.png";
import { convertedUnitMeasurement } from "models/UnitMeasurement";
import { pond } from "protobuf-ts/pond";

export function Power(subtype: number = 0): ComponentTypeExtension {
  let voltage = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_VOLTAGE,
    quack.ComponentType.COMPONENT_TYPE_POWER,
    subtype
  );
  let charged = describeMeasurement(
    quack.MeasurementType.MEASUREMENT_TYPE_PERCENT,
    quack.ComponentType.COMPONENT_TYPE_POWER,
    subtype
  );
  return {
    type: quack.ComponentType.COMPONENT_TYPE_POWER,
    subtypes: [
      {
        key: quack.PowerSubtype.POWER_SUBTYPE_NONE,
        value: "POWER_SUBTYPE_NONE",
        friendlyName: "Battery"
      } as Subtype,
      {
        key: quack.PowerSubtype.POWER_SUBTYPE_NO_BATTERY,
        value: "POWER_SUBTYPE_NO_BATTERY",
        friendlyName: "Power Supply"
      } as Subtype
    ],
    friendlyName: "Power",
    description: "Measures the power source for a device (voltage and charge percent)",
    isController: false,
    isSource: true,
    isCalibratable: true,
    addressTypes: [quack.AddressType.ADDRESS_TYPE_POWER],
    interactionResultTypes: [],
    states: [],
    measurements: simpleMeasurements(voltage, charged),
    measurementSummary: async function(measurement: quack.Measurement): Promise<Array<Summary>> {
      return simpleSummaries(measurement, voltage, charged);
    },
    unitMeasurementSummary: (
      measurements: convertedUnitMeasurement,
    ): Summary[] => {
      return unitMeasurementSummaries(
        measurements,
        quack.ComponentType.COMPONENT_TYPE_POWER,
        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_POWER,
        measurement,
        smoothingAverages,
        filters
      );
    },
    minMeasurementPeriodMs: 1000,
    icon: (theme?: "light" | "dark"): string | undefined => {
      return theme === "light" ? PowerDarkIcon : PowerLightIcon;
    }
  };
}
