# This function returns the day, month and year labels for forecast plots.
# If the data is time-aggregated (time_bounds is not NULL); then the time
# labels reflect the aggregated period, and will include the years to avoid
# displaying incorrect or repetitive information.
# dates: the array of POSIXt Dates in the s2dv_cube
# horizon: The forecast horizon: 'subseasonal', 'seasonal' or 'decadal'.
# init_date: Initialization date as POSIXt object
# start_date: start date 
# i_syear: The forecast year
# time_bounds: the array of POSIXt time_bounds from the s2dv_cube

.get_plot_time_labels <- function(dates, horizon, init_date, start_date,
                                  i_syear, time_bounds = NULL) {
  years <- lubridate::year(Subset(dates, along = 'syear',
             indices = which(start_date == i_syear)))
  if (is.null(attributes(time_bounds))) {
    if (horizon == "subseasonal") {
      time_labels <- dates[1, 1, which(start_date == i_syear), ]
      monday <- ymd_hms(time_labels) - days(wday(ymd_hms(time_labels),
                                                 week_start = 1) - 1)
      sunday <- monday + days(6)
      time_labels <- paste0("Valid from ", format(monday,"%d-%m"), " to ",
                            format(sunday, "%d-%m"), " of ")
    } else {
      time_labels <- lubridate::month(dates[1, 1, which(start_date == i_syear), ],
                                      label = T, abb = F)
    }
  } else {
    if (length(attributes(time_bounds)$plotting_attr) > 1) {
      years <- NULL
      start <- time_bounds$start[1, 1, which(start_date == i_syear), ]
      end <- time_bounds$end[1, 1, which(start_date == i_syear), ]
      time_labels <-
        unlist(lapply(1:length(start), function(i) {
          ftime_ini <- attributes(time_bounds)$plotting_attr$ini[i]
          ftime_end <- attributes(time_bounds)$plotting_attr$end[i]
          # labels for file name:
          ftime <- paste0(ftime_ini, "-", ftime_end)
          # title names:
          if (horizon %in% c("seasonal", "decadal")) {
            # start <- Subset(time_bounds$start,
            #                 along = c("syear", "time"),
            #                 indices = list(syear = i_syear, time = i),
            #                 drop = TRUE)
            # end <- Subset(time_bounds$end,
            #               along = c("syear", "time"),
            #               indices = list(syear = i_syear, time = i),
            #               drop = TRUE)
            year_ini <- year(start[i])
            month_ini <- month.name[month(start[i])]
            year_end <- year(end[i])
            month_end <- month.name[month(end[i])]
            if (year_ini == year_end) {
              toptitle <- paste(month_ini, "to", month_end, year_end)
            } else {
              toptitle <- paste(month_ini, year_ini, "to",
                                month_end, year_end)
            }
          } else if (horizon == "subseasonal") {
            ## set to make ftime_ini be a Monday
            ## if init_week always Thursday, the 2nd addend can be simplified to +4
            ftime_ini <- ymd(init_date) +
                         ((8 - wday(ymd(init_date), week_start = 1)) %% 7) +
                         weeks(ftime_ini - 1)
            ## set to make ftime_end be a Sunday
            ftime_end <- ymd(init_date) +
                         ((8 - wday(ymd(init_date), week_start = 1)) %% 7) +
                         weeks(ftime_end - 1) + 6
            toptitle <- paste("Valid from", ftime_ini, "to", ftime_end)
          }
        }))
    } else {
      time_labels <- attributes(time_bounds)$plotting_attr[[1]]
    }
  }
  return(list(time_labels = time_labels, years = years))
}
