"use strict";

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

import {
  calculateAndFormatDailySumObservations,
  calculateAndFormatMonthlySumObservations,
  calculateAndFormatDailyMaximumObservations,
  calculateAndFormatMonthlyMaximumObservations,
  calculateAndFormatDailyMinimumObservations,
  calculateAndFormatMonthlyMinimumObservations,
  calculateAndFormatDailyAverageObservations,
  calculateAndFormatMonthlyAverageObservations,
} from "./dropDownListProcessing.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} observationsAggregationNestedArr An array made up of sub-array(s) of aggregated 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 drawLineChartBasedOnSelectedAggregationOptions = function (
  selectedAggregationType,
  selectedAggregationDuration,
  observationsAggregationNestedArr,
  selectedSamplingRateAbbrev,
  uniqueCalendarDatesNestedArr,
  formattedMetadataNestedArr
) {
  if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Formatted observations and metadata for chart - daily / sum
    const [
      formattedObservationsSumDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailySumObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawLineChartHighcharts(
      formattedObservationsSumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  } else if (
    selectedAggregationType === "Sum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    // Formatted observations and metadata for chart - monthly / sum
    const [
      formattedObservationsSumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlySumObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

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

    drawLineChartHighcharts(
      formattedObservationsMaximumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  } else if (
    selectedAggregationType === "Maximum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    // Formatted observations and metadata for chart - monthly / maximum
    const [
      formattedObservationsMaximumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyMaximumObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

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

    drawLineChartHighcharts(
      formattedObservationsMinimumDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  } else if (
    selectedAggregationType === "Minimum" &&
    selectedAggregationDuration === "Monthly"
  ) {
    // Formatted observations and metadata for chart - monthly / minimum
    const [
      formattedObservationsMinimumMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyMinimumObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawLineChartHighcharts(
      formattedObservationsMinimumMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  } else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Daily"
  ) {
    // Formatted observations and metadata for chart - daily / average
    const [
      formattedObservationsAverageDailyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatDailyAverageObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawLineChartHighcharts(
      formattedObservationsAverageDailyNestedArr,
      extractedFormattedDatastreamProperties
    );
  } else if (
    selectedAggregationType === "Average" &&
    selectedAggregationDuration === "Monthly"
  ) {
    // Formatted observations and metadata for chart - monthly / average
    const [
      formattedObservationsAverageMonthlyNestedArr,
      extractedFormattedDatastreamProperties,
    ] = calculateAndFormatMonthlyAverageObservations(
      uniqueCalendarDatesNestedArr,
      observationsAggregationNestedArr,
      selectedSamplingRateAbbrev,
      formattedMetadataNestedArr
    );

    drawLineChartHighcharts(
      formattedObservationsAverageMonthlyNestedArr,
      extractedFormattedDatastreamProperties
    );
  }
};
