library("shinyBS")

ui <- fluidPage(
  shinyjs::useShinyjs(),
  # Ici on empêche l'utilisateur de pouvoir rafraîchir la page accidentellement  
  tags$script('
    window.onbeforeunload = function() {
      return "Voulez-vous vraiment quitter cette page ?";
    };
  '),

  shinyjs::extendShinyjs(text = "
    shinyjs.hideAllElements = function() {
      $('body > *').hide();
    }
  ",functions="shinyjs.hideAllElements"),

  tags$script(HTML(
    "
    function deleteAllCookies() {
      var cookies = document.cookie.split(';');
      for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf('=');
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
      }
    }
    "
  )),
  #theme = shinytheme("readable"),
  navbarPage(
    "Stat Plateform",
    tabPanel("Accueil",
             fluidRow(
               column(4,
                      textOutput("welcome"),
                      wellPanel(
                        fileInput("files", "Sélectionnez les fichiers Excel (xlsx) :", multiple = TRUE),
                        selectInput("ignore_columns", "Colonnes à ignorer :", multiple = TRUE, choices = NULL),
                        shinyBS::bsTooltip("columnTypes","Choisir les types des colonnes",placement = "right",trigger = "hover"),
                        uiOutput("columnTypes"),
                        actionButton("applyTypes", "Appliquer les types de données"),
                      )
               ),
               column(8,
                      wellPanel(
                        style = 'overflow-x: scroll;height: 950px;',
                        DT::dataTableOutput("merged_data_table")
                      )
               )
             ),
    ),
    navbarMenu("Analyse",
               tabPanel("ANOVA/Emmeans",
                        fluidRow(
                          column(4,
                                 wellPanel(
                                   selectInput("var_expliquees","Choisir la variable à expliquer",multiple = FALSE,choices = NULL),
                                   selectInput("var_explicatives","Choisir les variables explicatives",multiple = TRUE,choices = NULL),
                                   actionButton("anova_button", "ANOVA",class="btn-primary")
                                 ),
                                 wellPanel(
                                   selectInput("emmeans_expliquees","Choisir la variable à expliquer",multiple = FALSE,choices = NULL),
                                   selectInput("emmeans_explicatives","Choisir les variables explicatives",multiple = TRUE,choices = NULL),
                                   uiOutput("Temoins"),
                                   selectInput("type_comp", "Type de comparaison", multiple = FALSE, choices = c("pairwise","trt.vs.ctrl","personnalisé")),
                                   actionButton("emmeans_button", "EMMEANS",class="btn-primary")
                                 )
                          ),
                          column(8,
                                 tabsetPanel(
                                   tabPanel('Table',
                                            wellPanel(
                                              style = 'overflow-x: scroll;height: 650px;',
                                              DT::dataTableOutput("anova_table"),
                                              downloadButton("anova_button_download", "Télécharger le tableau",class="btn-primary")
                                            ),
                                            wellPanel(
                                              style = 'overflow-x: scroll;height: 650px;',
                                              DT::dataTableOutput("emmeans_table"),
                                              downloadButton("emmeans_button_download", "Télécharger le tableau",class="btn-primary"),
                                              DT::dataTableOutput("contrast_table"),
                                              downloadButton("contrast_button_download", "Télécharger le tableau",class="btn-primary")
                                            )
                                   ),
                                   tabPanel("result",
                                            h2("Résultat Anova"),
                                            verbatimTextOutput("anova_result"),
                                            h2("Résultat Emmeans"),
                                            verbatimTextOutput("emmeans_result"), 
                                            h2('Moyenne comparée'),
                                            plotly::plotlyOutput('graph_compared_mean')
                                   )
                                 )
                          )
                        )
               ),
               tabPanel("Test de Krustal-Wallis",
                        fluidRow(
                          column(4,
                                 wellPanel(
                                   selectInput("var_expliquees_kw","Choisir la variable à expliquer",multiple = FALSE,choices = NULL),
                                   selectInput("var_explicatives_kw","Choisir la variable explicative",multiple = FALSE,choices = NULL),
                                   actionButton("kw_button", "KW",class="btn-primary")
                                 ),
                          ),
                          column(8,
                                 wellPanel(
                                   style = 'overflow-x: scroll;height: 650px;',
                                   DT::dataTableOutput("kw_table"),
                                   downloadButton("kw_button_download", "Télécharger le tableau",class="btn-primary")
                                 ),
                                 wellPanel(
                                   "KW Resultat",
                                   verbatimTextOutput("kw_resultat")
                                 )
                          )
                        )
               ),
               tabPanel("Khi 2",
                        fluidRow(
                          column(4,
                                 wellPanel(
                                   selectizeInput("khi2_cat","Choisir les variables",multiple = TRUE,choices = NULL),
                                   actionButton("khi2_button", "KHI2",class="btn-primary")
                                 )
                          ),
                          column(8,
                                 wellPanel(
                                   style = 'overflow-x: scroll;height: 650px;',
                                   DT::dataTableOutput("khi2_table"),
                                   downloadButton("khi2_button_download", "Télécharger le tableau",class="btn-primary")
                                 )
                          )
                        )
               ), 
              tabPanel("Test de Pearson",
                       fluidRow(
                         column(4,
                                wellPanel(
                                  selectizeInput("pearson_num","Choisir la variable quantitative",multiple = FALSE,choices = NULL),
                                  selectizeInput("pearson_cat","Choisir la variable catégorielle",multiple = FALSE,choices = NULL),
                                  actionButton("pearson_button", "Pearson",class="btn-primary")
                                ),
                        ),
                          column(8,
                                 wellPanel("Pearson Resultat",
                                   verbatimTextOutput("pearson_result")
                                 )
                        )
                      )
            ),
            tabPanel("Test de Wilcoxon",
                     fluidRow(
                       column(4,
                              wellPanel(
                                selectizeInput("wilcox_num","Choisir la variable quantitative",multiple = FALSE,choices = NULL),
                                selectizeInput("wilcox_cat","Choisir la variable catégorielle",multiple = FALSE,choices = NULL),
                                actionButton("wilcox_button", "Wilcoxon",class="btn-primary")
                              ),
                       ),
                       column(8,
                              wellPanel("Wilcoxon Resultat",
                                        verbatimTextOutput("wilcox_result")
                              )
                       )
                     )
            ),
    ),
    tabPanel("Graphique",
             column(3,
                    wellPanel(
                      h2('Analyse Statistique'),
                      selectInput("dataset_graph", "dataset_graph à prendre", multiple = FALSE, choices = c("cars","airquality","iris")),
                      selectInput("type_graph", "Type de graphique", multiple = FALSE, choices = c("Barplot","Boxplot","Density plot","Geomline","Histograme","Scatterplot")),
                      selectInput("graph_x", "X", multiple = FALSE, choices = NULL),
                      selectInput("graph_y", "Y", multiple = FALSE, choices = NULL),
                      selectInput("graph_fill", "Fill", multiple = FALSE, choices = NULL),
                      selectInput("facet_a","Face grid",multiple = FALSE,choices = '.',),
                      selectInput("facet_b","Face grid",multiple = FALSE,choices = '.',)
                    )
             ), 
             column(9,
                    fluidRow(
                      wellPanel(
                        plotly::plotlyOutput("graph_graph"),
                      )
                    )
             )
    ),
    tabPanel("Instructions", 
             fluidRow(
               column(8,
                      includeMarkdown("README.md")
               )
             )
    ), 
    navbarMenu("Régressions",
               tabPanel("ACP",
                        column(3,
                               wellPanel(
                                 h2('ACP'),
                                 selectInput("acp_col", "Numerical Columns", multiple = TRUE, choices = NULL),
                                 actionButton("acp_button", "ACP",class="btn-primary"),
                               ),
                        ), 
                        column(9,
                               fluidRow(
                                 wellPanel(
                                   plotly::plotlyOutput("plotly_acp_graph")
                                 )
                               )
                        )
               ),
               tabPanel("PLS",
                        column(3,
                               wellPanel(
                                 h2('PLS'),
                                 selectInput("pls_x", "X", multiple = TRUE, choices = NULL),
                                 selectInput("pls_y", "Y", multiple = FALSE, choices = NULL),
                                 sliderInput("pls_slider","Nombre de composante",1,1,1,step = 1),
                                 actionButton("pls_button", "PLS",class="btn-primary")
                               )
                        ), 
                        column(9,
                               fluidRow(
                                 wellPanel( 
                                   h2('Résultats'),
                                   verbatimTextOutput("pls_text"),
                                 )
                               )
                        )
               ),
               tabPanel("Linéaire",
                        column(6,
                               wellPanel(
                                 h2('Régression Linéaire'),
                                 selectInput("lm_x", "X", multiple = FALSE, choices = NULL),
                                 selectInput("lm_y", "Y", multiple = TRUE, choices = NULL),
                                 actionButton("lm_button", "Régression",class="btn-primary")
                               )
                        ), 
                        column(6,
                               fluidRow(
                                 wellPanel( 
                                   h2('Résultats'),
                                   verbatimTextOutput("lm_text"),
                                 )
                               )
                        )
               ),
                tabPanel("Modèle Mixte",
                        fluidRow(
                          column(4,
                                 wellPanel(
                                   selectInput("mixte_expliquee","Choisir la variable à expliquer",multiple = FALSE,choices = NULL),
                                   selectInput("mixte_explicatives_fixes","Choisir les variables explicatives fixes",multiple = TRUE,choices = NULL),
                                   selectInput("mixte_explicatives_aleas","Choisir les variables explicatives aléatoires",multiple = TRUE,choices = NULL),
                                   textInput("mixte_name","Choisir le nom du modèle",),
                                    actionButton("mixte_button", "Modèle Mixte", class = "btn btn-primary")
                                 ),
                                 wellPanel(
                                   selectInput("mixte_effet_hypothesisGraph","Choisir les effets à afficher",multiple = TRUE,choices = NULL),
                                    selectInput("mixte_expliquee_model","Choisir le modèle",multiple = FALSE,choices = NULL),
                                   actionButton("hypothesisGraph_button", "Graphe hypothèse",class="btn-primary")
                                 ),
                                 wellPanel(
                                  selectInput("mixte_anova","Choisir le modèle pour Anova",multiple = FALSE,choices = NULL),
                                  actionButton("mixte_anova_button", "Anova", class = "btn btn-primary")
                                 ),
                                 wellPanel(
                                  selectInput("mixte_emmeans","Choisir le modèle pour Emmeans",multiple = FALSE,choices = NULL),
                                  selectInput("mixte_emmeans_explicatives","Choisir les variables explicatives",multiple = TRUE,choices = NULL),
                                  uiOutput("Temoins_mixte"),
                                  selectInput("type_comp_mixte_emmeans", "Type de comparaison", multiple = FALSE, choices = c("pairwise","trt.vs.ctrl","personnalisé")),
                                  actionButton("mixte_emmeans_button", "Anova", class = "btn btn-primary")
                                 ),
                                 
                          ),
                          column(8,
                                wellPanel(
                                 tabsetPanel(
                                   tabPanel('Table',
                                            wellPanel(
                                              h3("Présentation des modèles"),
                                              style = 'overflow-x: scroll;height: 328px;',
                                              DT::dataTableOutput("mixte_table"),
                                              downloadButton("mixte_button_download", "Télécharger le tableau",class="btn-primary")
                                            )
                                   ),
                                   tabPanel("result",
                                            h2("Résultat Modèle Mixte"),
                                            verbatimTextOutput("mixte_result"),
                                            h2('Moyenne comparée'),
                                            plotly::plotlyOutput('graph_compared_mean_mixte'),
                                            h2('Graphe d\'hypothèses'),
                                            uiOutput("graphsOutput")
                                   )
                                 )
                                ),
                                wellPanel(
                                  h3("Comparaison des modèles"),
                                  DT::dataTableOutput("mixte_comparaison_table")
                                ),
                                wellPanel(
                                  h3("Analyse du modèle"),
                                  DT::dataTableOutput("mixte_anova_table")
                                ),
                                wellPanel(
                                    style = 'overflow-x: scroll;height: 650px;',
                                    DT::dataTableOutput("mixte_emmeans_table"),
                                    downloadButton("mixte_emmeans_button_download", "Télécharger le tableau",class="btn-primary"),
                                    DT::dataTableOutput("mixte_contrast_table"),
                                    downloadButton("mixte_contrast_button_download", "Télécharger le tableau",class="btn-primary")
                                            )
                          )
                        )
                )
    ),
    tabPanel(
      tags$div(
        class = "navbar-text pull-right",
        actionButton("logout", "Se déconnecter", class = "btn-danger")
      )
    )
  )
)