###########################################

source("modules/Loading/Loading.R")
source("modules/Calibration/Calibration.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)

recipe_file <- "SUNSET_PROV/tests_provenance/recipes/recipe_test_provenance5.yml"

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

archive <- read_yaml("conf/archive_decadal.yml")

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

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

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

suppressWarnings({invisible(capture.output(
  probs <- Probabilities(recipe, data_cal)
))})

graph.prov <- skill_metrics$graph.prov

# Plotting
suppressWarnings({invisible(capture.output(
  Visualization(recipe = recipe, data = data_cal, graph.prov,
                skill_metrics = skill_metrics, probabilities = probs,
                significance = T )
))})

#Make JSON readable
#json_file5 <- fromJSON(file = "tests_provenance/out-logs/json_test5.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(
    "VerticalExtent_fcst" %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(
    "SpatialExtent_Calibration_fcst" %in% V(data_cal$graph.prov$graph)$name,
    TRUE
  )

})

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

test_that("3. 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,
    "Interpolation_obs"
  )
  expect_equal(
    skill_metrics$graph.prov$parentnodename$parentnodename_fcst,
    "Verification_fcst"
  )
  expect_equal(
    "Verification_fcst" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Verification_hcst" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )

})

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

test_that("4. 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,
    "Interpolation_obs"
  )
   expect_equal(
    graph.prov$parentnodename$parentnodename_fcst,
    "Verification_fcst"
  )

})

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

test_that("5. JSON", {

})

print("Test 5 successfull")