#' @title Generate provenance for Visualization computation
#' @description Generate provenance information for Visualization module in SUNSET based on forecast verification.
#' @param recipe Configuration file specifying parameters. 
#' @param data s2dv_cube object generated by SUNSET's Loading.R function.
#' @param graph Graph object containing provenance information.
#' @param type Type of data: "fcst" for forecast, "hcst" for hindcast, or "obs" for observational data.
#' @return List containing updated graph and parent node name.
#' @details This function generates provenance information for the computation of graphs in SUNSET.
#' @import igraph
#' @references https://earth.bsc.es/gitlab/es/sunset/-/tree/dev-test-provenance?ref_type=heads Documentation for SUNSET's provenance generation.
#' @references This function is based on the semantics outlined in the Graphical Output ontology within the Metaclip Framework (available at \url{http://www.metaclip.org/}).

Prov.Visualization <- function(recipe, data, graph, type){
  #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

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

      #Graphical representation node
      if (!is.null(recipe$Analysis$Workflow$Visualization$projection)) {
        projection <- recipe$Analysis$Workflow$Visualization$projection
      }else {
        projection <- "Cylindrical equidistant"
      }
      graph <- add_vertices(graph,
                            nv = 1,
                            name = graph.nodename,
                            label = paste0(label.name, " ",
                                         "Graphical representation"),
                            className = "go:Map",
                            description = "A Map class object",
                            attr = list("go:hasProjection" = projection))

      #Link graph node with Skill that plots
      graph <- add_edges(graph, 
                         c(getNodeIndexbyName(graph, 
                                              paste0("Metrics_", type,
                                                     "_", names)),
                           getNodeIndexbyName(graph, graph.nodename)),
                         label = "go:hadGraphicalRepresentation")
      
        #Add Layer definition
        #maplayer.nodename<- paste0("MapLayer_",type,"_",names)
        #graph <- add_vertices(graph,
        #                      nv = 1,
        #                      name = maplayer.nodename,
        #                      label = "MapRasterLayer",
        #                      className = "go:MapRaster",
        #                      description = "Map raster layer definition")
        #graph <- add_edges(graph,
        #               c(getNodeIndexbyName(graph, graph.nodename),
        #                 getNodeIndexbyName(graph, maplayer.nodename)),
        #               label = "go:hasMapLayer")

        #Textual annotations
         #Legend
        #textannot.nodename <- paste("TextLayer",randomName(), sep="_")
        #descr <- "Discrete value-color key" #Where can I get this 
        #          info from? The s2dv_cube? Make a conf. file?
        #graph <- add_vertices(graph,
        #                  nv = 1,
        #                  name = textannot.nodename,
        #                  label = "Legend",
        #                  className = "go:TextLayer",
        #                  attr = list("go:hasTextAnnotationType" = descr))
       #graph <- add_edges(graph,
        #               c(getNodeIndexbyName(graph, graph.nodename),
        #                 getNodeIndexbyName(graph, textannot.nodename)),
        #               label = "go:hasTextLayer")
         #Title
        #title.nodename<-paste("Title",randomName(), sep="_")
        #graph <- add_vertices(graph,
        #                      nv = 1,
        #                      name = title.nodename,
        #                      label = "Title",
        #                      className = "go:TextLayer")
        #                      #attr = list("go:hasTextAnnotationType" = descr))
        #graph <- add_edges(graph,
        #                   c(getNodeIndexbyName(graph, graph.nodename),
        #                     getNodeIndexbyName(graph, title.nodename)),
        #                   label = "go:hasTextLayer")


        #Spatial Extent

        #Additional Layers
        #maplayer.nodename <- paste("MapLayer",randomName(), sep="_")
        # graph <- add_vertices(graph,
        #                      nv = 1,
        #                      name = maplayer.nodename,
        #                      label = "MapLinesLayer",
        #                      className = "go:MapLines")
        #                      #attr = list("go:hasLayerDescription" = descr))
        #graph <- add_edges(graph,
        #                   c(getNodeIndexbyName(graph, graph.nodename),
        #                     getNodeIndexbyName(graph, maplayer.nodename)),
        ##                   label = "go:hasMapLayer")
        #    
        nodenames <- append(nodenames,list(graph.nodename))
    }
  }
  #Forecast ensemble graph node
  if (!is.null(data$fcst)) {
    if ("forecast_ensemble_mean" %in% plots) {
      graph.nodename <- paste0("Graph_", "forecast_ensemble")
      graph <- add_vertices(graph,
                            nv = 1,
                            name = graph.nodename,
                            label = paste0("Forecast Ensemble",
                                           "Graphical representation"),
                            className = "go:Map")
      lastnode <- list("Anomalies_fcst", "Calibration_fcst",
                       "DatasetSubset_fcst", "Interpolation_fcst")
      lastnode <- keep_latest_node(lastnode, graph)
      graph <- add_edges(graph,
                         c(getNodeIndexbyName(graph, lastnode),
                           getNodeIndexbyName(graph, graph.nodename)),
                         label = "go:hadGraphicalRepresentation")

      nodenames <- append(nodenames, list(graph.nodename))
    }
  }
  return(list("graph" = graph,
              "parentnodename" = parentnodename, "nodenames" = nodenames))

}
