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

# 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 <- 'PNG'
recipe$Analysis$Workflow$Visualization$projection <- 'Robinson'
recipe$Analysis$Workflow$Visualization$plots <- 'skill_metrics'

save_fun <- function(...) {
  path <- "./Prova_ARI"
  # paste0(recipe$Run$output_dir, "/plots/ECMWF-SEAS51/ERA5/evmos/tas")
  # "./tests/testthat/_snaps/seasonal_visualization"
  # remove tests/testthat/ when commenting getwd line.
  # recipe$Run$output_dir
  # not: tempfile(fileext = ".png")
  do.call(Visualization, list(...))
  path
}

# normalize_png <- function(file_path) {
#   normalized_path <- tempfile(fileext = ".png")
#   system(paste("convert", shQuote(file_path), "-strip", shQuote(normalized_path)))
#   return(normalized_path)
# }
# 
# save_fun <- function(...) {
#   # Ensure output directory is configured
#   dir.create(recipe$Run$output_dir, showWarnings = FALSE, recursive = TRUE)
#   
#   # Run Visualization to generate plots
#   do.call(Visualization, list(...))
#   
#   # Locate the generated plot file(s)
#   plot_dir <- file.path(recipe$Run$output_dir, "plots/ECMWF-SEAS51/ERA5/evmos/tas")  # Adjust based on structure
#   plot_files <- list.files(plot_dir, pattern = "\\.png$", full.names = TRUE)
#   
#   # Ensure plot file exists
#   if (length(plot_files) == 0) {
#     stop("No plot files found in the output directory!")
#   }
#   
#   normalized_file <- normalize_png(plot_files[1])
#   
#   return(plot_files[1])  # Return the first plot file (or adapt logic if needed)
# }


# expect_equal(recipe$Run$output_dir, "./tests/out-logs/")
# "./tests/out-logs//recipe-seasonal_visualization_20241121152902"
# "./tests/out-logs//recipe-seasonal_visualization_20241120111833"
#  11 21 163701

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'
  )
  
  expect_snapshot_file(
    save_fun(recipe = recipe, data = data, skill_metrics = skill_metrics,
             probabilities = probabilities, significance = 'no'),
    name = 'rpss-june_ft01_sig_no.png'
  )

})

# es crea un png buit amb el nom de name. Després la comparació surt bé perque clar, tot és buit. 

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

# 
# Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
#               probabilities = probabilities, significance = 'mask')

Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities)

Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
              probabilities = probabilities, significance = 'mask')

# Visualization(recipe = recipe, data = data, skill_metrics = skill_metrics,
#               probabilities = probabilities, significance = 'mask')

