# Given an startdate e.g. 20240104
# The number of weeks that will be used for metrics calculation
# The number of days that will be used for calibration
# the hindcast period and the forecast times
# It returns an array with dimension sday, sweek, syear
# for the hcst is out = 'hcst' or for the forecast if out = 'fcst'

subseas_file_dates <- function(startdate, n.skill.weeks, n.days,
                               hcst.start, hcst.end,
                               ftime_min, ftime_max, out) {
  # Generate the sday_window vectors:
  ## Only for Thursdays and Mondays 
  ## Create a diagonal matrix of 3 and 4 days matching Mondays/Thursdays
  ## To be substracted to startdate in the loop below 
  prev_sday <- t(sapply(1:((n.days + 1)/2 - 1), function(x) {
     if (x %% 2 == 0) {
       res <- rep(4, (n.days + 1)/2 - 1)
     } else {
       res <- rep(3, (n.days + 1)/2 - 1)
     } 
     return(res)}))
  prev_sday[lower.tri(prev_sday)] <- 0
  
  next_sday <- t(sapply(1:((n.days + 1)/2 - 1), function(x) {
     if (x %% 2 == 0) {
       res <- rep(3, (n.days + 1)/2 - 1)
     } else {
       res <- rep(4, (n.days + 1)/2 - 1)
     } 
     return(res)}))
  next_sday[lower.tri(next_sday)] <- 0

  if (n.days > 1) {
    prev_sday <- colSums(prev_sday)
    next_sday <- colSums(next_sday)
    thrusday_win <- c(rev(prev_sday) * -1, 0, next_sday)
    monday_win <- c(rev(next_sday) * -1, 0, prev_sday)
  } else {
    thrusday_win <- monday_win <- 0
  }
  #### END Generation sdya_window vectors

  ftime_min <- as.numeric(substr(as.character(startdate), 1, 4)) - hcst.end
  ftime_max <- as.numeric(substr(as.character(startdate), 1, 4)) - hcst.start
  startdate <- as.Date(toString(startdate), "%Y%m%d")
  sdates <- numeric(0)
  while (length(sdates) < n.skill.weeks){
    if (format(startdate, "%a") == "Thu" || format(startdate, "%a") == "Mon") {
      sdates <- c(sdates, format(startdate, "%Y%m%d"))
    }
    startdate <- startdate - 1
  }

  file_dates <- array(numeric(), c(0, n.days, (ftime_max -ftime_min) + 1))
  file_dates.fcst <- array(numeric(), c(0, n.days, (ftime_max - ftime_min) + 1))
  for (sdate in sdates) {
    ## TODO: take into account year of the fcst to select hcst years
    fcst.day <- substr(sdate, 7, 8)
    fcst.month <- substr(sdate, 5, 6)
    fcst.year <- substr(sdate, 1, 4)

    startdate <- as.Date(toString(sdate), "%Y%m%d")

    # makes sure of giving a: [M T M || T M T] combination
    if(format(startdate, "%a") == "Thu") {
         hcst.sdays <- startdate + thrusday_win
#        hcst.sdays <- c(startdate - 3, startdate, startdate + 4, recursive = T)
     } else {
         hcst.sdays <- startdate + monday_win
#        hcst.sdays <- c(startdate - 4, startdate, startdate + 3, recursive = T)
     }
    hcst.sdays <- format(as.Date(hcst.sdays,"%Y%m%d"), "%Y%m%d")

    sdays.file_dates <- array(numeric(),c(0, (ftime_max - ftime_min) + 1))
    sdays.file_dates.fcst <- array(hcst.sdays, c(n.days, 
                                   (ftime_max - ftime_min) + 1))
    for (sday.sdate in hcst.sdays) {
      sday.year <- substr(sday.sdate, 1, 4)
      sday.mday <- substr(sday.sdate, 5, 8)
      sday.years <- paste((strtoi(sday.year)- ftime_max):
                                  (strtoi(sday.year) - ftime_min), sep = "")
        sday.dates <- apply(expand.grid(sday.years, sday.mday), 
                            1, paste, collapse = "")
        sdays.file_dates <- abind(sdays.file_dates, sday.dates, along = 1)
    }

    names(dim(sdays.file_dates)) <- c('sday','syear')
    file_dates <- abind(file_dates, sdays.file_dates, along = 1)
    file_dates.fcst <- abind(file_dates.fcst, sdays.file_dates.fcst, along = 1)

  }
  names(dim(file_dates)) <- c('sweek','sday','syear')
  names(dim(file_dates.fcst)) <- c('sweek','sday','syear')

#  dir_dates <- paste0(file_dates.fcst,"/",var,"_",file_dates)
#  dim(dir_dates) <- dim(file_dates)
#  names(dim(dir_dates)) <- c('sweek','sday','syear')
#  dir_dates <- substr(dir_dates,10,24)
#  return(dir_dates)
  if (out == 'fcst') {
    central_day <- (n.days + 1)/2
    central_week <- (n.skill.weeks + 1)/2
    file_dates <- file_dates.fcst[central_week,central_day, 1]
    dim(file_dates) <- c(sday = 1, sweek = 1, syear = 1)
  }
  file_dates <- Reorder(file_dates, c('sday', 'sweek', 'syear'))
  return(file_dates)
}
