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?
for (system in 1:length(datasets$System)) {
  for (reference in 1:length(datasets$Reference)) {
    for (variable in 1:length(recipe$Analysis$Variables)) {
	info(recipe$Run$logger,
         paste0("System",recipe$Analysis$Datasets$System[[system]]))
        info(recipe$Run$logger,
         paste0("Reference",  recipe$Analysis$Datasets$Reference[[reference]]))
	 info(recipe$Run$logger,
         paste0("Variable", recipe$Analysis$Variables[[variable]])

      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]]
      # Plot Scorecards
      Scorecards(scorecard_recipe)
    }
  }
}
print("##### SCORECARDS SAVED TO THE OUTPUT DIRECTORY #####")
