"use strict";

import { formatSensorThingsApiResponseForLineOrColumnChart } from "./chartHelpers.mjs";

import { extractPropertiesFromFormattedDatastreamMetadata } from "./fetchedDataProcessing.mjs";

import { drawLineChartHighcharts } from "./chartLine.mjs";

import {
  calculateAndFormatDailySumObservations,
  calculateAndFormatMonthlySumObservations,
} from "./dropDownListAggregationSum.mjs";

import {
  calculateAndFormatDailyMaximumObservations,
  calculateAndFormatMonthlyMaximumObservations,
} from "./dropDownListAggregationMaximum.mjs";

import {
  calculateAndFormatDailyMinimumObservations,
  calculateAndFormatMonthlyMinimumObservations,
} from "./dropDownListAggregationMinimum.mjs";

import {
  calculateAndFormatDailyAverageObservations,
  calculateAndFormatMonthlyAverageObservations,
} from "./dropDownListAggregationAverage.mjs";

/**
 * Draw a line chart based on the selected aggregation options from a drop-down list
 *
 * @param {String} selectedAggregationType A string representing the selected aggregation type. The currently supported strings include `Sum`, `Maximum`, `Minimum` and `Average`
 * @param {String} selectedAggregationDuration A string representing the selected aggregation duration. The currently supported strings include `Daily` and `Monthly`
 * @param {Array} observationsNestedArr An array made up of sub-array(s) of observations
 * @param {String} selectedSamplingRateAbbrev A string representing the abbreviated form of the selected sampling rate option
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {undefined} undefined
 */
const drawLineChartBasedOnSelectedAggregationOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  // Daily / sum
  if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Note: The `drawColumChart` function expects two arguments,
    // these are obtained by using the spread operator on the
    // result returned from the `calculateAndFormat...` functions
    drawLineChartHighcharts(
      ...calculateAndFormatDailySumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / sum
  else if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatMonthlySumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatDailyMaximumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatMonthlyMaximumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatDailyMinimumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatMonthlyMinimumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatDailyAverageObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawLineChartHighcharts(
      ...calculateAndFormatMonthlyAverageObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
};

/**
 * Draw a line chart based on the selected options from a drop-down list.
 * This chart type supports both raw observations and aggregated observations
 *
 * @param {String} selectedAggregationType The selected aggregation type. The currently supported strings include `Sum`, `Maximum`, `Minimum` and `Average`
 * @param {String} selectedAggregationDuration The selected aggregation duration. The currently supported strings include `Daily` and `Monthly`
 * @param {Array} observationsNestedArr An array made up of sub-array(s) of observations
 * @param {String} selectedSamplingRateAbbrev The abbreviated form of the selected sampling rate option
 * @param {Array} uniqueCalendarDatesNestedArr An array made up of sub-array(s) of unique calendar date(s) string(s)
 * @param {Array} formattedMetadataNestedArr An array of sub-arrays of formatted metadata properties
 * @returns {undefined} undefined
 */
export const drawLineChartBasedOnSelectedOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  // Raw observations
  if (selectedAggregationType === "None (raw data)") {
    // Create formatted array(s) for observations
    const formattedRawObservationsLineChartNestedArr =
      observationsNestedArr.map((observationsArr) =>
        formatSensorThingsApiResponseForLineOrColumnChart(observationsArr)
      );

    // Extract formatted metadata properties
    const rawObsExtractedFormattedDatastreamProperties =
      extractPropertiesFromFormattedDatastreamMetadata(
        formattedMetadataNestedArr,
        false
      );

    drawLineChartHighcharts(
      formattedRawObservationsLineChartNestedArr,
      rawObsExtractedFormattedDatastreamProperties
    );
  }
  // Aggregated observations
  else {
    drawLineChartBasedOnSelectedAggregationOptions(
      selectedAggregationType,
      selectedAggregationDuration,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      formattedMetadataNestedArr
    );
  }
};
