"use strict";

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
 */
export const drawColumnChartBasedOnSelectedAggregationOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  // Daily / sum
  if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Daily"
  ) {
    const [
      formattedObservationsSumDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailySumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsSumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Monthly / sum
  else if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    const [
      formattedObservationsSumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlySumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsSumMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Daily / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Formatted observations and metadata for chart - daily / maximum
    const [
      formattedObservationsMaximumDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailyMaximumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsMaximumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Monthly / maximum
  else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    const [
      formattedObservationsMaximumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyMaximumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsMaximumMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Daily / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Formatted observations and metadata for chart - daily / minimum
    const [
      formattedObservationsMinimumDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailyMinimumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsMinimumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Monthly / minimum
  else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    const [
      formattedObservationsMinimumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyMinimumObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsMinimumMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Daily / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Daily"
  ) {
    const [
      formattedObservationsAverageDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailyAverageObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsAverageDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
  // Monthly / average
  else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Monthly"
  ) {
    const [
      formattedObservationsAverageMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyAverageObservations(
      uniqueCalendarDatesNestedArr,
      observationsNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawColumnChartHighcharts(
      formattedObservationsAverageMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
};
