source("modules/Loading/Loading.R")
source("modules/Units/Units.R")
source("modules/Calibration/Calibration.R")
source("modules/Anomalies/Anomalies.R")
source("modules/Skill/Skill.R")
source("modules/Saving/Saving.R")
source("modules/Visualization/Visualization.R")
source("modules/Indices/Indices.R")
source("modules/Downscaling/Downscaling.R")
source("SUNSET_PROV/R/SunsetProv.Command.R")

library(igraph)
library(rjson)
library(testthat)

#Recipe
recipe_file <- "SUNSET_PROV/tests_provenance/recipes/recipe_test_provenance1.yml"

#Loading recipe
suppressWarnings({invisible(capture.output(
  recipe <- prepare_outputs(recipe_file)
))})

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

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

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

#Anomalies
suppressWarnings({invisible(capture.output(
  data_ano <- Anomalies(recipe, data_cal)
))})

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

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

#Define provenance graph for Visualization
suppressWarnings({invisible(capture.output(
  graph.prov <- skill_metrics$graph.prov
))})

#Visualization
suppressWarnings({invisible(capture.output(
  Visualization(recipe, data_ano, graph.prov,
                skill_metrics = skill_metrics,
                probabilities = probabilities,
                significance = TRUE)
))})º
#Generate JSON file

#graph2json(graph.prov$graph,"/esarchive/scratch/apuiggro/sunset/tests_provenance/out-logs/json_test1.json")

#Make JSON readable
#json_file1 <- fromJSON(file = "tests_provenance/out-logs/json_test1.json")

#======================================

test_that("1. Loading", {

  expect_equal(
    is.list(data$graph.prov),
    TRUE
  )
  expect_equal(
    class(data$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    data$graph.prov$parentnodename$parentnodename_hcst,
    "Interpolation_hcst"
  )
  expect_equal(
    data$graph.prov$parentnodename$parentnodename_obs,
    "DatasetSubset_obs"
  )
  expect_equal(
    !is.null(length(V(data$graph.prov$graph)$name)),
    TRUE
  )
  expect_equal(
    "Variable_hcst" %in% V(data$graph.prov$graph)$name,
    TRUE)

  expect_equal(
    "ds:ECMWF" %in% V(data$graph.prov$graph)$name,
    TRUE)
})

#======================================

test_that("2. Calibration", {

  expect_equal(
    is.list(data_cal$graph.prov),
    TRUE
  )
  expect_equal(
    class(data_cal$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    data_cal$graph.prov$parentnodename$parentnodename_hcst,
    "Calibration_hcst"
  )
  expect_equal(
    data_cal$graph.prov$parentnodename$parentnodename_obs,
    "DatasetSubset_obs"
  )
  expect_equal(
    "Calibration_hcst" %in% V(data_cal$graph.prov$graph)$name, 
    TRUE
  )
  expect_equal(
    "SpatialExtent_Calibration_hcst" %in% V(data_cal$graph.prov$graph)$name,
    TRUE
  )

})

#======================================

test_that("3. Anomalies", {

  expect_equal(
    is.list(data_ano$graph.prov),
    TRUE
  )
  expect_equal(
    class(data_ano$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    data_ano$graph.prov$parentnodename$parentnodename_hcst,
    "Anomalies_hcst"
  )
  expect_equal(
    data_ano$graph.prov$parentnodename$parentnodename_obs,
    "Anomalies_obs"
  )
  expect_equal(
    "Anomalies_hcst" %in% V(data_ano$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Climatology_obs" %in% V(data_ano$graph.prov$graph)$name,
    TRUE
  )
})

#======================================

test_that("4. Skill", {

  expect_equal(
    is.list(skill_metrics$graph.prov),
    TRUE
  )
  expect_equal(
    class(skill_metrics$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    skill_metrics$graph.prov$parentnodename$parentnodename_hcst,
    "Verification_hcst"
  )
  expect_equal(
    skill_metrics$graph.prov$parentnodename$parentnodename_obs,
    "Anomalies_obs"
  )
  expect_equal(
    "Verification_hcst" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Metrics_hcst_crps" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )

})

#======================================

test_that("5. Visualization", {

  expect_equal(
    is.list(graph.prov),
    TRUE
  )
  expect_equal(
    class(graph.prov$graph),
    "igraph"
  )
  expect_equal(
    graph.prov$parentnodename$parentnodename_hcst,
    "Verification_hcst"
  )
  expect_equal(
    "Anomalies_obs" %in% V(graph.prov$graph)$name,
    TRUE
  )
})

#======================================

test_that("6. JSON", {
})
#Here should check that in the directory the is the json.
print("Test 1 successfull")
