﻿B4J=true
Group=Misc
ModulesStructureVersion=1
Type=Class
Version=8.5
@EndOfDesignText@
'WebSocket class
Sub Class_Globals
	Private ws As WebSocket
	'these variables will be set automatically.
	Private Txt As JQueryElement 
	Private Result As JQueryElement
	Private btnSubmit As JQueryElement
	Private Progress As JQueryElement
	Private JavaExe, JavaFXLibs As String
	Private ThreadIndex As Int
	Private su As StringUtils
	Private Socket As UDPSocket
	Private Const QRServerPort As Int = 51042
	Private MessageIndex As Int
	Private bc As ByteConverter
End Sub

Public Sub Initialize
	'assuming that using OpenJDK 11+
	JavaExe = File.Combine(GetSystemProperty("java.home", ""), "bin/java")
	JavaFXLibs = File.Combine(GetSystemProperty("java.home", ""), "javafx/lib")
	ThreadIndex = Main.srvr.CurrentThreadIndex
	Socket.Initialize("Socket", 0, 8192)
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
	ws = WebSocket1
End Sub

Private Sub WebSocket_Disconnected
End Sub

Private Sub Socket_PacketArrived (Packet As UDPPacket)
	MessageIndex = MessageIndex + 1
	Dim res As Byte = Packet.Data(0)
	If res = 1 Then
		Dim sb As StringBuilder
		sb.Initialize
		sb.Append("<img width=400 height=400 src='data:image/png;base64,")
		sb.Append(su.EncodeBase64(File.ReadBytes(File.DirApp, $"qr/${ThreadIndex}.png"$)))
		File.Delete(File.DirApp, $"qr/${ThreadIndex}.png"$)
		sb.Append("'/img>")
		Log("qr sb.length: " & sb.Length)
		Result.SetHtml(sb.ToString)
	Else
		Result.SetText("Error creating QR.")
	End If
	EnableControls
End Sub

Sub btnSubmit_Click (Params As Map)
	SendToQRServer
End Sub

Sub txt_KeyUp(Params As Map)
	If 13 =Params.Get("which") Then
		SendToQRServer
	End If
End Sub

Private Sub EnableControls
	btnSubmit.SetProp("disabled", False)
	Txt.SetProp("disabled", False)
	Progress.SetCSS("visibility", "hidden")
	ws.Flush
End Sub

Private Sub SendToQRServer
	Dim Text As String = Txt.GetVal.Value
	File.WriteString(File.DirApp, $"qr/${ThreadIndex}.txt"$, Text)
	btnSubmit.SetProp("disabled", True)
	Txt.SetProp("disabled", True)
	Progress.SetCSS("visibility", "visible")
	Result.SetHtml("")
	Dim packet As UDPPacket
	packet.Initialize(bc.IntsToBytes(Array As Int(ThreadIndex)), "127.0.01", QRServerPort)
	Socket.Send(packet)
	ws.Flush
	MessageIndex = MessageIndex + 1
	Dim MyIndex As Int = MessageIndex
	Sleep(10000)
	If MyIndex = MessageIndex Then
		'this means that the server hasn't responded in 10 seconds.
		Result.SetText("QR server not responding. Starting QR server. Try again in a few seconds.")
		StartQRServer
		EnableControls
	End If
End Sub


Private Sub StartQRServer
	If Main.StartingQRServer = True Then 'Boolean variable
		Log("qr server already being started")
		Return
	End If
	Main.StartingQRServer = True
	Try
		Log("Starting QR server")
		Dim args As List
		args.Initialize
		Dim shl As Shell
		args.AddAll(Array As String("@release_java_modules.txt", "-p", JavaFXLibs, "-jar", "QRCmdLine.jar"))
		'ONLINE is a build configuration symbol which is used when building for the online server.
		#if ONLINE
		args.InsertAt(0, JavaExe)
		shl.Initialize("shl", "xvfb-run", args)
		#else
		shl.Initialize("shl", JavaExe, args)
		#End If
		shl.WorkingDirectory = File.DirApp
		shl.Run(-1)
		Sleep(30000)
		Main.StartingQRServer = False
		Log("Setting StartingQRServer to false")
	Catch
		Log(LastException)
		Main.StartingQRServer = False
	End Try
End Sub

Private Sub Shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
	Log("QR server exited!")
	Log(StdOut)
	Log(StdErr)
End Sub

