#############################################################################################
# 1. init lola parameters (as needed), create logging window and global DEBUG procedure
#############################################################################################
SETG sDebug:0

# create the dockable window for the output stream
IF (sDebug) {
  CC "AdoScript" CREATE_OUTPUT_WIN winid:"DEBUG_LOG" title:"Logging"
}
# establish a global procedure as a helper implementation for timestamped logging
 PROCEDURE global DEBUG msgType:string message:string {
   IF (sDebug) {
     CC "Application" GET_DATE_TIME date-format:"dd/mm/yyyy" time-format:"hh:mm:ss"
     SET currentDateTime: ((date) + " " + (time))
     CC "AdoScript" OUT winid:"DEBUG_LOG" text: ("[" + (msgType) + "@" + currentDateTime + "]: " + (message) + "\n")
  }
}
# Initial event log message for the "AppInitialized" event
DEBUG msgType:"INFO" message: ("LoLA triggered")





#############################################################################################
# 2. Establish input (for demonstration manual through an EDITBOX)
#############################################################################################
# a. network query
CC "AdoScript" EDITBOX text:"" title:"Provide network" oktext:"Run"
IF (endbutton = "ok") {
  SET sNetwork:(replall(text, "\r\n", "%0A"))
}
ELSE {
  EXIT
}
# b. formula query
CC "AdoScript" EDITFIELD title:"Enter formula" caption:"Formula (without quotes)" text:"NOFORMULA"
IF (ecode <> 0) {
  EXIT
}
IF (text <> "NOFORMULA") {
  SET sFormula:(replall(text, " ", "%20"))
} ELSE {
  SET sFormula:""
}

# b. formula query
CC "AdoScript" EDITFIELD title:"Enter options" caption:"Options" text:"NOOPTIONS"
IF (ecode <> 0) {
  EXIT
}
IF (text <> "NOOPTIONS") {
  SET sOptions:(replall(text, " ", "%20"))
} ELSE {
  SET sOptions:""
}
DEBUG msgType:"INFO" message:("Network" + sNetwork)
DEBUG msgType:"INFO" message:("Running an analysis with the following settings: --formula \"" + sFormula + "\" " + sOptions)


#############################################################################################
# 3. Run LoLA
#############################################################################################
CC "AdoScript" GET_TEMP_FILENAME
SET sAnalysisLocation:(filename + "\\")
CC "AdoScript" DIR_CREATE path:(sAnalysisLocation)

SET sAnalysisResult:(sAnalysisLocation + "result.log")

CC "AdoScript" GET_CWD
SET oldCwd:(cwd)
CC "AdoScript" SET_CWD path:(sAnalysisLocation)

CC "AdoScript" FILE_COPY from:"db:\\curl.exe" to:(sAnalysisLocation + "curl.exe")
DEBUG msgType:"INFO" message:("Running remote LoLA at " + sAnalysisLocation)
DEBUG msgType:"INFO" message:("curl -X POST --header \"Content-Type: application/x-www-form-urlencoded\" --header \"Accept: application/json\" -d \"network="+sNetwork+"\" \""+sRemoteLoLAURL+"services/rest/lolaservice/analyse?formula="+sFormula+"\" -o "+sAnalysisResult+" -k")
SYSTEM ("curl -X POST --header \"Content-Type: application/x-www-form-urlencoded\" --header \"Accept: application/json\" -d \"network="+sNetwork+"\" \""+sRemoteLoLAURL+"services/rest/lolaservice/analyse?formula="+sFormula+"\" -o "+sAnalysisResult+" -k")



#############################################################################################
# 4. Show result of analysis
#############################################################################################
CC "AdoScript" FREAD file:(sAnalysisResult)
SET sResult:(replall(text, "\\r\\n", "\n"))
SET sResult:(replall(sResult, "\\n", "\n"))
CC "AdoScript" EDITBOX title:"Network analysis result" text:(sResult)