
connect <- function (){
  
  cat("\n\n\n----------------------------------------------------------------------------------------------------------")
  cat("\nPlease select the number corresponding to the hospital you want to analyse, if you want to do a combined analysis select multiple hospitals")
  cat("\n  
        Princesa -> 1  
        CIPH -> 2  
        UMF_Iasis -> 3  
        SMUC -> 4  
        HM -> 5  
        Porto -> 6  
        FJD -> 7  
        Coimbra -> 8  
        UNAV -> 9 
        TU -> 10 
        
          Baskent:
          
          Ankara Impatient -> 11
          Konya Impatient -> 12
          Istambul Impatient -> 13
          Izmir Impatient -> 14
          Alanya Impatient -> 15
          Adana Impatient -> 16
          
          Ankara Outpatient -> 17
          Konya Outpatient -> 18
          Istambul Outpatient -> 19
          Izmir Outpatient -> 20
          Alanya Outpatient -> 21
          
        Sacrocuore:
        
          Emergency database -> 22
          Employees database -> 23
          Verona database -> 24
          Isaric -> 25
        
        TU Dublin -> 26
        UMF Cluj -> 27
        UdeA -> 28
        Inantro -> 29
        UNSA -> 30
        UZA -> 31
          
      ")
  
  inp <- scan()
  
  builder <- DSI::newDSLoginBuilder()
  
  
  hospital_names <- hospital_names[inp]
  project_names <- project_names[inp]
  resource_names <- resource_names[inp]
  urls <- urls[inp]
  users <- users[inp]
  pass <- pass[inp]
  
  print(hospital_names)
  print(project_names)
  print(resource_names)
  print(urls)
  print(users)
  print(pass)
  
  url_ctr <- 0
  for(i in 1:length(urls)){
    print(paste("Connecting to Server with URL:", urls[i], sep=" "))
    builder$append(server = hospital_names[i], url = urls[i],
                   user = users[i], password = pass[i],
                   resource = paste(project_names[i], resource_names[i], sep="."),
                   driver = "OpalDriver", options="list(ssl_verifyhost=0,ssl_verifypeer=0)")
    
    url_ctr <- url_ctr+1
  }
  
  logindata <- builder$build()
  
  connections <- DSI::datashield.login(logins = logindata, assign = TRUE, symbol = "D", failSafe = TRUE)
  
  
  datashield.assign.expr(connections, symbol = 'data', expr = quote(as.resource.data.frame(D)))
  
  print("Successful connection to servers.")
  
  return(list(connections,inp))
}


getCountryHosp <- function(hospitals){
  
  Spain <- c("HM", "Princesa", "FJD", "UNAV")
  Turkey <- c("Ankara Impatient", "Konya Impatient", "Adana Impatient", "Izmir Impatient", "Istambul Impatient", "Alanya Impatient", "Ankara Outpatient", "Konya Outpatient", "Izmir Outpatient", "Istambul Outpatient", "Alanya Outpatient")
  Italy <- c("Sacrocuore Emergency",  "Sacrocuore Employees",  "Sacrocuore Verona", "Sacrocuore Isaric")
  Portugal <- c("Porto", "Coimbra")
  UK <- c("SMUC")
  Ireland <- c("TUDublin")
  Slovakia  <- c("TU")
  Romania <- c("UMF_Iasis", "UMF_Cluj")
  Croatia <- c("CIPH","Inantro")
  Colombia <- c("UdeA")
  Bosnia_Herzegovina <- c("UNSA")
  Belgium <- c("UZA")
  
  Spain_Index <- c()
  Turkey_Index <- c()
  Italy_Index <- c()
  Portugal_Index <- c()
  UK_Index <- c()
  Ireland_Index <- c()
  Slovakia_Index <- c()
  Romania_Index <- c()
  Croatia_Index <- c()
  Colombia_Index <- c()
  Bosnia_Herzegovina_Index <- c()
  Belgium_Index <- c()
  
  country_index <- list()
  
  for(i in 1:length(hospitals)){
    
    if(hospitals[i] %in% Spain){
      Spain_Index <- c(Spain_Index, i)
      
    }else if(hospitals[i] %in% Turkey){
      Turkey_Index <- c(Turkey_Index, i)
      
    }else if(hospitals[i] %in% Italy){
      Italy_Index <- c(Italy_Index, i)
      
    }else if(hospitals[i] %in% Portugal){
      Portugal_Index <- c(Portugal_Index, i)
      
    }else if(hospitals[i] %in% UK){
      UK_Index <- c(UK_Index, i)
      
    }else if(hospitals[i] %in% Ireland){
      Ireland_Index <- c(Ireland_Index, i)
      
    }else if(hospitals[i] %in% Slovakia){
      Slovakia_Index <- c(Slovakia_Index, i)
      
    }else if(hospitals[i] %in% Romania){
      Romania_Index <- c(Romania_Index, i)
      
    }else if(hospitals[i] %in% Croatia){
      Croatia_Index <- c(Croatia_Index, i)
      
    }else if(hospitals[i] %in% Colombia){
      Colombia_Index <- c(Colombia_Index, i)
      
    }else if(hospitals[i] %in% Bosnia_Herzegovina){
      Bosnia_Herzegovina_Index <- c(Bosnia_Herzegovina_Index, i)
      
    }else if(hospitals[i] %in% Belgium){
      Belgium_Index <- c(Belgium_Index, i)
      
    }
    
    
    
    
  }
  
  if(length(Spain_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Spain_Index)
    names(country_index)[aux] <- "Spain"
  }
  
  if(length(Turkey_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Turkey_Index)
    names(country_index)[aux] <- "Turkey"
  }
  
  if(length(Italy_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Italy_Index)
    names(country_index)[aux] <- "Italy"
  }
  
  if(length(Portugal_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Portugal_Index)
    names(country_index)[aux] <- "Portugal"
  }
  
  if(length(UK_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(UK_Index)
    names(country_index)[aux] <- "UK"
  }
  
  if(length(Ireland_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Ireland_Index)
    names(country_index)[aux] <- "Ireland"
  }
  
  if(length(Slovakia_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Slovakia_Index)
    names(country_index)[aux] <- "Slovakia"
  }
  
  if(length(Romania_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Romania_Index)
    names(country_index)[aux] <- "Romania"
  }
  
  if(length(Croatia_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Croatia_Index)
    names(country_index)[aux] <- "Croatia"
  }
  
  if(length(Colombia_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Colombia_Index)
    names(country_index)[aux] <- "Colombia"
  }
  
  if(length(Bosnia_Herzegovina_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Bosnia_Herzegovina_Index)
    names(country_index)[aux] <- "Bosnia & Herzegovina"
  }
  
  if(length(Belgium_Index) != 0 ){
    aux <- length(country_index)+1
    country_index[aux] <- list(Belgium_Index)
    names(country_index)[aux] <- "Belgium"
  }
  

  
  return(country_index)
  
}

#Distinguish variable Types
numeric <- c("DMRAGEYR","DMXHT","DMXWT","DMXBMI","DATLGT","DATLGTI","DATSSDHn","CSXBTPA","CSXBTPHn","CSXOSTA","CSXOSTHn","CSXCHRA","CSXCHRHn","CSXRRA","CSXRRHn","CSXRRI","CSXSYA","CSXSYHn","CSXDIA","CSXDIHn","SMTFE","SMTCO","SMTST","SMTSB","SMXSEA","DATIMD","IMDXCTLD","IMDXEQ","DATLBDHn","LBXHGBA","LBXHGBHn","LBXESRA","LBXESRHn","LBXWBCSIA","LBXWBCSIHn","LBXLYMNOA","LBXLYMNOHn","LBXNENOA","LBXNENOHn","LBXHCTA","LBXHCTHn","LBXPLTSIA","LBXPLTSIHn","LBXGHA","LBXGHHn","LBXAPTTA","LBXAPTTHn","LBXAPTRA","LBXAPTRHn","LBXPTA","LBXPRHn","LBXINRA","LBXINRHn","LBXSATSIA","LBXSATSIHn","LBXSTBA","LBXSTBHn","LBXSCBA","LBXSCBHn","LBXSUBA","LBXSUBHn","LBXSASSIA","LBXSASSIHn","LBXSGLA","LBXSGLHn","LBXSBUA","LBXSBUHn","LBXSBLA","LBXSBLHn","LBXSCRA","LBXSCRHn","LBXSNASIA","LBXSNASIHn","LBXSCLSIA","LBXSCLSIHn","LBXSKSIA","LBXSKSIHn","LBXSPCA","LBXSPCHn","LBXCRPA","LBXCRPHn","LBXSLDSIA","LBXSLDSIHn","LBXCTRA","LBXCTRHn","LBXCDDA","LBXCDDHn","LBXFERSIA","LBXFERSIHn","LBXIL6A","LBXIL6Hn","LBDFBSIA","LBDFBSIHn","LBDSALSIA","LBDSALSIHn","LBXSAPSIA","LBXSAPSIHn","LBXSGTSIA","LBXSGTSIHn","LBXCFDA","LBXCFDHn","LBXFIOA","LBXFIOHn","LBXPOA","LBXPOHn","LBXPCOA","LBXPCOHn","LBXSC3SIA","LBXSC3SIHn","LBXPHA","LBXPHHn","LBXBEH","LBXBEHn","LBXA4A","LBXA4Hn","LBXTCA","LBXTCHn","LBXTRA","LBXTRHn","LBXSCKA","LBXSCKHn","LBXPSCKA","LBXPSCKHn")
categoric <- c("DMRGENDR", "DMRBORN", "DMRRETH1", "DMROCCU", "DMRHREDU", "DSXOS", "DSXHO", "DSXIC", "TRXAV","TRXRIB","TRXLR","TRXRM","TRXIA","TRXIB","TRXCH","TRXAB","TRXCS","TRXHEP","TRXAF","TRXCP","TRXOT","TRXECM","TRXIV","TRXNIV","TRXNO","TRXOX","TRXRR","TRXTR","TRXVA","TRXPE","TRXPV","TRXIT","TRXNMB","TRXAC","TRXINA","TRXIS","TRXIM","TRXVC","TRXVD","TRXZN",                         "CSXCOT","CSXCTR","SMXASAH","SMXFEA","SMXCOA","SMXSTA","SMXSBA","SMXRNA","SMXMYA","SMXARA","SMXCPA","SMXAPA","SMXINA","SMXNAA","SMXDIA","SMXFAA","SMXHEA","SMXCNA","SMXACA","SMXSLA","SMXTLA","SMXSYA","SMXWHA","SMXLYA","SMXANA","SMXIWA","SMXSRA","SMXBLA","CMXPRG","CMXCVD","CMXCMP","CMXHT","CMXDI","CMXCKD","CMXCLD","CMXCPD","CMXASM","CMXCND","CMXRHE","CMXCCI","CMXCBD","CMXDE","CMXPU","CMXST","CMXLY","CMXAP","RFXSM","RFXFSM","RFXOB","RFXTB","RFXIMD","RFXHIV","RFXAIDS","RFXUI","RFXHC","RFXONC","RFXMN",                         "HMRACI","HMRARB","HMRAHO","HMRNS","HMROS","HMRCS","HMRIS","HMRAV","HMRAB","HMRCOV","IMDXCT","IMDXCTCR","IMDXCTTE","IMDXCTAB","IMDXXR","IMDXPN",                         "COXRD","COXAR","COXPM","COXMOD","COXPT","COXEC","COXSH","COXIO","COXPE","COXST","COXDIC","COXRIO","COXKF","COXHF","COXBC")



hospitals_contain_var <- function(hospitals_cols, variable, index){

  res <- c()

  for(i in 1: length(hospitals_cols)){

    if(variable %in% unlist(hospitals_cols[i]))
      res <- c(res, i)

  }

  if(is.null(res)){
    return(NULL)
  }else if(is.null(index)){
    return (res)
  }else{
    return(index[res])
  }

}

get_working_hosp <- function(index, connections, to_analyse){

  new_index <- c()


  for(i in 1:length(index)){


    analysisRes <- NULL

    try(analysisRes <- ds.table(to_analyse,datasources = connections[index[i]]))


    if(!is.null(analysisRes)){
      if(!grepl("Error", analysisRes[1])  & !grepl("failed", analysisRes[1]))
      new_index <- c(new_index,index[i])
    }

  }

  return(new_index)


}

obtain_table1 <- function(connections, inp, varToAnalize){


  #Obtain the name of the hospitals and the corresoponding indexes according to country
  hospitals_cols <- ds.colnames("data")
  hospitals <- names(hospitals_cols)
  country_Index_Total <- getCountryHosp(hospitals)

  #Create empty result
  result_df <- data.frame(matrix(ncol = 3, nrow = 1))
  colnames(result_df) <- c('Codebook Variable', 'Description', 'Category/Units')

  name_df <- result_df

  #Add the necessary headers
  for(i in 1:length(country_Index_Total)){

    country <- names(country_Index_Total)[i]
    country_Index <- unname(unlist(country_Index_Total[i]))
    country_hospitals <- hospitals[country_Index]

    nPatients <- ds.dim("data",datasources = connections[country_hospitals])

    for(j in 1:length(country_Index)){

      n_Patients_Hosp <- nPatients[[j]][1]
      new_col_name <- paste(paste(paste(country_hospitals[j],"(N ="),n_Patients_Hosp),")",sep="")

      result_df[new_col_name] <- NA
      name_df[country_hospitals[j]] <- NA

    }

    new_col_name <- paste(paste(paste(paste("Total", country), "(N ="),nPatients[[j+1]][1]),")",sep="")
    result_df[new_col_name] <- NA
    name_df[country] <- NA

  }

  for(i in 1:length(varToAnalize)){

    variable <- varToAnalize[i]
    description <- subset(codebook$description,codebook$variable == variable)
    if(length(description)== 0)
      description <-  ""
    category <- subset(codebook$unit,codebook$variable == variable)

    to_analyse <- paste("data$",variable,sep="")



    if(variable %in% categoric){

      print("Categoric")

      possible_vars <- c()

      contain_var <- hospitals_contain_var(hospitals_cols, variable, NULL)

      analysis <- NULL
      analysis <- try(ds.table(to_analyse,datasources = connections[contain_var]))

      
      if(is.null(analysis)){
        
        new_index <- get_working_hosp(contain_var, connections, to_analyse)
        if(length(new_index) > 0)
          analysis <- try(ds.table(to_analyse,datasources = connections[new_index]))
        if(is.null(new_index))
          next
        
      }else if(grepl("Error", analysis[1]) | grepl("failed", analysis[1])){
        
        new_index <- get_working_hosp(contain_var, connections, to_analyse)
        if(length(new_index) > 0)
          analysis <- try(ds.table(to_analyse,datasources = connections[new_index]))
        if(is.null(new_index))
          next
      }

      if(!is.null(analysis) & !grepl("Error", analysis[1])){

        auxAnalisis <- analysis[[1]]
        auxAnalisis <- unlist(unname(auxAnalisis[length(auxAnalisis)]))

        for(j in 1:length(auxAnalisis)){
          possible_vars <- c(possible_vars, names(auxAnalisis[j]))
        }

        auxdf <- data.frame (matrix(ncol = length(colnames(name_df)), nrow = length(possible_vars)))
        colnames(auxdf) <- colnames(name_df)
        auxdf[1,"Codebook Variable"] <- variable
        auxdf[1,"Description"] <- description

        for(k in 1:length(country_Index_Total)){


          countries_index <- unlist(country_Index_Total[k])
          country <- names(country_Index_Total)[k]

          print("Analysis for the country:")
          print(country)

          if(length(countries_index)>1)
            contain_var <- hospitals_contain_var(hospitals_cols[countries_index], variable, countries_index)
          else{
            if(variable %in% unlist(hospitals_cols[countries_index]))
              contain_var <- countries_index
            else
              next
          }
            

          if(is.null(contain_var)){
            print("No selected hospitals contain this variable in this country")
            next
          }
          
          for(j in 1:length(contain_var)){
            
            new_index <- contain_var[j]
            hospital <- hospitals[new_index]
            
            print(hospital)
            
            analysis <- NULL
            try(analysis <- ds.table(to_analyse,datasources = connections[new_index]))
            
            if(!is.null(analysis)){
              if(!(grepl("Error", analysis[1]) | grepl("failed", analysis[1]))){
                
                auxAnalisis <- analysis[[1]]
                auxAnalisis <- unlist(unname(auxAnalisis[length(auxAnalisis)]))

                num_result_per_hosp <- as.data.frame(analysis[[1]][[3]])
                prop_result_per_hosp <- as.data.frame(analysis[[1]][[2]])
                
                for(h in 1:length(possible_vars)){
                  
                  auxdf[h,"Category/Units"] <- possible_vars[h]
                  
                }
                
                
                for(l in 1:dim(num_result_per_hosp)[[1]]){
                  
                  num_res <- num_result_per_hosp[hospital][[1]][l]
                  position <- get_posVal_index(row.names(num_result_per_hosp)[l], possible_vars)
                  
                  if(is.na(num_res))
                    auxdf[position,hospital] <- ""
                  else{
                    prop_res <- format(round(prop_result_per_hosp[hospital][[1]][l], 4), nsmall = 2)
                    prop_res <- as.double(prop_res)*100
                    res <- paste(paste(paste(num_res, "("),prop_res,sep=""),"%)",sep="")
                    auxdf[position,hospital] <- res
                  }
                  
                }
                
                
              }
            }
          }
          
          try(analysis <- ds.table(to_analyse,datasources = connections[contain_var]))

          if(is.null(analysis)){

            new_index <- get_working_hosp(contain_var, connections, to_analyse)
            if(length(new_index) > 0)
              analysis <- try(ds.table(to_analyse,datasources = connections[new_index]))
            if(is.null(new_index))
              next

          }else if(grepl("Error", analysis[1]) | grepl("failed", analysis[1])){

            new_index <- get_working_hosp(contain_var, connections, to_analyse)
            if(length(new_index) > 0)
              analysis <- try(ds.table(to_analyse,datasources = connections[new_index]))
            if(is.null(new_index))
              next
          }
          
          for(l in 1:length(possible_vars)){
            
            num_result_tot <- as.data.frame(analysis[[1]][[5]])
            prop_result_tot <- as.data.frame(analysis[[1]][[4]])
            num_res <- num_result_tot[[1]][l]
            position <- get_posVal_index(row.names(num_result_tot)[l], possible_vars)
            
            if(is.na(num_res))
              auxdf[position,country] <- ""
            
            else{
              
              prop_res <- format(round(prop_result_tot[[1]][l], 4), nsmall = 2)
              prop_res <- as.double(prop_res)*100
              res <- paste(paste(paste(num_res, "("),prop_res,sep=""),"%)",sep="")
              
              
              auxdf[position,country] <- res
            }
          }

        }
        
        colnames(auxdf) <- colnames(result_df)
        result_df <- rbind(result_df,auxdf)

        # auxdf <- name_df
        # colnames(auxdf) <- colnames(result_df)
        # result_df <- rbind(result_df,auxdf)
      }else{
        print("Unable to abtain data form the field:")
        print(variable)
      }

    }else if (variable %in% numeric){

      print("Numeric")

      auxdf <- data.frame (matrix(ncol = length(colnames(name_df)), nrow = 1))
      colnames(auxdf) <- colnames(name_df)
      auxdf[1,"Codebook Variable"] <- variable
      auxdf[1,"Description"] <- description
      auxdf[1,"Category/Units"] <- category

      for(k in 1:length(country_Index_Total)){

        countries_index <- unlist(country_Index_Total[k])
        country <- names(country_Index_Total)[k]
        
        print("Analysis for the country:")
        print(country)

        analyzedHosp <- hospitals[countries_index]

        if(length(countries_index)>1)
          contain_var <- hospitals_contain_var(hospitals_cols[countries_index], variable, countries_index)
        else
          contain_var <- countries_index

        if(is.null(contain_var)){
          print("No selected hospitals contain this variable in this country")
          next
        }

        analysisMean <- NULL
        try(analysisMean <- ds.mean(to_analyse,datasources = connections[contain_var]))
        try(analysisVar <- ds.var(to_analyse,datasources = connections[contain_var]))

        #If the variable exists
        if(!is.null(analysisMean)){
          if(!(grepl("Error", analysisMean[1]) | grepl("FAILED", analysisMean[3]))){
            
            means <- c()
            
            for(j in 1:length(contain_var)){
              
              
              index_hosp <- contain_var[j]
              hospital <- hospitals[index_hosp]
              mean_res <- analysisMean[[1]][j]
              var_res <- analysisVar[[1]][j]
              
              if(!is.na(mean_res) & mean_res !=0){
                
                
                means <- c(means,mean_res)
                mean_res <- format(round(mean_res, 2), nsmall = 2)
                sd_res <- format(round(sqrt(var_res), 2), nsmall = 2)
                res <- paste(paste(paste(mean_res, "(+-"),sd_res,sep=""),")",sep="")
                auxdf[hospital] <- res
              }else{
                auxdf[hospital] <- NA
              }
              
            }
            
            
            if(length(means)!= 0){
              res <- format(round(mean(means), 2), nsmall = 2)
              auxdf[country] <- res
            }else {
              auxdf[country] <- NA
            }
            
          }

         

        }

      }

      colnames(auxdf) <- colnames(result_df)
      result_df <- rbind(result_df,auxdf)

    }

  }

  for(i in 1:nrow(result_df)){
    for (j in 1:ncol(result_df)){
      if(is.na(result_df[i,j]))
        result_df[i,j] <- ""
    }
  }

  result_df <- result_df[-1,]

  write.csv(x=result_df, file = "table1.csv", row.names = FALSE)


  datashield.logout(connections)

  saveRDS(result_df,file="table1_save.Rda")

  return(result_df)
  
}



apply_filters <- function(){
  
  cols <- ds.colnames("data")
  posVarsCols <- Reduce(intersect, cols)
  filters <- TRUE
  
  while(filters){
    okvar <- FALSE
    okval <- FALSE
    okcountry <- FALSE
    
      
      while(!okvar | !okcountry){
        variableN <- readline("Please enter the variable you want to subset from > ")
        if(variableN %in% categoric)
          okvar <- TRUE
        else{
          okvar <- FALSE
          print("Please enter a categoric variable")
        }
          
        if(variableN %in% posVarsCols)
          okcountry <- TRUE
        else{
          print("Please use a variable that is present in all datasets")
          print("The variables that are present in all the datasets are the following")
          print(posVarsCols)
        }
      }
      
      
      
    
    
    posvals <- strsplit(subset(codebook, codebook$variable == variableN)[1,3], split = "/")
    variableN <- paste(variableN,"_numeric", sep="")
    
    while(!okval){
      value<- readline("Please enter the desired value > ")
      
      if(value %in% unlist(posvals)){
        okval <- TRUE
      }else
        print("Please enter a valid value")
    }
    
    if(value == "Yes")
      value <- "1"
    if(value == "No")
      value <- "0"
    if(value == "Male")
      value <- "0"
    if(value == "Female")
      value <- "1"
    if(value == "Recovered")
      value <- "0"
    if(value == "Deceased")
      value <- "1"
    if(value == "Transferred")
      value <- "2"
    
    #Only if you want to divide the subset
    try(ds.dataFrameSubset(df.name = 'data', V1.name = paste("data$",variableN,sep=""), V2.name = value, Boolean.operator = "==", newobj = "data", datasources = connections))
    
    input <- readline("Do you wish to apply another filter? Yes=1 / No=0 > " )
    if(input == "0")
      filters <- FALSE
  }
  

}


get_posVal_index <- function(var, possible_vars){
  
  for(i in 1:length(possible_vars)){
    
    if(is.na(var)){
      
      if(is.na(possible_vars[i]))
        return (i)
      else if(possible_vars[i] == "NA"){
        return (i)
      }
      
    }else if(var == possible_vars[i])
      return (i)
    
  }
  
}
