source('tools/libs.R')
source('modules/Scorecards/Scorecards.R')

args = commandArgs(trailingOnly = TRUE)
recipe_file <- args[1]
output_dir <- args[2]

## TODO: Replace with function
# Read recipe and set outdir
recipe <- read_yaml(recipe_file)
recipe$Run$output_dir <- output_dir

## Loop over variables
datasets <- recipe$Analysis$Datasets
## TODO: Improve dependency system?

## TODO: Correctly define region condition
for (system in 1:length(datasets$System)) {
  for (reference in 1:length(datasets$Reference)) {
    for (variable in 1:length(recipe$Analysis$Variables)) {
      for (region in 1:length(recipe$Analysis$Region)) {
        scorecard_recipe <- recipe 
        scorecard_recipe$Analysis$Datasets$System <- 
          recipe$Analysis$Datasets$System[[system]]
        scorecard_recipe$Analysis$Datasets$Reference <- 
          recipe$Analysis$Datasets$Reference[[reference]]
        scorecard_recipe$Analysis$Variables <-
          recipe$Analysis$Variables[[variable]]
        ## TODO: is this necessary?
        scorecard_recipe$Analysis$Region <-
          recipe$Analysis$Region[[region]]
        # Plot Scorecards
        Scorecards(scorecard_recipe)
      }
    }
  }
}
print("##### SCORECARDS SAVED TO THE OUTPUT DIRECTORY #####")
