
#' Creates dashboard for the given table with data
#'
#'
#' @param table trips_output tibble from readTripsTable()
#'
#' @param append specifies if the ouput folder should be erased before creating, default = FALSE
#'
#' @param dump.output.to output folder, yaml files and plots from the following functions are saved there:
#' plotModalSplitBarChart(),plotModalSplitPieChart(),plotModalShift().
#'
#' @return generates folder with content for simwrapper out of trips table
#'
#' @export
prepareSimwrapperDashboardFromTable <- function(table, dump.output.to = matsimDumpOutputDirectory, append = FALSE) {
  if (append == FALSE) {
    if (file.exists(dump.output.to)) {
      unlink(dump.output.to, recursive = TRUE)
    }
  }
  plotModalSplitBarChart(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotModalSplitPieChart(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotAverageTravelWait(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotModalDistanceDistribution(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotTripDistanceByMode(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotTripCountByDepTime(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotStartActCountByDepTime(table,dump.output.to = dump.output.to,only.files = TRUE)
  plotEndActCountByArrTime(table,dump.output.to = dump.output.to,only.files = TRUE)
  #Not sure if it is needed
  #plotModalShift(table, table,dump.output.to = dump.output.to)
}



#' Creates dashboard for the given folder with data
#'
#'
#'
#' @param folder file path, specifies data source folder that includes a output_trips.csv file
#'
#' @param append specifies if the ouput folder should be erased before creating, default = FALSE
#'
#' @param dump.output.to output folder, yaml files are saved there
#'
#' @return tibble of output_trips from folder and content needed for Simwrapper
#'
#' @export
prepareSimwrapperDashboardFromFolder <- function(folder,dump.output.to = matsimDumpOutputDirectory, append = FALSE) {
  options(digits = 18)
  table = readTripsTable(folder)
  path = attr(table,"table_name")
  prepareSimwrapperDashboardFromTable(table,dump.output.to,append)
  crs = getCrsFromConfig(folder)
  if(!is.na(crs)){
    generateXYHexagonYaml(attr(table,"table_name"),crs,dump.output.to = dump.output.to)
  }
  return(table)
}

#' Creates/adds XY hexagon definition of output trips to a summary dashboard
#'
#' @param pathToOutputTrips file path to output_trips.csv file
#'
#' @param crs specifies the coordinate reference system of output_trips
#'
#' @param dump.output.to path to a folder with simwrapper dashboard
#'
#' @return changed file
#'
#' @export
generateXYHexagonYaml<- function(pathToOutputTrips,crs,dump.output.to = matsimDumpOutputDirectory)
{
  yaml_list <- list(
    header = list(tab = "Summary", title = "Dashboard", description = "Plots from output directory"),
    layout = list("1" = list(
      title =  "XY Hexagon of trip origings/destinations ",
      description = "generated by generateXYHexagonYaml()",
      type = "hexagons",
      width = 1,
      props = list(height = 10,
                   width=2,
                   file =paste0("../",pathToOutputTrips) ,
                   projection = crs,
                   zoom  = 10,
                   radius = 100,
                   maxHeight=20,
                   aggregations = list("OD Summary" = list(list(title = "origins",x = "start_x",y = "start_y"),
                                                           list(title = "destinations",x = "end_x",y = "end_y")))
      )
    )
    )
  )
  if (file.exists(paste0(dump.output.to, "/dashboard-sum.yaml"))) {
    yaml_from_directory <- read_yaml(paste0(dump.output.to, "/dashboard-sum.yaml"))
    yaml_from_directory$layout <- append(yaml_from_directory$layout, list(new_row = list(
      title =  "XY Hexagon of trip origins/destinations ",
      description = "generated by generateXYHexagonYaml()",
      type = "hexagons",
      width = 1,
      props = list(height = 10,
                   width=2,
                   file =paste0("../",pathToOutputTrips) ,
                   projection = crs,
                   zoom  = 10,
                   radius = 100,
                   maxHeight=20,
                   aggregations = list("OD Summary" = list(list(title = "origins",x = "start_x",y = "start_y"),
                                                           list(title = "destinations",x = "end_x",y = "end_y")))
      )
    )))
    names(yaml_from_directory$layout) <- 1:length(names(yaml_from_directory$layout))

    write_yaml(yaml_from_directory, paste0(dump.output.to, "/dashboard-sum.yaml"))
  } else {
    write_yaml(yaml_list, paste0(dump.output.to, "/dashboard-sum.yaml"))
  }

}


clearDashboard<- function(matsimOutputFolder){
  if (file.exists(dump.output.to)) {
    unlink(dump.output.to, recursive = TRUE)
  }
}




#' Creates comparison dashboard for the two given tables or folders with data
#'
#'
#'
#' @param table1 trips_output tibble from readTripsTable()
#'
#' @param table2 trips_output tibble from readTripsTable()
#'
#' @param append specifies if the output folder should be erased before creating, default = FALSE
#'
#' @param dump.output.to output folder, yaml files and plots from the following functions are saved there:
#' plotModalSplitBarChart(),plotModalSplitPieChart(),plotModalShift().
#'
#' @return generates folder with content for simwrapper from a trips table
#'
#' @export
prepareComparisonSimwrapperDashboardFromTable <- function(table1,table2, dump.output.to = matsimDumpOutputDirectory, append = FALSE) {
  if (append == FALSE) {
    if (file.exists(dump.output.to)) {
      unlink(dump.output.to, recursive = TRUE)
    }
  }
  compareAverageTravelWait(table1,table2)
  compareModalDistanceDistribution(table,table2)
  plotModalShiftSankey(table1,table2)
  #Not sure if it is needed
  #plotModalShift(table, table,dump.output.to = dump.output.to)
}
