"use strict";

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

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

import { drawColumnChartHighcharts } from "./chartColumn.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 column 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 drawColumnChartBasedOnSelectedAggregationOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  // Daily / sum
  if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Note: The `drawColumnChart` function expects two arguments,
    // these are obtained by using the spread operator on the
    // result returned from the `calculateAndFormat...` functions
    drawColumnChartHighcharts(
      ...calculateAndFormatDailySumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / sum
  else if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatMonthlySumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatDailyMaximumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatMonthlyMaximumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatDailyMinimumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatMonthlyMinimumObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Daily / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Daily"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatDailyAverageObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
  // Monthly / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Monthly"
  ) {
    drawColumnChartHighcharts(
      ...calculateAndFormatMonthlyAverageObservations(
        uniqueCalendarDatesNestedArr,
        observationsNestedArr,
        selectedSamplingRateAbbrev,
        formattedMetadataNestedArr
      )
    );
  }
};

/**
 * Draw a column 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 drawColumnChartBasedOnSelectedOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  // Case 1: Raw observations
  if (selectedAggregationType === "None (raw data)") {
    // Create formatted array(s) for observations
    const formattedRawObservationsColumnChartNestedArr =
      observationsNestedArr.map((observationsArr) =>
        formatSensorThingsApiResponseForLineOrColumnChart(observationsArr)
      );

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

    drawColumnChartHighcharts(
      formattedRawObservationsColumnChartNestedArr,
      rawObsExtractedFormattedDatastreamProperties
    );
  }
  // Case 2: Aggregated observations
  else {
    drawColumnChartBasedOnSelectedAggregationOptions(
      selectedAggregationType,
      selectedAggregationDuration,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      uniqueCalendarDatesNestedArr,
      formattedMetadataNestedArr
    );
  }
};
