#'Function to create all multi system/reference scorecards
#' 
#'@description Scorecards function to create scorecard tables for multiple systems
#' and references (types 9 to 12).
#'@param data is an array of spatially aggregated metrics containing the
#'   following dimensions; system, reference, metric, time, sdate, region.
#'@param sign is an array with the same dimensions as data indicting the 
#'   significance of the metrics, with either true, false or null.
#'@param system a vector of character strings defining the systems following the
#'   archive.yml format from verification suite 
#'@param reference a vector of character strings defining the references 
#'  following the archive.yml format from verification suite 
#'@param var a character string following the format from 
#'  variable-dictionary.yml from verification suite 
#'@param start.year a numeric indicating the start year of the reference period
#'@param end.year a numeric indicating the end year of the reference period
#'@param start.date a vector of character strings indicating the start months
#'@param forecast.month a vector of numeric indicating the forecast months
#'@param region.names a vector of character strings containing names of the 
#'  regions corresponding to the input data
#'@param metrics a vector of character strings containing the metrics.
#'@param table.label a character string containing additional information to 
#'  include in the scorecard title
#'@param fileout.label a character string containing additional information to 
#'  include in the output png file when saving the scorecard.
#'@param plot.legend A logical value to determine if the legend is plotted.
#'@param legend.breaks A vector of numerics or a list of vectors of numerics,
#'  containing the breaks for the legends. If a vector is given as input, then 
#'  these breaks will be repeated for each legend.dim. A list of vectors can be 
#'  given as input if the legend.dims require different breaks. This parameter 
#'  is required even if the legend is not plotted, to define the colors in the 
#'  scorecard table.
#'@param legend.white.space A numeric value defining the initial starting 
#'  position of the legend bars, the white space infront of the legend is 
#'  calculated from the left most point of the table as a distance in cm.
#'@param legend.width A numeric value to define the width of the legend bars.
#'@param legend.height A numeric value to define the height of the legend bars.
#'@param label.scale A numeric value to define the size of the legend labels.
#'@param col1.width A numeric value defining the width of the first table column
#'  in cm.
#'@param col2.width A numeric value defining the width of the second table 
#'  column in cm.
#'@param columns.width A numeric value defining the width all columns within the 
#'  table in cm (excluding the first and second columns containing the titles).
#'@param font.size A numeric indicating the font size on the scorecard table.
#'@param round.decimal A numeric indicating to which decimal point the data 
#'  is to be displayed in the scorecard table. Default is 2.
#'@param output.path a path of the location to save the scorecard plots.
#'
#'@return
#' This function returns 4 scorecard images for each region requested, the 
#' images are saved in the directory output.path.

#'@example
#' scorecard_multi <- ScorecardsMulti(data = aggregated_metrics,
#'                                    system.name = c('ECMWF-SEAS5','DWD-GFCS2.1'),
#'                                    reference.name = 'ERA5',
#'                                    var = 'tas', 
#'                                    start.year = 1993,
#'                                    end.year = 2016,
#'                                    start.months = 1:12,
#'                                    forecast.months = 1:6,
#'                                    region.names = c('Tropics', 'Extra-tropical NH', 'Extra-tropical SH')
#'                                    metrics = c('mean_bias', 'enscorr', 'rpss','crpss', 'enssprerr'),
#'                                    table.label = '(Interpolation = to system, Aggregation level = skill, Cross-validation = terciles)',
#'                                    fileout.label = '_crossval-terciles_agg-skill',
#'                                    output.path = '/esarchive/scratch/nmilders/scorecards_images/testing'
#'                                    )


