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)

#json_file <- fromJSON(file = "tests_provenance/out-logs/json_test2.json")

# Read recipe
recipe_file <- "tests_provenance/recipes/recipe_test_provenance2.yml"

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

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

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

#Compute El Nino indices
suppressWarnings({invisible(capture.output(
  nino_data <- Indices(recipe, data_ano)
))})

#Compute skill metrics
graph.prov <- nino_data$graph.prov
nino_data_prov <- nino_data #To use for the unit test
nino_data$graph.prov <- NULL

suppressWarnings({invisible(capture.output(
  for (index in nino_data) {
    index$graph.prov <- graph.prov
    nino_skill_metrics <- Skill(recipe = recipe, data = index, agg = "region")
    graph.prov <- nino_skill_metrics$graph.prov
    nino_skill_metrics$graph.prov <- NULL
  }
))})

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

#Make JSON readable
json_file2 <- fromJSON(file = "tests_provenance/out-logs/json_test2.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(
    !is.null(length(V(data$graph.prov$graph)$name)),
    TRUE
  )
  expect_equal(
    "Realization_hcst_3" %in% V(data$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "ds:Copernicus" %in% V(data$graph.prov$graph)$name,
    TRUE
  )
})

test_that("2. 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_obs" %in% V(data_ano$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Anomalies_hcst" %in% V(data_ano$graph.prov$graph)$name,
    TRUE
  )
})

test_that("3. Indices", {

  expect_equal(
    is.list(nino_data_prov$graph.prov),
    TRUE
  )
  expect_equal(
    class(nino_data_prov$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    nino_data_prov$graph.prov$parentnodename$parentnodename_hcst,
    "Anomalies_hcst"
  )
  expect_equal(
    nino_data_prov$graph.prov$parentnodename$parentnodename_obs,
    "Anomalies_obs"
  )
  expect_equal(
    length(nino_data_prov$graph.prov$parentnodename$indicesnodenames),
    8
  )
  expect_equal(
    "ClimateIndex_obs_3" %in% V(nino_data_prov$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "ClimateIndex_hcst_4" %in% V(nino_data_prov$graph.prov$graph)$name,
    TRUE
  )
})

test_that("4. Skill", {

  expect_equal(
    is.list(graph.prov),
    TRUE
  )
  expect_equal(
    class(graph.prov$graph),
    "igraph"
  )
  expect_equal(
    graph.prov$parentnodename$parentnodename_hcst,
    "Verification_hcst_2"
  )
  expect_equal(
    graph.prov$parentnodename$parentnodename_obs,
    "ClimateIndex_obs_4"
  )
  expect_equal(
    "Metrics_hcst_enscorr_4" %in% V(graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Verification_hcst_4" %in% V(graph.prov$graph)$name,
    TRUE
  )

})

test_that("6. JSON", {

})

print("Test 2 succesfull")