#'Read requested dates from recipe and return array of file dates to be loaded
#'
#'The purpose of this function is to read the recipe configuration data for 
#'Auto-S2S workflows, retrieve the start and end dates for the hindcast and
#'the forecast dates, and return two arrays containing the requested dates, to
#'be passed to the Start() call. If no fcst date is provided, it returns an
#'empty object.
#'
#'@param recipe Auto-S2S configuration recipe as returned by read_yaml()
#'@param logger object of class logger containing log output file information
#'
#'@return a list of two arrays containing file dates for hcst and fcst
#'
#'@export

library(lubridate)
source("modules/Loading/R/subseas_file_dates.R")

dates2load <- function(recipe, logger) {
# from recipe it uses:
# temporal frequency of the variable requested
# the details in $Time request: hcst period, forecast times ...
  temp_freq <- recipe$Analysis$Variables$freq
  recipe <- recipe$Analysis$Time
  if (temp_freq == "monthly_mean") {
   # hcst dates
    file_dates <- paste0(strtoi(recipe$hcst_start):strtoi(recipe$hcst_end),
                         recipe$sdate)
    file_dates <- .add_dims(file_dates)
  } else if (temp_freq == "weekly_mean") {
    sday <- recipe$sday_window
    if (is.null(sday)) {
      sday <- 3
    }
    n.skill.weeks <- recipe$sweek_window
    if (n.skill.weeks %% 2 != 0) {
      fcst_sweek_ind <- (n.skill.weeks + 1)/2
    } else {
      fcst_sweek_ind <- (n.skill.weeks)/2
    }
    fcst.sdate <- recipe$sdate
    if (fcst_sweek_ind != 1) {
      if (fcst_sweek_ind %% 2 == 0) {
        fcst.sdate_to_start <- as.character(format(as.Date(as.character(fcst.sdate),
                   format = "%Y%m%d") + 4 + 7 * ((fcst_sweek_ind/2) - 1), "%Y%m%d"))
      } else {
        fcst.sdate_to_start <- as.character(format(as.Date(as.character(fcst.sdate),
                   format = "%Y%m%d") + 7 *((fcst_sweek_ind - 1)/2), "%Y%m%d"))
      }
    } else {
      fcst.sdate_to_start <- startdate
    }
    file_dates <- subseas_file_dates(startdate = fcst.sdate_to_start, 
                                     n.skill.weeks = n.skill.weeks, 
                                     n.days = sday,
                                     hcst.start = as.numeric(recipe$hcst_start),
                                     hcst.end = as.numeric(recipe$hcst_end),
                                     ftime_min = recipe$ftime_min,
                                     ftime_max = recipe$ftime_max, out = 'hcst')
  } else {
    file_dates <- paste0(strtoi(recipe$hcst_start):strtoi(recipe$hcst_end),
                       recipe$sdate)
  }
  # fcst dates (if fcst_year empty it creates an empty object)
  if (! is.null(recipe$fcst_year)) {
    if (temp_freq == "monthly_mean") {
      file_dates.fcst <- paste0(recipe$fcst_year, recipe$sdate)
      file_dates.fcst <- .add_dims(file_dates.fcst)
    } else if (temp_freq == "weekly_mean") {
      file_dates.fcst <- subseas_file_dates(startdate = fcst.sdate_to_start,
                                   n.skill.weeks = n.skill.weeks,
                                   n.days = sday,
                                   hcst.start = as.numeric(recipe$hcst_start),
                                   hcst.end = as.numeric(recipe$hcst_end),
                                   ftime_min = recipe$ftime_min,
                                   ftime_max = recipe$ftime_max, out = 'fcst')
    } else {
      file_dates.fcst <- paste0(recipe$fcst_year, recipe$sdate)
    }
  } else {
    # if no fcst year is requested:
    file_dates.fcst <- NULL
  }
  return(list(hcst = file_dates, fcst = file_dates.fcst))
  ## TODO: document header of fun
}

# adds the correspondent dims to each sdate array
.add_dims <- function(data) {

  default_dims <- c(sday = 1, sweek = 1, syear = length(data))
  default_dims[names(dim(data))] <- dim(data)
  dim(data) <- default_dims
  return(data)
}
