#'Function to create all single system/reference scorecards
#' 
#'@description Scorecards function to create scorecard tables for one system and 
#'  one reference combination (types 1 to 4).
#'@param input_data is an array of spatially aggregated metrics containing the
#'   following dimensions; system, reference, metric, time, sdate, region.
#'@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 output.path a path of the location to save the scorecard plots
#'
#'@return
#' This function returns 4 scorecards images, saved in the directory output.path
#'@examples
#' scorecard_single <- ScorecardsSingle(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/test'
#'                                     )
#'@export
ScorecardsSingle <- function(data, system, reference, var, start.year, end.year,
                             start.months, forecast.months, region.names,
                             metrics, legend.breaks = NULL,
                             table.label = NULL, fileout.label =  NULL,
                             legend.white.space = NULL, 
                             col1.width = NULL, col2.width = NULL,
                             output.path){
  
  ## Checks to apply:
  # First dimension in aggregated_metrics is system and second dimension is reference
  # To allow 1 region - if region = 1 --> only scorecards 1 & 3 need to be plotted
  # If any dimension of input dat is 1, make sure dimension is still present in array
  
  ## Initial checks
  # data
  if (!is.array(data)) {
    stop("Parameter 'data' must be an array with named dimensions.")
  }
  if (!is.array(data)) {
    stop("Parameter 'data' must be an array with named dimensions.")
  }
  if (is.null(names(dim(data)))) {
    stop("Parameter 'data' must have dimenision names.")
  }
  if (!all(c('system','reference','metric','time','sdate','region') %in% 
           names(dim(data)))) {
    stop("Dimension names of 'data' must be: 'system','reference','metric', 
          'time','sdate','region'.")
  }
  if (is.null(table.label)){
    table.label <- ""
  }
  if (is.null(fileout.label)){
    fileout.label <- ""
  }
  
  ## Make sure input_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
  input_data <- Subset(data, along = 'metric', indices = match(metrics, metrics_loaded))
  attributes(input_data)$metrics <- metrics
  
  ## Transform data for scorecards by forecast month (types 3 & 4)
  if(length(start.months) >= length(forecast.months)){
    transformed_data <- SCTransform(data = input_data, 
                                    sdate_dim = 'sdate', 
                                    ftime_dim = 'time')
  }
  
  ## Load configuration files
  sys_dict <- read_yaml("conf/archive.yml")$esarchive
  var_dict <- read_yaml("conf/variable-dictionary.yml")$vars
  
  ## Get scorecards table display names from configuration files
  var.name <- var_dict[[var]]$long_name
  var.units <- var_dict[[var]]$units
  
  ## 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 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]

  ## Legend inputs
  plot.legend = TRUE
  label.scale = 1.4
  legend.width = 555
  legend.height = 50
  
  ## Data display inputs
  round.decimal = 2
  font.size = 1.1
  
  ## Loop over system and reference for each scorecard plot
  for (sys in 1:dim(input_data)['system']) {
    for (ref in 1:dim(input_data)['reference']) {
      
      ## TO DO: Apply check to each scorecard function
      ## check dimension 'metric' exists:
      if (!("metric" %in% names(dim(input_data)))) {
        dim(input_data) <- c(metric = 1, dim(input_data))
      }
      
      ## Find position of mean bias metric to calculate breaks
      breaks_bias <- NULL
      if ('mean_bias' %in% metrics){
        stopifnot(identical(names(dim(Subset(input_data, c('system', 'reference'), list(sys, ref), drop = 'selected'))), c('metric','time','sdate','region')))
        temp_data <- Subset(input_data, c('system', 'reference'), list(sys, ref), drop = 'selected')
        pos_bias <- which(metrics == 'mean_bias')
        if(var == 'psl'){
          temp_data[pos_bias,,,] <- temp_data[pos_bias,,,]/100
        }
        breaks_bias <- .SCBiasBreaks(Subset(temp_data, along = 'metric',
                                            indices = pos_bias))
      }
      
      ## Define breaks for each metric based of metric position:
      legend.breaks <- .met_breaks(metrics, breaks_bias)
      
      ## Put breaks in same order as metrics
      legend.breaks <- legend.breaks[metrics]
      
      ## Get scorecards table display names from configuration files
      system.name <- sys_dict$System[[system[sys]]]$name
      reference.name <- sys_dict$Reference[[reference[ref]]]$name
      
      ## Define scorecard titles
      table.title <- paste0(var.name, " of ", system.name, " ", table.label)
      table.subtitle <- paste0("(Ref: ", reference.name, " ", start.year, "-", end.year, ")")
      
      #############################################################################     
      
      #### Scorecard_type 1 ####
      ## (no transformation or reorder)
      fileout <- .Filename(system = system[sys], reference = reference[ref], var = var,
                           start.year = start.year, end.year = end.year, scorecard.type = 1,
                           fileout.label = fileout.label, output.path = output.path)
      data_sc_1 <- Subset(input_data, c('system', 'reference'), list(sys, ref), drop = 'selected')
      SCPlotScorecard(data = data_sc_1,
                      row.dim = 'region',
                      subrow.dim = 'time',
                      col.dim = 'metric',
                      subcol.dim = 'sdate',
                      legend.dim = 'metric',
                      row.names = region.names,
                      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 = 'Region',
                      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 = col1.width,
                      col2.width = col2.width,
                      fileout = fileout)
      
      
      #### Scorecard_type 2 ####
      ## (reorder only)
      ## Scorecard type 2 is same as type 1 for only one region, therefore is
      ## only plotted if more that one region is requested
      if(dim(input_data)['region'] > 1) {
        fileout <- .Filename(system = system[sys], reference = reference[ref], var = var,
                             start.year = start.year, end.year = end.year, scorecard.type = 2,
                             fileout.label = fileout.label, output.path = output.path)
        new_order <- c('metric', 'region', 'sdate', 'time')
        data_sc_2 <- Reorder(Subset(input_data, c('system', 'reference'), list(sys, ref), drop = 'selected'), new_order)
        SCPlotScorecard(data = data_sc_2,
                        row.dim = 'time',
                        subrow.dim = 'region',
                        col.dim = 'metric',
                        subcol.dim = 'sdate',
                        legend.dim = 'metric',
                        row.names = forecast.months,
                        subrow.names = region.names,
                        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 = 'Region',
                        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 = col2.width,
                        fileout = fileout)
      } ## close if


      #### Scorecard_type 3 ####
      ## (transformation only)
      if(length(start.months) >= length(forecast.months)){
        fileout <- .Filename(system = system[sys], reference = reference[ref], var = var,
                             start.year = start.year, end.year = end.year, scorecard.type = 3,
                             fileout.label = fileout.label, output.path = output.path)
        data_sc_3 <- Subset(transformed_data, c('system', 'reference'), list(sys, ref), drop = 'selected')
        SCPlotScorecard(data = data_sc_3,
                        row.dim = 'region',
                        subrow.dim = 'time',
                        col.dim = 'metric',
                        subcol.dim = 'sdate',
                        legend.dim = 'metric',
                        row.names = region.names,
                        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 = 'Region',
                        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 = col1.width,
                        col2.width = col2.width,
                        fileout = fileout)
      }


      #### Scorecard_type 4 ####
      ## (transformation and reorder)
      ## Scorecard type 4 is same as type 3 for only one region, therefore is
      ## only plotted if more that one region is requested
      if(dim(data)['region'] > 1 & length(start.months) >= length(forecast.months)){
        fileout <- .Filename(system = system[sys], reference = reference[ref], var = var,
                             start.year = start.year, end.year = end.year, scorecard.type = 4,
                             fileout.label = fileout.label, output.path = output.path)
        new_order <- c('metric', 'region', 'sdate', 'time')
        data_sc_4 <- Reorder(Subset(transformed_data, c('system', 'reference'), list(sys, ref), drop = 'selected'), new_order)
        SCPlotScorecard(data = data_sc_4,
                        row.dim = 'time',
                        subrow.dim = 'region',
                        col.dim = 'metric',
                        subcol.dim = 'sdate',
                        legend.dim = 'metric',
                        row.names = forecast.months,
                        subrow.names = region.names,
                        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 = 'Region',
                        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 = col2.width,
                        fileout = fileout)
      } ## close if
      
    } ## close loop on ref
  } ## close loop on sys
  
  print("All single system scorecard plots created")
  
} ## close function      



