# Seasonal monthly data

setwd("/home/Earth/abatalla/SUNSET_RS_GIT/sunset")
# setwd("/esarchive/scratch/vagudets/repos/auto-s2s/")

source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Calibration/Calibration.R")
source("modules/Skill/Skill.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")


recipe_file <- "tests/recipes/recipe-seasonal_visualization.yml"
recipe <- prepare_outputs(recipe_file, uniqueID = TRUE, disable_checks = FALSE)

# Load datasets
suppressWarnings({invisible(capture.output(
  data <- Loading(recipe)
))})

# Units transformation
suppressWarnings({invisible(capture.output(
  data <- Units(recipe, data)
))})

# Calibrate data
suppressWarnings({invisible(capture.output(
  data <- Calibration(recipe, data)
))})

# Compute skill metrics
suppressWarnings({invisible(capture.output(
  skill_metrics <- Skill(recipe, data)
))})

# Compute percentiles and probability bins
suppressWarnings({invisible(capture.output(
  probabilities <- Probabilities(recipe, data)
))})


# ------- TESTS --------

# for local testing:
# library(thestthat)
# testthat::test_file("tests/testthat/test-seasonal_visualization.R")
Sys.setenv(TESTTHAT_EDITION = "3")

recipe$Analysis$Workflow$Visualization$file_format <- 'PNG' # 'PDF'
## recipe$Analysis$Workflow$Visualization$projection <- 'Robinson'
## recipe$Analysis$Workflow$Visualization$plots <- 'skill_metrics'
## folder_timestamp <- sub(".*_(\\d+)$", "\\1", recipe$Run$output_dir)

# path <- file.path(recipe$Run$output_dir, "plots", "ECMWF-SEAS51", "ERA5", "evmos", "tas")
Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'dots')

save_fun <- function(...) {
  path <- file.path(recipe$Run$output_dir, "plots", "ECMWF-SEAS51", "ERA5", "evmos", "tas")
  ## "tests/out-logs/recipe-seasonal_visualization_/plots/ECMWF-SEAS51/ERA5/evmos/tas" # local
  do.call(Visualization, list(...))
  return(path)
}

retorn_save_fun  <- save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
                             probabilities = probabilities, significance = 'dots')


test_that("2. Output checks", {

  expect_snapshot_file(
    path = paste0(save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
                  probabilities = probabilities, significance = 'dots'),
                  '/rpss-june_ft01_dots.pdf'),
    # path = path,
    name = 'rpss-june_ft01_dots.pdf'
  )
  
  # expect_snapshot_file(
  #   save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
  #            probabilities = probabilities, significance = 'mask'),
  #   name = 'rpss-june_ft01_mask.png'
  # )
  # 
  # expect_snapshot_file(
  #   save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
  #            probabilities = probabilities, significance = FALSE),
  #   name = 'rpss-june_ft01_sig_no.png'
  # )

})


# ## TODO: check size of file. 
# test_that("1. Visualization", {
#   plots <- file.path(recipe$Run$output_dir, "plots", "ECMWF-SEAS51", "ERA5", "evmos", "tas")
#   expect_equal(
#     all(basename(list.files(plots, recursive = T)) %in%
#           c("forecast_ensemble_mean-20230601_ft01.pdf", 
#             "forecast_most_likely_tercile-20230601_ft01.pdf",
#             "forecast_most_likely_tercile-20230601_rpssmask_ft01.pdf",
#             "rpss-june_ft01_dots.pdf", "rpss-june_ft01_mask.pdf",
#             "rpss-june_ft01.pdf")),
#     TRUE
#   )
#   # expect_equal(
#   #   length(list.files(plots, recursive = T)),
#   #   6
#   # )
#   
# })
