#' Scorecards spatially transform calculated means
#' 
#'@description Scorecards function to spatially transform the layout of the 
#'calculated metric means, to show 'Target Month' instead of 'Start Date'.
#'
#'@param data A multidimensional array of spatially aggregated data containing 
#'  the following dimensions; system, reference, metric, time, sdate, region.
#'@param sdate_dim A character name referring to the dimension of start date 
#'  in the array aggregated_metrics.
#'@param ftime_dim A character name referring to the dimension of forecast 
#'  time in the array aggregated_metrics.
#'@param ncores An integer indicating the number of cores to use in parallel 
#'  computation. It is NULL by default (1 core).
#'  
#'@example 
#'transformed_data <- SCTransform(data = aggregated_metrics,
#'                                sdate_dim = 'sdate',
#'                                ftime_dim = 'time')
#'@import multiApply
#'@importFrom s2dv Reorder
#'@export 
SCTransform <- function(data,
                        sdate_dim,
                        ftime_dim, 
                        ncores = NULL) {
  
  output <- multiApply::Apply(data = data, 
                              target_dims = c(ftime_dim, sdate_dim),
                              fun = .SCTransform, 
                              ncores = ncores)$output1
  
  return(Reorder(data = output, order = names(dim(data))))
}

.SCTransform <- function(data) {
  output <- data
  n_sdates <- dim(data)[sdate_dim]
  for (i in 2:dim(data)[ftime_dim]) {
    output[i, ] <- data[i, c((n_sdates - i + 2):n_sdates, 1:(n_sdates - i + 1))]
  }
  return(output)
}
