## TODO: Separate by time aggregation
## TODO: Build a default path that accounts for:
## variable, system, reference, start date and region name

get_filename <- function(dir, recipe, var, date, agg, file.type) {
  # This function builds the path of the output file based on directory,
  # variable, forecast date, startdate, aggregation, forecast horizon and
  # type of metric/forecast/probability.
  
  if (tolower(recipe$Analysis$Horizon) == "subseasonal") {
    shortdate <- format(as.Date(as.character(date), "%Y%m%d"), "%V")
    dd <- "week"
  } else {
    shortdate <- format(as.Date(as.character(date), "%Y%m%d"), "%m")
    dd <- "month"
  }

  if (tolower(recipe$Analysis$Horizon) == "decadal") {
    # to not save the month and day in the filename (needed for the multimodel)
    date <- substr(date, 1, 4)
    # for the models initialised in January - it may be better to do this in save_* functions
    archive <- read_yaml(paste0("conf/archive_decadal.yml"))$esarchive
    exp.name <- recipe$Analysis$Datasets$System$name
    if (exp.name != 'Multimodel' && archive$System[[exp.name]]$initial_month == 1) {
      date <- as.character(as.numeric(date) - 1)
    }
  }
  
  switch(tolower(agg),
	 "region" = {gg <- "-region"},
         "global" = {gg <- ""})

  system <- gsub('.','', recipe$Analysis$Datasets$System$name, fixed = T)
  reference <- gsub('.','', recipe$Analysis$Datasets$Reference$name, fixed = T)

  if (tolower(recipe$Analysis$Output_format) == 'scorecards') {
    # Define output dir name accordint to Scorecards format
    dict <- read_yaml("conf/output_dictionaries/scorecards.yml")
    # Get necessary names
    hcst_start <- recipe$Analysis$Time$hcst_start
    hcst_end <- recipe$Analysis$Time$hcst_end

    switch(tolower(file.type),
	   "skill" = {type_info <- "-skill"},
	   "corr"  = {type_info <- "-corr"},
	   "exp"   = {type_info <- paste0("_", date)},
	   "obs"   = {type_info <- paste0("-obs_", date)},
	   "percentiles" = {type_info <- "-percentiles"},
	   "probs" = {type_info <- paste0("-probs_", date)},
	   "bias"  = {type_info <- paste0("-bias_", date)},
           "rps_syear" = {type_info <- "_rps_syear"},
           "rps_clim_syear" = {type_info <- "_rps_clim_syear"},
           "crps_syear" = {type_info <- "_crps_syear"},
           "crps_clim_syear" = {type_info <- "_crps_clim_syear"},
           "crps" = {type_info <- "_crps"},
           "mean_bias" = {type_info <- "_mean_bias"},
	   {type_info <- paste0("_", file.type)})

    # Build file name
    filename <- paste0("scorecards_", system, "_", reference, "_", var,
                       type_info, "_", hcst_start, "-", hcst_end,
                       "_s", shortdate)
  } else {
    if (tolower(recipe$Analysis$Horizon) == "decadal") {
      shortdate_aux <- ''
    } else {
      shortdate_aux <- paste0("_", dd, shortdate)
    }
    switch(tolower(file.type),
           {filename <- paste0(var, gg, "-", tolower(file.type), shortdate_aux)},
  	   "skill" = {filename <- paste0(var, gg, "-skill", shortdate_aux)},
  	   "corr"  = {filename <- paste0(var, gg, "-corr", shortdate_aux)},
  	   "exp"   = {filename <- paste0(var, gg, "_", date)},
  	   "obs"   = {filename <- paste0(var, gg, "-obs_", date)},
  	   "percentiles" = {filename <- paste0(var, gg, "-percentiles", shortdate_aux)},
  	   "probs" = {filename <- paste0(var, gg, "-probs_", date)},
  	   "bias"  = {filename <- paste0(var, gg, "-bias_", date)})
  }
  return(file.path(dir, paste0(filename, ".nc")))
}

