#Conditional Hooks
$Application: FS2_Open

$On Game Init:
[
PromptBox = {}

function PromptBox:Init(noenable)

	ba.print("*****Initializing PromptBox System...\n")

	self.Enabled = true
	self.FreeMode = false
	self.Visible = false
	self.HideAXM = true
	self.Result = 0
	self.Locked = {}
	self.Gauges = {"PromptBoxOpt1", "PromptBoxOpt2", "PromptBoxOpt3", "PromptBoxOpt4", "PromptBoxOpt5"}
	self.List = {}
			
	self:LoadMsgBox("messagebox3_big_b")
	
	self.Head = {}
	
	AXUI.InGame = true
	AXUI:ScreenInit()
	AXUI:SetFocus(0)
	
	self.PlayerMouse = io.MouseControlStatus
	self.Actions = {"afterburners-locked", "primaries-locked", "secondaries-locked"}
	if not self.Flags then
		self.Flags = {}
	end
		
end

function PromptBox:LoadMsgBox(filename)

	ba.print("LoadMsgBox called\n")

	if filename then
		self.Image = {file = filename, w = gr.getImageWidth(filename), h = gr.getImageHeight(filename)}
	end
	self.Position = {}
	
	self.Position.x = (gr.getScreenWidth() * (0.5) - (self.Image.w/2))
	self.Position.y = (gr.getScreenHeight() * ( 0.7 ))

end

function PromptBox:MakeList()
			
	self:UpdateText()
			
	self.Menu = AXUI:CreateListBox(self.List, self.Position.x + 25, self.Position.y + 50, self.Image.w - 20, self.Image.h - 50, "white", "silver", "bright_white", false, 0, 2)
	self.Menu.FontHeight = self.Menu.FontHeight + 2
	self.Menu:UpdateList(0)

end

function PromptBox:UpdateText()

	self.List = self:GetTextFromGauges()
	self.TitleText = hu.getHUDGaugeHandle("PromptBox").Text
	
	--gr.setColor(255,255,255,255)
	--gr.drawString("DEBUG:", 600,50)
	
	if self.List and self.Menu then
		for i, v in pairs(self.Menu.Item) do
			v.Text = self.List[i]
			gr.CurrentFont = gr.Fonts[2]
			v:CalculateRegion()
			gr.CurrentFont = gr.Fonts[2]
			--gr.drawString(v.Text .. ": " .. v.TextWidth .. " // " .. gr.getStringWidth(self.List[i]))
		end
	end


end

function PromptBox:GetTextFromGauges()
	
	local t = {}
	for i, v in pairs(self.Gauges) do
		local gauge = hu.getHUDGaugeHandle(v).Text
			if gauge:gsub("%s+", "") ~= "" and not self.Locked[i] then
				t[i] = ">> " .. gauge
			else
				t[i] = ""
			end
	end
	
	return t

end

function PromptBox:SetMsgGaugeVisible(visible)

	--ba.print("SetMsgGaugeVisible called\n")
	
	local retailGauges
	local customGauges
	
	--self.Enabled = visible

	if visible then -- turn on
		retailGauges = "false"
		self.Visible = true
		if AxHUD then AxHUD.Visible = false end
		ad.playGameSound(29) --The sound that plays when you bring up the PromptBox
		io.setCursorHidden(false)
	else -- turn off
		retailGauges = "true"
		self.Visible = false
		if AxHUD then AxHUD.Visible = true end
		--self.Menu = nil
		self.Locked = {}
		self.Result = 0
		io.setCursorHidden(true)
	end

	mn.runSEXP("(hud-activate-gauge-type !Center reticle! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Lock indicator! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Threat indicator! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Throttle! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Weapon energy! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Afterburner energy! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Mini shield! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Radar! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Player shield! (".. retailGauges.."))")
	mn.runSEXP("(hud-activate-gauge-type !Target shield! (".. retailGauges.."))")
		
	if gr.getScreenWidth() == 1024 then
	
		mn.runSEXP("(hud-activate-gauge-type !Ets retail! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Kills! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Target monitor! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Weapons! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Mission time! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Countermeasures! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Auto speed! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Auto target! (".. retailGauges.."))")
		mn.runSEXP("(hud-activate-gauge-type !Extra target data! (".. retailGauges.."))")
		
	end
	
	if PromptBox.AllowPause == true then
		if visible then
			mn.runSEXP("(set-time-compression !1!)")
		else
			mn.runSEXP("(reset-time-compression)")
		end
	end

end

function PromptBox:SetHead(filename)

	if not filename then
		if self.Head.Filename then
			self.Head.Filename:unload()
		end
		self.Head = {}
	else
		self.Head.Filename = gr.loadTexture(filename, true)
		self.Head.X = self.Position.x - self.Head.Filename:getWidth() - 2
		self.Head.Y = self.Position.y
	end

end

function proBoxInit()

	PromptBox:Init()

end

function proBoxOn()

	ba.print("proBoxOn called\n")
	if not PromptBox.Enabled then
		PromptBox:Init()
	end
	
	if PromptBox.HideAXM and AXMessage and AXMessage.Enabled then
		AXMessage.Enabled = false
		--Berserk.CanDraw = false
		PromptBox.AXMessage = true
	end
	
	--[[if PromptBox.HideAXM and AxHUD and AxHUD.Visible then
		AxHUD.Visible = false
		PromptBox.AxHUD = true
	end]]--
	
	PromptBox:MakeList()
	PromptBox:SetMsgGaugeVisible(true)

end

function proBoxOff()

	ba.print("proBoxOff called\n")
	PromptBox:SetMsgGaugeVisible(false)
	PromptBox.UnlockTimer = mn.getMissionTime() + 0.15
	
	if PromptBox.HideAXM and AXMessage and PromptBox.AXMessage then
		AXMessage.Enabled = true
		--Berserk.CanDraw = true
		PromptBox.AXMessage = nil
	end
	
	--[[if PromptBox.HideAXM and AxHUD and PromptBox.AxHUD then
		AxHUD.Visible = true
		PromptBox.AxHUD = nil
	end]]--

end

function proBoxAllowPause()

	PromptBox.AllowPause = true
	
end

function proBoxDisallowPause()

	PromptBox.AllowPause = false
	
end

function proBoxModeFree()

	PromptBox.FreeMode = true

end

function proBoxModeLock()

	PromptBox.FreeMode = false

end

function proBoxInFreeMode()

	local result = 0
	
	if PromptBox.FreeMode == true then
		result = 1
	end
	
	return result
	
end

function proBoxSet(num)
	
	PromptBox.Locked = {}
	
	for i = num+1, 5 do
		proBoxLock(i)
	end

end

function proBoxHead(filename)

	PromptBox:SetHead(filename)

end

function proBoxLock(num)
	
	PromptBox.Locked[num] = true

end

function proBoxClear()

	mn.runSEXP("(hud-set-text !PromptBox! ! !)")
	mn.runSEXP("(hud-set-text !PromptBoxOpt1! ! !)")
	mn.runSEXP("(hud-set-text !PromptBoxOpt2! ! !)")
	mn.runSEXP("(hud-set-text !PromptBoxOpt3! ! !)")
	mn.runSEXP("(hud-set-text !PromptBoxOpt4! ! !)")
	mn.runSEXP("(hud-set-text !PromptBoxOpt5! ! !)")

end

function proBoxValue()

	local result = 0
	
	if PromptBox.Enabled and PromptBox.Visible then
		result = PromptBox.Result
	end
	
	return result
	
end

function PromptBox:LockPlayer()

	if hv.Player:isValid() and not self.PlayerLocked then
	
		self.PlayerLocked = true
	
		local playerName = hv.Player.Name

			if self.PlayerMouse then
				io.MouseControlStatus = false
			end
			
			self.Flags = {}
						
			for __,v in pairs(self.Actions) do
				if not mn.evaluateSEXP("(are-ship-flags-set !" .. playerName .. "! !" .. v .. "!)") then
					mn.runSEXP("(alter-ship-flag !" .. v .. "! ( true ) ( false ) !" .. playerName .. "!)")
					self.Flags[#self.Flags+1] = v
				end
			end
			
			mn.runSEXP("(ignore-key !-1! !C!)")
	
	end

end

function PromptBox:UnlockPlayer()

	if hv.Player:isValid() and self.PlayerLocked then
	
		self.PlayerLocked = nil
		self.UnlockTimer = nil

		local playerName = hv.Player.Name

		if self.PlayerMouse then
			io.MouseControlStatus = true
		end
					
		for __,v in pairs(self.Flags) do
			mn.runSEXP("(alter-ship-flag !" .. v .. "! ( false ) ( false ) !" .. playerName .. "!)")
		end
		
		self.Flags = {}
		
		mn.runSEXP("(ignore-key !0! !C!)")
	
	end

end

function PromptBox:Draw()

	gr.drawImage(self.Image.file, self.Position.x, self.Position.y)

	self:UpdateText()
		
	gr.CurrentFont = gr.Fonts[2]
	AXUI:SetColor("white")
	
	gr.drawString(self.TitleText, self.Position.x + 25, self.Position.y + 22)
	
	self.Menu:DrawList()
	
	if self.Head.Filename then
		gr.setColor(hu.getHUDGaugeColor(25))
		gr.drawMonochromeImage(self.Head.Filename[1], self.Head.X, self.Head.Y)
	end
	
end

function PromptBox:MonitorActions()

	local line = 0

	if self.Menu and self.Visible then
		for i, v in ipairs(self.Menu.Item) do
			if v:Clicked() then
				ad.playInterfaceSound(0)
				line = i
			end
		end
	end
	
	return line
	
end

function PromptBox:Exit()

	self.Enabled = nil
	self.AllowPause = nil
	self.FreeMode = nil
	self.Visible = nil
	self.HideAXM = nil
	self.Result = nil
	self.Locked = nil
	self.Gauges = nil
	self.Image = nil
	self:SetHead()
	self.Head = nil	
	self.PlayerMouse = nil
	self.Actions = nil
	self.Flags = nil

end
]

$State: GS_STATE_GAME_PLAY
$On State Start:
[
	if PromptBox.Enabled and PromptBox.Visible then
		io.setCursorHidden(false)
	end
]
$On Gameplay Start:
[
	PromptBox:Init(true)
]

$On State End:
[
	if PromptBox.Enabled then
		PromptBox.Enabled = false
		--io.setCursorHidden(true)
	end
]

$On HUD Draw:
[
	if PromptBox.Enabled then
		if PromptBox.Visible then
			PromptBox:UpdateText()
			PromptBox:Draw()
			PromptBox:LockPlayer()
			
			if NoSDL2 then
				io.setCursorHidden(false)
			end
			
		end
	
	end
	if PromptBox.UnlockTimer and PromptBox.UnlockTimer < mn.getMissionTime() then
		PromptBox:UnlockPlayer()
	end
	
	--[[if PromptBox.Enabled then
		PrintDebug(PromptBox)
	end]]--
]

$On Mouse Released:
[
	if PromptBox.Enabled then
		PromptBox.Result = PromptBox:MonitorActions()
	end
]

$On Key Released:
[
	if hv.Key == "Alt-C" then
		if PromptBox.FreeMode then
			if PromptBox.Visible then
				proBoxOff()
			else
				proBoxOn()
			end
		end
	end
]

$On Mission End:
[
	if PromptBox.Enabled then -- Give mouse controls back if we didn't yet
		PromptBox.Enabled = nil
		PromptBox.FreeMode = nil
		if PromptBox.PlayerMouse == true and io.MouseControlStatus == false then
			io.MouseControlStatus = true
		end
		proBoxOff()
		PromptBox:Exit()
		
	end
]

#End