# Seasonal monthly data

## To remove:
## expect_equal(getwd(), "/home/Earth/abatalla/SUNSET_RS_GIT/sunset")
setwd("/home/Earth/abatalla/SUNSET_RS_GIT/sunset")

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, disable_checks = F, uniqueID = F)

# 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 --------

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

recipe$Analysis$Workflow$Visualization$file_format <- 'SVG'
recipe$Analysis$Workflow$Visualization$projection <- 'Robinson'
recipe$Analysis$Workflow$Visualization$plots <- 'skill_metrics'

library(vdiffr)
library(usethis)
library(svglite)
# usethis::use_package(package = "vdiffr", type = "Suggests")
# usethis::use_package(package = "testthat", type = "Suggests")

save_fun <- function(...) {
  path <- file.path(recipe$Run$output_dir, "plots", "ECMWF-SEAS51", "ERA5", "evmos", "tas") # recipe$Run$output_dir # not: tempfile(fileext = ".png")
  svglite::svglite(path)# paste0(path, "/rpss-june_ft01_mask.svg")) # "prova_Ari.svg"
  do.call(Visualization, list(...))
  dev.off()
  return(path)
}

test_that("Visualization test", {
  
  vdiffr::expect_doppelganger("rpss-june_ft01_mask.svg", 
                              paste0(save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
                                              probabilities = probabilities, significance = 'dots'),
                                     '/rpss-june_ft01_dots.svg'))
  
})

test_that("2. Output checks", {
  expect_doppelganger("rpss-june_ft01_mask.svg", function() {
    Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
                  probabilities = probabilities, significance = 'dots')
  })
})



# 
# test_that("1. Output checks", {
#   
#   expect_snapshot_file(
#     save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
#              probabilities = probabilities, significance = 'dots'),
#     name = 'rpss-june_ft01_dots.png'
#   )
#   
#   expect_snapshot_file(
#     save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
#              probabilities = probabilities, significance = 'mask'),
#     name = 'rpss-june_ft01_mask.png'
#   )
#   
# })
