library(httr)
RETURN_JSON <-0
RETURN_WB <- 1
RETURN_XML <- 2
RETURN_STRING <- 3

GroUtil.WBCall <- function(wb, command, returnType, parameters=NULL, content=NULL){
  return(GroUtil.Call(wb[1],paste("/wb/",wb[2],sep=""),command,returnType,parameters, content))
}

GroUtil.Call <- function(url, identifyer,command,returnType, parameters=NULL, content=NULL){
  
req <- httr::POST(paste(url,identifyer,command,"?",parameters,sep=""),body = content)
if(returnType==RETURN_JSON){
  return(httr::content(req))
}else if(returnType==RETURN_WB){
  jey <- httr::content(req)
  id <- jey['id']
  return(c(url,id))
}else if(returnType==RETURN_XML){
  return(httr::content(req,"text/xml"))
}else{
  return(httr::content(req,"text"))
}
}



GroLink.create <- function(url,type=NULL, name=NULL){
  parameters<-NULL
  if(!is.null(type)){
    parameters<-paste("name=",type,sep="")
  }
  return(GroUtil.Call(url,"/app","/ui/commands/app/create",RETURN_WB,parameters))
}

GroLink.open <- function(url, path=NULL, content=NULL){
  if(!is.null(path)){
    return(GroUtil.Call(url,"/app","/ui/commands/app/open",RETURN_WB,paste("path=",path,sep="")))
  }else if(!is.null(content)){
    return(GroUtil.Call(url,"/app","/ui/commands/app/open",RETURN_WB,content=content))
  }
}



WBRef.listRGGFunctions <- function(wb){
  return(GroUtil.WBCall(wb,"/ui/commands/listFunctions",RETURN_JSON))
}

WBRef.runRGGFunction <- function(wb,name){
  return(GroUtil.WBCall(wb,"/ui/commands/runRGGFunction",RETURN_JSON,paste("name=",name,sep="")))
}

WBRef.runXLQuery <- function(wb,query){
  return(GroUtil.WBCall(wb,"/ui/commands/runXLQuery",RETURN_JSON,content=query))
}

WBRef.compile<- function(wb){
  return(GroUtil.WBCall(wb,"/ui/commands/compile",RETURN_JSON))
}

WBRef.listFiles<-function(wb){
  return(GroUtil.WBCall(wb,"/ui/commands/getSourceFiles",RETURN_JSON))
}
WBRef.renameFile<-function(wb,name,newName){
  return(GroUtil.WBCall(wb,"/ui/commands/renameFile",RETURN_JSON,paste("name=",name,"&newName=",newName,sep="")))
}
WBRef.addFile <-function(wb,inp, content=NULL){
  if(!is.null(content)){
    return(GroUtil.WBCall(wb,"/ui/commands/addFile",RETURN_JSON,paste("name=",inp,sep=""),content))
  }
  return(GroUtil.WBCall(wb,"/ui/commands/addFile",RETURN_JSON,paste("path=",inp,sep="")))
}

WBRef.updateFile <- function(wb,name,content){
  return(GroUtil.WBCall(wb,"/ui/commands/setSourceFileContent",RETURN_JSON,paste("name=",name,sep=""),content))
}

WBRef.removeFile<-function(wb,name){
  return(GroUtil.WBCall(wb,"/ui/commands/removeFile",RETURN_JSON,paste("name=",name,sep="")))
}

WBRef.getFile<-function(wb,name){
  return(GroUtil.WBCall(wb,"/ui/commands/getSourceFileContent",RETURN_STRING,paste("name=",name,sep="")))
}


WBRef.getProjectGraph<-function(wb){
  return(GroUtil.WBCall(wb,"/ui/commands/getProjectGraph",RETURN_STRING))
}

WBRef.addNode <-function(wb,inp, content=NULL){
  if(!is.null(content)){
    return(GroUtil.WBCall(wb,"/ui/commands/addNode",RETURN_JSON,paste("extension=",inp,sep=""),content))
  }
  return(GroUtil.WBCall(wb,"/ui/commands/addNode",RETURN_JSON,paste("path=",inp,sep="")))
}

WBRef.export3d <- function(wb, extension){
  return(GroUtil.WBCall(wb,"/ui/commands/export3d",RETURN_STRING,paste("extension=",extension,sep="")))
}

WBRef.export3d_toFile<-function(wb, path){
  return(GroUtil.WBCall(wb,"/ui/commands/export3d",RETURN_JSON,paste("path=",path,sep="")))
}

WBRef.save<-function(wb,path=NULL){
  if(!is.null(path)){
    return(GroUtil.WBCall(wb,"/ui/commands/saveas",RETURN_JSON,paste("path=",path,sep="")))
  }else{
    return(GroUtil.WBCall(wb,"/ui/commands/saveas",RETURN_STRING))
  }
}

WBRef.close<-function(wb){
   return(GroUtil.WBCall(wb,"/ui/commands/close",RETURN_JSON))
}
WBRef.listDatasets<-function(wb){
  return(GroUtil.WBCall(wb,"/ui/commands/getDatasets",RETURN_JSON))
}
WBRef.getDataset<-function(wb,name){
  return(GroUtil.WBCall(wb,"/ui/commands/getDataset",RETURN_STRING,paste("name=",name,sep="")))
}
