# leadtimes: list 2 arrays, 'start' and 'end', containing the time bounds for
#   each leadtime
# sdate: start date in POSIXt/POSIXct format
# calendar: name of the calendar in string format

.get_times <- function(recipe, data_cube, sdate, calendar, init_date) {

  # Define list to contain metadata for time and time_bnds
  times <- list()
  # Compute initial date
  fcst.horizon <- tolower(recipe$Analysis$Horizon)
  # Generate time dimensions and the corresponding metadata.
  ## TODO: This addresses subseasonal case, but does not work well
  ## when there is missing data.
  for (aux_dim in c("sweek", "sday")) {
    if (aux_dim %in% names(dim(data_cube$attrs$Dates))) {
    central_value <- (dim(data_cube$attrs$Dates)[[aux_dim]] + 1) / 2
    data_cube$attrs$Dates <- Subset(data_cube$attrs$Dates,
                                    along = c(aux_dim),
                                    indices = list(central_value))
    }
  }
  dates <- as.PCICt(ClimProjDiags::Subset(data_cube$attrs$Dates, 'syear', 1),
                    cal = calendar)
  leadtimes <- as.numeric(dates - as.PCICt(init_date, cal = calendar))/3600
    
  switch(fcst.horizon,
         "seasonal" = {time <- leadtimes; ref <- 'hours since ';
                       stdname <- paste(strtoi(leadtimes), collapse=", ")},
         "subseasonal" = {time <- seq(7, 7*dim(data_cube$attrs$Dates)[['time']], by = 7);
                          ref <- 'days since ';
                          stdname <- ''},
         "decadal" = {time <- leadtimes; ref <- 'hours since ';
                      stdname <- paste(strtoi(leadtimes), collapse=", ")})
  dim(time) <- length(time)
  sdate <- as.Date(sdate, format = '%Y%m%d') # reformatting
  metadata <- list(time = list(units = paste0(ref, sdate, 'T00:00:00'),
                               calendar = calendar))
  attr(time, 'variables') <- metadata
  names(dim(time)) <- 'time'
  times$time <- time
  # Generate time_bnds dimensions and the corresponding metadata.
  if (!is.null(data_cube$attrs$time_bounds)) {
    time_bounds <- lapply(data_cube$attrs$time_bounds,
                          function(x) {
                            y <- as.PCICt(ClimProjDiags::Subset(x,
                                                                along = 'syear',
                                                                indices = 1,
                                                                drop = 'non-selected'),
                                          cal = calendar)
                            y <- as.numeric(y - as.PCICt(init_date, cal = calendar))/3600
                            return(y)
                          })
    # Generates time_bnds dimensions and the corresponding metadata.
    time_bnds <- abind(time_bounds, along = 0)
    names(dim(time_bnds)) <- c("bnds", "time")
    sdate <- as.Date(sdate, format = '%Y%m%d') # reformatting
    metadata <- list(time_bnds = list(units = paste0(ref, sdate, 'T00:00:00'),
                                      calendar = calendar,
                                      long_name = "time bounds"))
    attr(time_bnds, 'variables') <- metadata
    attr(time, 'variables')$bounds <- "time_bounds"
    times$time_bnds <- time_bnds
  }
  return(times)
}