ScorecardsMulti <- function(data, sign, system, reference, var, start.year, 
                            end.year, start.months, forecast.months, 
                            region.names, metrics, plot.legend = TRUE,
                            legend.breaks = NULL, legend.white.space = NULL, 
                            legend.width = 555, legend.height = 50,
                            table.label = NULL, fileout.label =  NULL,
                            label.scale =  1.4, font.size = 1.1, 
                            col1.width = NULL, col2.width = NULL, 
                            columns.width = NULL,
                            round.decimal = 2, output.path){

  ## Checks to apply:
  # first dimension in aggregated_metrics is system and second dimension is reference
  # either multi-system and one reference, or multi-reference and one system
  
  ## Initial checks
  if (is.null(table.label)){
    table.label <- ""
  }
  if (is.null(fileout.label)){
    fileout.label <- ""
  }
  
  ## Make sure data is in correct order for using in functions:
  data_order <- c('system','reference','metric','time','sdate','region')
  data <- Reorder(data, data_order)

  ## Identify metrics loaded
  metrics_loaded <-  attributes(data)$metrics
  
  ## Select only the metrics to visualize from data
  data <- Subset(data, along = 'metric', indices = match(metrics, metrics_loaded))
  attributes(data)$metrics <- metrics
  
  if(!is.null(sign)){
    sign <- Reorder(sign, data_order)
    sign <- Subset(sign, along = 'metric', indices = match(metrics, metrics_loaded))
    attributes(sign)$metrics <- metrics
  }
  

  ## Transform data for scorecards by forecast month (types 11 & 12)
  if(length(start.months) >= length(forecast.months)){
    
    transformed_data <- SCTransform(data = data,
                                    sdate_dim = 'sdate',
                                    ftime_dim = 'time')
    
    if(!is.null(sign)){
      transformed_sign <- SCTransform(data = sign, 
                                      sdate_dim = 'sdate', 
                                      ftime_dim = 'time')
    } else {
      transformed_sign <- NULL
    }
  }
  
  ## Load configuration files
  if (is.null(recipe$Run$filesystem)) {
    filesystem <- 'esarchive'
  } else {
    filesystem <- recipe$Run$filesystem
  }
  sys_dict <- read_yaml("conf/archive.yml")[[filesystem]]
  var_dict <- read_yaml("conf/variable-dictionary.yml")$vars

  ## Get scorecards table display names from configuration files
  var.name <- var_dict[[var]]$long_name
  
  if ('name' %in% names(recipe$Analysis$Variables)){
    if (recipe$Analysis$Variables$name == var) {
      var.units <- recipe$Analysis$Variables$units
    }
  } else {
    for (i in 1:length(recipe$Analysis$Variables)) {
      if (recipe$Analysis$Variables[[i]]$name == var) { 
        var.units <- recipe$Analysis$Variables[[i]]$units 
      }
    }
  }
  if (is.null(var.units)) {
    var.units <- var_dict[[var]]$units
  }

  system.name <-  NULL
  reference.name <- NULL
  
  for(sys in 1:length(system)){
    system.name1 <- sys_dict$System[[system[sys]]]$name 
    system.name <- c(system.name, system.name1)
  }
  for(ref in 1:length(length)){
    reference.name1 <- sys_dict$Reference[[reference[ref]]]$name
    reference.name <- c(reference.name, reference.name1)
  }
  
  if("Multimodel" %in%  system ){
    system.name <- c(system.name, "Multimodel")
  }
  
  ## Get metric long names
  metric.names.list <- .met_names(metrics, var.units)
  
  ## format the metric names as character instead of list
  for(met in metrics){
    if(met == metrics[1]){
      metric.names <- metric.names.list[[met]] 
    } else {
      metric.names <- c(metric.names, metric.names.list[[met]])
    }
  }
  
  ## Define parameters depending on Multi-system or Multi-reference
  if(length(system) > 1 && length(reference) == 1){
    model <- 'system'
    table.model.name <- 'System'
    model.name <- system.name
    eval.label <- 'Ref'
    eval.name <- reference.name
    eval.filename <- reference
  } else if(length(system) == 1 && length(reference) > 1){
    model <- 'reference'
    table.model.name <- 'Reference'
    model.name <- reference.name
    eval.label <- 'Sys'
    eval.name <- system.name
    eval.filename <- system
  } else {stop('Not multi system or multi reference')}
  
  ## Define table colors 
  palette <- c('#2D004B', '#542789', '#8073AC', '#B2ABD2', '#D8DAEB', '#FEE0B6', '#FDB863', '#E08214', '#B35806', '#7F3B08')
  colorunder <- "#04040E"
  colorsup <- "#730C04"
  
  ## Legend lower limit color
  legend.col.inf <- .legend_col_inf(metrics, colorunder)
  legend.col.inf <- legend.col.inf[metrics]
  
  ## Legend upper limit color
  legend.col.sup <- .legend_col_sup(metrics, colorsup)
  legend.col.sup <- legend.col.sup[metrics]

  ## Loop over region 
  for(reg in 1:length(region.names)){
    
    breaks_bias <- NULL
    
    ## Find position of mean bias metric to calculate breaks
    if ('mean_bias' %in% metrics) {
      pos_bias <- which(metrics == 'mean_bias')
      if(var == 'psl'){
        data[,,pos_bias,,,] <- data[,,pos_bias,,,]/100 ## temporary
      }
      breaks_bias <- .SCBiasBreaks(Subset(data, along = c('metric','region'), 
                                          indices = list(pos_bias,reg)))
    }
    
    ## Define breaks for each metric based of metric position:
    legend.breaks <- .met_breaks(metrics, breaks_bias)
    
    ## Define scorecard titles
    table.title <- paste0(var.name, ".  Region: ", region.names[reg], " ", table.label)
    table.subtitle <- paste0("(", eval.label, ": ", eval.name, " ", start.year, "-", end.year, ")")
    
    
      #### Scorecard_type 9 ####
      ## (no transformation or reorder)
      fileout <- .Filename(model = model, eval.name = eval.filename, var = var, 
                           start.year = start.year, end.year = end.year, scorecard.type = 9, 
                           region =  sub(" ", "-", region.names[reg]),
                           fileout.label = fileout.label, output.path = output.path)
    
    if(model == 'system'){
      data_sc_9 <- Subset(data, c('reference','region'), list(1, reg), drop = 'selected')
      if(!is.null(sign)){
        sign_sc_9 <- Subset(sign, c('reference','region'), list(1, reg), drop = 'selected')
      } else {
        sign_sc_9 <- NULL
      }
    } else if(model == 'reference'){
      data_sc_9 <- Subset(data, c('system','region'), list(1, reg), drop = 'selected')
      if(!is.null(sign)){
        sign_sc_9 <- Subset(sign, c('system','region'), list(1, reg), drop = 'selected')
      } else {
        sign_sc_9 <- NULL
      }
    }
    
    VizScorecard(data = data_sc_9,
                 sign = sign_sc_9,
                 row_dim = model,
                 subrow_dim = 'time',
                 col_dim = 'metric',
                 subcol_dim = 'sdate',
                 legend_dim = 'metric',
                 row_names = model.name,
                 subrow_names = forecast.months,
                 col_names = metric.names,
                 subcol_names = month.abb[as.numeric(start.months)],
                 table_title = table.title,
                 table_subtitle = table.subtitle,
                 row_title = table.model.name,
                 subrow_title = 'Forecast Month',
                 col_title = 'Start date',
                 legend_breaks = legend.breaks,
                 plot_legend = plot.legend,
                 label_scale = label.scale,
                 legend_width = legend.width,
                 legend_height = legend.height,
                 palette = palette,
                 colorunder = legend.col.inf,
                 colorsup = legend.col.sup,
                 round_decimal = round.decimal,
                 font_size = font.size,
                 legend_white_space = legend.white.space, 
                 col1_width = 4,
                 col2_width = col2.width,
                 columns_width = columns.width,
                 fileout = fileout)
      
      
      #### Scorecard_type 10 ####
      ## (reorder only) 
      fileout <- .Filename(model = model, eval.name = eval.filename, var = var, 
                           start.year = start.year, end.year = end.year, scorecard.type = 10, 
                           region =  sub(" ", "-", region.names[reg]),
                           fileout.label = fileout.label, output.path = output.path)
      
      new_order <- c('system', 'reference', 'metric', 'region','sdate', 'time')
      
      if(model == 'system'){
        data_sc_10 <- Subset(Reorder(data, new_order), c('reference','region'), list(1, reg), drop = 'selected')
        if(!is.null(sign)){
          sign_sc_10 <- Subset(Reorder(sign, new_order), c('reference','region'), list(1, reg), drop = 'selected')
        } else {
          sign_sc_10 <- NULL
        }
      } else if(model == 'reference'){
        data_sc_10 <- Subset(Reorder(data, new_order), c('system','region'), list(1, reg), drop = 'selected')
        if(!is.null(sign)){
          sign_sc_10 <- Subset(Reorder(sign, new_order), c('system','region'), list(1, reg), drop = 'selected')
        } else {
          sign_sc_10 <- NULL
        }
      }
      VizScorecard(data = data_sc_10,
                   sign = sign_sc_10,
                   row_dim = 'time',
                   subrow_dim = model,
                   col_dim = 'metric',
                   subcol_dim = 'sdate',
                   legend_dim = 'metric',
                   row_names = forecast.months,
                   subrow_names = model.name,
                   col_names = metric.names,
                   subcol_names = month.abb[as.numeric(start.months)],
                   table_title = table.title,
                   table_subtitle = table.subtitle,
                   row_title = 'Forecast month',
                   subrow_title = table.model.name,
                   col_title = 'Start date',
                   legend_breaks = legend.breaks,
                   plot_legend = plot.legend,
                   label_scale = label.scale,
                   legend_width = legend.width,
                   legend_height = legend.height,
                   palette = palette,
                   colorunder = legend.col.inf,
                   colorsup = legend.col.sup,
                   round_decimal = round.decimal,
                   font_size = font.size,
                   legend_white_space = legend.white.space, 
                   col1_width = col1.width,
                   col2_width = 4,
                   columns_width = columns.width,
                   fileout = fileout)
      
  
      #### Scorecard_type 11 ####
      ## (transformation only)
      if(length(start.months) >= length(forecast.months)){
      
        fileout <- .Filename(model = model, eval.name = eval.filename, var = var, 
                             start.year = start.year, end.year = end.year, scorecard.type = 11, 
                             region =  sub(" ", "-", region.names[reg]),
                             fileout.label = fileout.label, output.path = output.path)
        
        if(model == 'system'){
          data_sc_11 <- Subset(transformed_data, c('reference','region'), list(1, reg), drop = 'selected')
          if(!is.null(sign)){
            sign_sc_11 <- Subset(transformed_sign, c('reference','region'), list(1, reg), drop = 'selected')
          } else {
            sign_sc_11 <- NULL
          }
        } else if(model == 'reference'){
          data_sc_11 <- Subset(transformed_data, c('system','region'), list(1, reg), drop = 'selected')
          if(!is.null(sign)){
            sign_sc_11 <- Subset(transformed_sign, c('system','region'), list(1, reg), drop = 'selected')
          } else {
            sign_sc_11 <- NULL
          }
        }
        
        VizScorecard(data = data_sc_11,
                     sign = sign_sc_11,
                     row_dim = model,
                     subrow_dim = 'time',
                     col_dim = 'metric',
                     subcol_dim = 'sdate',
                     legend_dim = 'metric',
                     row_names = model.name,
                     subrow_names = forecast.months,
                     col_names = metric.names,
                     subcol_names = month.abb[as.numeric(start.months)],
                     table_title = table.title,
                     table_subtitle = table.subtitle,
                     row_title = table.model.name,
                     subrow_title = 'Forecast Month',
                     col_title = 'Target month',
                     legend_breaks = legend.breaks,
                     plot_legend = plot.legend,
                     label_scale = label.scale,
                     legend_width = legend.width,
                     legend_height = legend.height,
                     palette = palette,
                     colorunder = legend.col.inf,
                     colorsup = legend.col.sup,
                     round_decimal = round.decimal,
                     font_size = font.size,
                     legend_white_space = legend.white.space, 
                     col1_width = 4,
                     col2_width = col2.width,
                     columns_width = columns.width,
                     fileout = fileout)
      }
      
      #### Scorecard_type 12 ####
      ## (transformation and reorder)
      if(length(start.months) >= length(forecast.months)){
        fileout <- .Filename(model = model, eval.name = eval.filename, var = var, 
                             start.year = start.year, end.year = end.year, scorecard.type = 12, 
                             region =  sub(" ", "-", region.names[reg]),
                             fileout.label = fileout.label, output.path = output.path)
        
        new_order <- c('system', 'reference', 'metric', 'region','sdate', 'time')
        
        if(model == 'system'){
          data_sc_12 <- Subset(Reorder(transformed_data, new_order), c('reference','region'), list(1, reg), drop = 'selected')
          if(!is.null(sign)){
            sign_sc_12 <- Subset(Reorder(transformed_sign, new_order), c('reference','region'), list(1, reg), drop = 'selected')
          } else {
            sign_sc_12 <- NULL
          }
        } else if(model == 'reference'){
          data_sc_12 <- Subset(Reorder(transformed_data, new_order), c('system','region'), list(1, reg), drop = 'selected')
          if(!is.null(sign)){
            sign_sc_12 <- Subset(Reorder(transformed_sign, new_order), c('system','region'), list(1, reg), drop = 'selected')
          } else {
            sign_sc_12 <- NULL
          }
        }
        
        VizScorecard(data = data_sc_12,
                     sign = sign_sc_12,
                     row_dim = 'time',
                     subrow_dim = model,
                     col_dim = 'metric',
                     subcol_dim = 'sdate',
                     legend_dim = 'metric',
                     row_names = forecast.months,
                     subrow_names = model.name,
                     col_names = metric.names,
                     subcol_names = month.abb[as.numeric(start.months)],
                     table_title = table.title,
                     table_subtitle = table.subtitle,
                     row_title = 'Forecast Month',
                     subrow_title = table.model.name,
                     col_title = 'Target month',
                     legend_breaks = legend.breaks,
                     plot_legend = plot.legend,
                     label_scale = label.scale,
                     legend_width = legend.width,
                     legend_height = legend.height,
                     palette = palette,
                     colorunder = legend.col.inf,
                     colorsup = legend.col.sup,
                     round_decimal = round.decimal,
                     font_size = font.size,
                     legend_white_space = legend.white.space, 
                     col1_width = col1.width,
                     col2_width = 4,
                     columns_width = columns.width,
                     fileout = fileout)
      }

  } ## close loop on region  
      
  print("All multi scorecard plots created")
  
} ## close function      

