#Currently can only embed the JSON to the metrics graphs and the forcast ensemble.P
#Provenance not defined for most_likely_terciles!
library(stringr)

prov_visualization <- function(recipe, data, graph, type, outdirs) {
  #Define parameters
  plots <- strsplit(recipe$Analysis$Workflow$Visualization$plots, ", | |,")[[1]]
  i <- switch(type,
              "fcst" = "parentnodename_fcst",
              "hcst" = "parentnodename_hcst")
  parentnodename <- graph$parentnodename[[i]]
  graph <- graph$graph
  graph.full <- graph

  #Skill metrics graph nodes
  if ("skill_metrics" %in% plots) {
    measure <- unlist(strsplit(recipe$Analysis$Workflow$Skill$metric, " "))
    nodenames <- list()
    for (names in measure){
      graph.plot <- graph
      names <- tolower(names)
      graph.nodename <- paste0("Graph_", type, "_", names)
      label.name <- names
      graph.path <- list()

      for (i in 1:length(outdirs)) {
        if (names %in% as.list(unlist(strsplit(outdirs[[i]], "[/-]"))) ||
              names %in% as.list(unlist(strsplit(outdirs[[i]], "[/.]")))) {
          graph.path <- append(graph.path, outdirs[[i]]) #All graph paths
        }
      }
      #Graphical representation node
      if (!is.null(recipe$Analysis$Workflow$Visualization$projection)) {
        projection <- recipe$Analysis$Workflow$Visualization$projection
      }else {
        projection <- "Cylindrical equidistant"
      }
      graph.plot <-
        add_vertices(graph.plot,
                     nv = 1,
                     name = graph.nodename,
                     label = paste0(label.name, " ",
                                    "Graphical representation"),
                     className = "go:Map",
                     description = "A Map class object",
                     attr = list("go:hasProjection" = projection,
                                 "prov:AtLocation" =
                                   paste(unlist(graph.path),
                                         collapse = " ")))

      #Link graph node with Skill that plots
      graph.plot <- add_edges(graph.plot,
                         c(getNodeIndexbyName(graph.plot,
                                              paste0("Metrics_", type,
                                                     "_", names)),
                           getNodeIndexbyName(graph.plot, graph.nodename)),
                         label = "go:hadGraphicalRepresentation")

      nodenames <- append(nodenames, list(graph.nodename))

      graph.plot <-
        prov_Command(graph.plot, package = "Visualization",
                           version = "2.0.0",
                           fun = "modules/Visualization/Visualization.R",
                           arg.list =
                           list(
                                "Recipe",
                                "Data"),
                           origin.node.name
                           = c(
                               unlist(
                                      graph.nodename)))

      #Embbed provenance to the generated image files
      for (paths in graph.path) {
        path.json <- paste0(str_sub(paths, 1, -4), "json")
        graph2json(graph.plot, path.json)
        embedJSON(paths, path.json, paths)
      }

      graph.full <- my_union_graph(graph.full, graph.plot)
    }
  }

  #Forecast ensemble graph node
  if (!is.null(data$fcst)) {
    if ("forecast_ensemble_mean" %in% plots) {
      graph.nodename <- paste0("Graph_", "forecast_ensemble")
      graph.path <- list()
      for (i in 1:length(outdirs)) {
        if ("forecast_ensemble_mean"
            %in%
              as.list(unlist(strsplit(outdirs[[i]], "[/-]")))) {
          graph.path <- append(graph.path, outdirs[[i]])
        }
      }
      graph.plot <- graph
      graph.plot <-
        add_vertices(graph.plot,
                     nv = 1,
                     name = graph.nodename,
                     label = paste0("Forecast Ensemble",
                                    "Graphical representation"),
                     className = "go:Map",
                     attr = list("prov:AtLocation" =
                                   paste(unlist(graph.path),
                                         collapse = " ")))


      #Find the latest node for fcst (before computing Skills)
      known_names <- list("Anomalies_fcst", "Calibration_fcst",
                       "DatasetSubset_fcst", "Interpolation_fcst")
      max_index <- -1
      max_name <- NULL
    
      for (name in known_names) {
          if (name %in% V(graph)$name) {
            node_index <- which(V(graph)$name == name)
          if (node_index > max_index) {
              max_index <- node_index
              max_name <- name
          }
          }
      }
      lastnode <- max_name
      graph.plot <- add_edges(graph.plot,
                              c(getNodeIndexbyName(graph.plot, lastnode),
                                getNodeIndexbyName(graph.plot, graph.nodename)),
                              label = "go:hadGraphicalRepresentation")

      nodenames <- append(nodenames, list(graph.nodename))

      graph.plot <-
        prov_Command(graph.plot, package = "Visualization",
                           version = "2.0.0",
                           fun = "modules/Visualization/Visualization.R",
                           arg.list =
                           list(
                                "Recipe",
                                "Data"),
                           origin.node.name
                           = c(
                               unlist(
                                      graph.nodename)))   
      for (paths in graph.path) {
        graph2json(graph.plot, path.json)
        embedJSON(paths, path.json, paths)
      }
      graph.full <- my_union_graph(graph.plot, graph.full)
    }
  }
  return(list("graph" = graph.full,
              "parentnodename" = parentnodename, "nodenames" = nodenames))

}
