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("SUNSET_PROV/R/SunsetProv.Command.R")

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

#SUNSET loading module execution
recipe_file <- "SUNSET_PROV/tests_provenance/recipes/recipe_test_provenance4.yml"

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

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

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

#Anomalies MODULE
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
graph.prov <- skill_metrics$graph.prov


#Visualization
suppressWarnings({invisible(capture.output(
Visualization(recipe, data, 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_test4.json")

#Make JSON readable
#json_file4 <- fromJSON(file = "tests_provenance/out-logs/json_test4.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,
    "DatasetSubset_hcst"
  )
  expect_equal(
    data$graph.prov$parentnodename$parentnodename_obs,
    "Interpolation_obs"
  )
  expect_equal(
    data$graph.prov$parentnodename$parentnodename_fcst,
    "DatasetSubset_fcst"
  )
  expect_equal(
    !is.null(length(V(data$graph.prov$graph)$name)),
    TRUE
  )
  expect_equal(
    "Variable_fcst" %in% V(data$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "ModellingCenter" %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,
    "Interpolation_obs"
  )
  expect_equal(
    data_cal$graph.prov$parentnodename$parentnodename_fcst,
    "Calibration_fcst"
  )
  expect_equal(
    "Calibration_fcst" %in% V(data_cal$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "VerticalExtent_obs" %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(
    data_ano$graph.prov$parentnodename$parentnodename_fcst,
    "Anomalies_fcst"
  )
  expect_equal(
    "Anomalies_obs" %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(
    skill_metrics$graph.prov$parentnodename$parentnodename_fcst,
    "Verification_fcst"
  )
  expect_equal(
    "Metrics_fcst_bss10" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Verification_fcst" %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(
    graph.prov$parentnodename$parentnodename_obs,
    "Anomalies_obs"
  )
   expect_equal(
    graph.prov$parentnodename$parentnodename_fcst,
    "Verification_fcst"
  )

})

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

test_that("6. JSON", {

})

print("Test 4 successfull")