# ---------------------------
#
# Script name: Health Data Training App
#
# Purpose of script: creates a shiny app, where health data can be loaded,
# cleaned, agregated and visualized
# Author: Daniela Lührsen
# Date Created: 2023-03
# Email: daniela.luhrsen@bsc.es
#
# ---------------------------

# Load the packages

packages <- c("plotly", "shiny")
lapply(packages, require, character.only = TRUE)

# loading the modules
options(shiny.maxRequestSize = 200 * 1024^2)
source("ui/about_ui.R")
source("ui/access_ui.R")
source("ui/exploration_ui.R")
source("ui/clean_ui.R")
source("ui/filter_ui.R")
source("ui/aggregate_ui.R")
source("ui/visualise_ui.R")
source("R/utils.R")
source("server/about_server.R")
source("server/data_server.R")
source("server/clean_server.R")
source("server/filter_server.R")
source("server/aggregate_server.R")
source("server/visualise_server.R")


# Define UI for application that draws a histogram
ui <- fluidPage(
  style = "padding-right: 10%; padding-left: 10%;",
  tags$style(HTML("
  body {
	  font-family: 'Roboto', Helvetica, Arial, sans-serif;
	  color: #525252;
  }
  h1, h2, h3, h4, h5, h6 {
    font-family: 'Roboto', Helvetica, Arial, sans-serif;
    font-weight: 500;
    line-height: 1.1;
    color: #333333;
  }
  p {
	  text-align: justify;
	  font-family: 'Roboto', Helvetica, Arial, sans-serif;
	  color: #525252;
  }
  .navbar-brand{
    display: flex;
    align-items: center;
  }
  .dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
  }
  .dropdown:hover .dropdown-content {
    display: block;
  }
  #editor {
  background-color: #f0f0f0; /* Change background color */
    color: #333; /* Change font color */
    font-family: Arial, sans-serif; /* Change font family */
    font-size: 14px; /* Change font size */
    }
")),
  div(
    class = "alert alert-warning alert-dismissible",
    role = "alert",
    style = "margin-bottom:15px;",

    paste0(
      "⚠️ This is a beta version of the user interface. ",
      "Features may change and bugs may occur. ",
      "Please report any bugs or suggestions for improvement ",
      "to daniela.luhrsen@bsc.es. Thank you for your collaboration."
    ),

    tags$button(
      type = "button",
      class = "close",
      `data-dismiss` = "alert",
      HTML("&times;")
    )
  ),
  img(src = "harmonize_logo2.png", height = "100px"),
  tabsetPanel(
    id = "tabs",
    type = "tabs",
    about_ui("about"),
    access_ui("access"),
    # exploration_ui("exploration"),
    clean_ui("clean"),
    filter_ui("filter"),
    aggregate_ui("aggregate"),
    visualise_ui("visualise")
  )
)


# Define server logic required to draw a histogram
server <- function(input, output, session) {

  # data module, so that data is available in all modules
  data_store <- data_server("data")

  # vector containing all variables to switch tabs
  app_state <- reactiveValues(
    go_filter = FALSE,
    go_aggregation = FALSE,
    go_visualisation = FALSE
  )

  clean_server("clean", data_store, app_state)
  filter_server("filter", data_store, app_state)
  aggregate_server("aggregate", data_store, app_state)
  visualise_server("visualise", data_store, app_state)

  observeEvent(app_state$go_filter, {
    if (app_state$go_filter) {
      updateTabsetPanel(session, "tabs", "filter-tab")
      app_state$go_filter <- FALSE
    }
  })
  observeEvent(app_state$go_aggregation, {
    if (app_state$go_aggregation) {
      updateTabsetPanel(session, "tabs", "aggregate-tab")
      app_state$go_aggregation <- FALSE
    }
  })
  observeEvent(app_state$go_visualisation, {
    if (app_state$go_visualisation) {
      updateTabsetPanel(session, "tabs", "visualise-tab")
      app_state$go_visualisation <- FALSE
    }
  })
}

# Run the application
shinyApp(ui = ui, server = server)

### END
