source("provenance/R/prov_skill.R")

prov_Skill <- function(recipe, graph) {
  #fcst definition (Dataset+Datasubset)
  if (!is.null(graph$parentnodename$indicesnodenames)) {
    Indices <- TRUE #If True the last step was Indices calculation
  }else {
    Indices <- FALSE
  }

  if (!is.null(data$fcst)) {
    graph_fcst <- prov_skill(recipe, data, graph, type = "fcst",
                             Indices)
  }
  #obs definition (Dataset+Datasubset)
  graph_obs <- prov_skill(recipe, data, graph, type = "obs",
                          Indices)

  #hcst definition (Dataset+Datasubset)
  graph_hcst <- prov_skill(recipe, data, graph, type = "hcst",
                           Indices)

  #Command Call and output definition
  if (!is.null(data$fcst)) {
    graph <- my_union_graph(graph_fcst$graph, graph_hcst$graph)

    graph <- prov_Command(graph, package = "Skill",
                                version = "2.0.0",
                                fun = "modules/Skill/Skill.R",
                                arg.list =
                                  list(
                                       "Recipe",
                                       "Data"),
                                origin.node.name =
                                  list(graph_fcst$parentnodename,
                                       graph_hcst$parentnodename))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list(
                         "parentnodename_fcst" = graph_fcst$parentnodename,
                         "parentnodename_hcst" = graph_hcst$parentnodename,
                         "parentnodename_obs" = graph_obs$parentnodename,
                         "indicesnodenames" = graph_hcst$indicesnodenames))
  } else {
    graph <- graph_hcst$graph
    graph <- prov_Command(graph, package = "Skill",
                                version = "2.0.0",
                                fun = "modules/Skill/Skill.R",
                                arg.list =
                                  list(
                                       "Recipe",
                                       "Data"),
                                origin.node.name =
                                  list(graph_hcst$parentnodename))

    graph <- list("graph" = graph,
                  "parentnodename" =
                    list("parentnodename_hcst" = graph_hcst$parentnodename,
                         "parentnodename_obs" = graph_obs$parentnodename,
                         "indicesnodenames" = graph_hcst$indicesnodenames))

  }

  message("INFO  ", "[", Sys.time(), "]",
          "Provenance correctly generated for the Skill module")

  #Name of the generated JSON according to recipe values
  trimmed_dir <- dirname(dirname(recipe$Run$output_dir))

  main_dir <- paste0(trimmed_dir, "/outputs", "/provenance/")

  recipe_model <- paste0("sys-",
                         gsub('\\.', '', recipe$Analysis$Datasets$System$name))
  recipe_split <- paste0("_ref-", recipe$Analysis$Datasets$Reference$name,
                         "_var-", recipe$Analysis$Variables$name,
                         "_reg-", recipe$Analysis$Region$name,
                         "_sdate-", recipe$Analysis$Time$sdate)

  json_name <- paste0("main_provenance", "_", recipe_model, recipe_split)

  #Convert from igraph to JSON format and store in the output directory
  graph2json(graph$graph, output.file = paste0(main_dir,
                                               json_name,  ".json"))

  return(graph)

}