#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

#install.packages("anyLib")
anyLib::anyLib(c("shiny", "shinydashboard", "shinyWidgets", "DT", "plotly", "ggplot2", "googleVis", "colourpicker"))


library(shiny)
library(shinydashboard)
library(readxl)
library(shinythemes)

# Load full database
# IDEA: have a button in the interface to update the DB (so that it does not do it by default, but we can ask for it)
SNIPPETS = "~/local/bitbucket/snippets"
source(paste0(SNIPPETS,"/bash/danstemDatabaseFunctions.R"))
DB = loadDB(update=F)
PROJECTS = DB$db$proj
DATASETS = DB$db$db
RUNS = DB$dbrun
DATA = flattenDB(DB$db)
USERS = unique(DATA[,c("owner","email")])

# side bar
side = dashboardSidebar(
    sidebarMenu(
        #menuItem("Uploading", tabName = "readData", icon = icon("readme")),
        menuItem("Full database", tabName = "full", icon = icon("poll")),
        menuItem("Selection", tabName = "selection", icon = icon("poll"))
    )
)

# body
# IDEA: have different tabs to look at 
# - projects (table with relevant columns)
# - something per group? or is this part of the selection (per date, per group, per technique)
# - runs
body =  dashboardBody(
    tabItems(
        tabItem(tabName = "full",
                #h1("Full database"),
                fluidRow(
                    infoBox(
                        width=12,
                        title="Welcome to the Genomics Platform Database!",
                        icon=icon("bong"),
                        color="purple",
                        "Helen and Magali")
                ),
                fluidRow(
                    column(width=6,
                           valueBox(
                               width=NULL,
                               value = nrow(PROJECTS),
                               subtitle = "Projects",
                               icon = icon("archive"),
                               color="purple"
                           )
                    ),
                    column(width=6,
                           valueBox(
                               width=NULL,
                               value = nrow(RUNS),
                               subtitle = "Runs",
                               color="purple",
                               icon = icon("adn")
                           )
                    )
                ),
                fluidRow(
                    column(width = 6,
                           infoBox(
                               width = NULL,
                               title = "Users",
                               color="purple",
                               subtitle = paste(nrow(USERS),"project owners")
                           )
                    ),
                    column(width = 6,
                           infoBox(
                               width = NULL,
                               title = "Groups",
                               color="purple",
                               subtitle = paste(length(unique(PROJECTS$group)),"groups")
                           )
                    )
                ),
                
                #h2("Exploring the table"),
                dataTableOutput('dataTable'),
                #h2("Graphics"),
                fluidRow(
                    column(3,plotOutput("plotGroups") ),
                    column(3, plotOutput("plotDataType"))
                )
        )
    )
)

# Create the interface
ui <- dashboardPage(
    skin = "purple",
    dashboardHeader(title="DanStem-Genomics"),
    side,
    body
)


# Define server logic
server <- function(input, output,session) {
    #data <- reactiveValues()
    #data = RUNS
    
    # output$preview <- renderDataTable({
    #     RUNS},  options = list(scrollX = TRUE , dom = 't'))
    
    output$dataTable = DT::renderDataTable(DATA)
    
    output$plotGroups <- renderPlot({
        counts = table(DATA$group)
        barplot(sort(counts),las=1,horiz = T,xlab="Number of runs")
        grid()
    })
    
    output$plotDataType <- renderPlot({
        counts = table(DATA$group)
        pie(counts)
    })
}

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