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

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

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

suppressWarnings({invisible(capture.output(
  recipe <- prepare_outputs(recipe_file, disable_checks = F )
))})

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

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

#Downscale the data
suppressWarnings({invisible(capture.output(
  downscaled_data <- Downscaling(recipe, data_ano)
))})

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

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


#Make JSON readable
#json_file3 <- fromJSON(file = "tests_provenance/out-logs/json_test3.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,
    "DatasetSubset_obs"
  )
  expect_equal(
    !is.null(length(V(data$graph.prov$graph)$name)),
    TRUE
  )
  expect_equal(
    "VerticalExtent_hcst" %in% V(data$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Location_Dataset_hcst" %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(
    "Climatology_obs" %in% V(data_ano$graph.prov$graph)$name,
    TRUE
  )
})

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


test_that("3. Downscaling", {

  expect_equal(
    is.list(downscaled_data$graph.prov),
    TRUE
  )
  expect_equal(
    class(downscaled_data$graph.prov$graph),
    "igraph"
  )
  expect_equal(
    downscaled_data$graph.prov$parentnodename$parentnodename_hcst,
    "Downscaling_hcst"
  )
  expect_equal(
    downscaled_data$graph.prov$parentnodename$parentnodename_obs,
    "Anomalies_obs"
  )
  expect_equal(
    "SpatialExtent_Downscaling_hcst" %in% V(downscaled_data$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(
    "Metrics_hcst_rpss"  %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )
  expect_equal(
    "Metrics_hcst_mean_bias" %in% V(skill_metrics$graph.prov$graph)$name,
    TRUE
  )

})

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

test_that("5. JSON", {
})

print("Test 3 successfull")