#'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)

dates2load <- function(recipe) {

  temp_freq <- recipe$Analysis$Variables$freq
  Time <- recipe$Analysis$Time
  # hcst dates
  file_dates <- paste0(strtoi(Time$hcst_start):strtoi(Time$hcst_end),
                       Time$sdate)
  
  if (temp_freq == "monthly_mean") {
    file_dates <- .add_dims(file_dates)
  }
 # fcst dates (if fcst_year empty it creates an empty object)
  if (!is.null(Time$fcst_year)) {
    file_dates.fcst <- paste0(Time$fcst_year, Time$sdate)
    if (temp_freq == "monthly_mean") {
      file_dates.fcst <- .add_dims(file_dates.fcst)
    }
  } else {
    file_dates.fcst <- NULL
    info(recipe$Run$logger, retrieve = TRUE,
         "fcst_year empty in the recipe, creating empty fcst object...")
  }
  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)
}
