
local vgui = vgui
local table = table
local pairs = pairs
local pcall = pcall
local ScrW = ScrW
local ScrH = ScrH
local Msg = Msg
local unpack = unpack

require("timer")

local timer = timer

module("vguix")

local active_frames = {}

local function FrameFinished( box, returnValue )
	if (active_frames[box].timername) then
		timer.Destroy( active_frames[box].timername )
	end
	active_frames[box].frame:PostMessage("Close", "", "")
	if (active_frames[box].callback) then
		local result, error
		
		if (#active_frames[box].cbargs > 0) then
			result,error = pcall( active_frames[box].callback, unpack(active_frames[box].cbargs), returnValue )
		else
			result,error = pcall( active_frames[box].callback, returnValue )
		end
		
		if (result == false and error) then
			Msg("LUA Error: " .. error .. "\n")
		end

	end
	active_frames[box] = nil
end

local function FrameTimerExpire( panel )
	for index,qbox in pairs(active_frames) do
		if (qbox.frame == panel) then
			qbox.timername = nil
			FrameFinished( index, qbox.default )
			return
		end
	end
end

local function AlertBoxClosed( panel, message )
	if (message ~= "Command") then return end
	
	for index,qbox in pairs(active_frames) do
		if (qbox.frame == panel) then
			FrameFinished( index )
			return
		end
	end	
end

function AlertBox(text, title, btntext, callback, timeout, ... )
	if (title == nil) then title = "Alert" end
	if (btntext == nil) then btntext = "Ok" end
	
	local alert_box = {}
	alert_box.cbargs = arg
	alert_box.callback = callback
	alert_box.frame = vgui.Create( "MessageBox" )
	alert_box.frame:PostMessage( "SetTitle", "text", title )
	alert_box.frame:PostMessage( "SetText", "text", text )
	alert_box.frame:PostMessage( "SetButtonText", "text", btntext )
	alert_box.frame:SetActionFunction( AlertBoxClosed )
	
	if (timeout and timeout > 0) then
		alert_box.timername = "alert_box" .. (#active_frames)+1
		timer.Create(alert_box.timername, timeout, 1, FrameTimerExpire, alert_box.frame)
	end
	
	table.insert(active_frames, alert_box)

	alert_box.frame:DoModal()
end

local function QuestionButtonPressed( panel, message )
	if (message ~= "Command") then return end

	for index,qbox in pairs(active_frames) do
		for index2,qboxbtn in pairs(active_frames[index].buttons) do
			if (qboxbtn.panel == panel) then
				FrameFinished( index, index2 )
				return
			end
		end
	end	
end

function QuesionBox(text, title, btntexts, callback, timeout, default, ... )
	if (title == nil) then title = "Question" end
	
	local buttons = ""
	local width = (#btntexts + 1) * 80
	local height = 130
	local x = 80
	
	for index,text in pairs(btntexts) do
		local singlebutton = [[
			"Button]] .. index .. [["
			{
				"ControlName"		"Button"
				"fieldName"			"Button]] .. index .. [["
				"xpos"				"]] .. x .. [["
				"ypos"				"]] .. height - 35 .. [["
				"zpos"				"290"
				"wide"				"70"
				"tall"				"25"
				"autoResize"		"0"
				"pinCorner"			"0"
				"visible"			"1"
				"enabled"			"1"
				"tabPosition"		"0"
				"labelText"			"]] .. text .. [["
				"textAlignment"		"west"
				"dulltext"			"0"
				"brighttext"		"0"
				"wrap"				"0"
				"Command"			"null"
				"Default"			"0"
			}
		]]
		
		buttons = buttons .. singlebutton
		
		x = x + 75
	end
	

	local resource = [[
		"question_box"
		{
			"question_box_frame"
			{
				"ControlName"			"Frame"
				"fieldName"				"question_box_frame"
				"xpos"					"]] .. ScrW() / 3 .. [["
				"ypos"					"]] .. ScrH() / 3 .. [["
				"zpos"					"290"
				"wide"					"]] .. width .. [["
				"tall"					"]] .. height .. [["
				"autoResize"			"0"
				"pinCorner"				"0"
				"visible"				"1"
				"enabled"				"1"
				"tabPosition"			"0"
				"settitlebarvisible"	"1"
				"title"					"]] .. title .. [["
			}
			"Label"
			{
				"ControlName"		"Label"
				"fieldName"			"Label"
				"xpos"				"10"
				"ypos"				"30"
				"zpos"				"290"
				"wide"				"]] .. width-20 .. [["
				"tall"				"]] .. height-70 .. [["
				"autoResize"		"0"
				"pinCorner"			"0"
				"textAlignment"		"north"
				"visible"			"1"
				"enabled"			"1"
				"tabPosition"		"0"
				"wrap"				"1"
				"labelText"			"]] .. text .. [["
			}
			]] .. 
			
				buttons
			
			 .. [[
		}
	]]
	
	local question_box = {}
	
	question_box.cbargs = arg
	question_box.buttons = {}
	question_box.callback = callback
	question_box.default = default
	question_box.frame = vgui.Create( "Frame" )
	question_box.frame:SetName( "question_box_frame" )

	for index,text in pairs(btntexts) do
		local button = vgui.Create( "Button", question_box.frame, "Button" .. index )	// The last 2 parameters are optional
		button:SetActionFunction( QuestionButtonPressed )
		
		question_box.buttons[index] = {}
		question_box.buttons[index].panel = button
		question_box.buttons[index].text = text
	end	
	
	question_box.frame:LoadControlsFromString( resource )

	if (timeout and timeout > 0) then
		question_box.timername = "question_box" .. (#active_frames)+1
		timer.Create(question_box.timername, timeout, 1, FrameTimerExpire, question_box.frame)
	end
	
	table.insert(active_frames, question_box)
	
	question_box.frame:SetVisible(true)
	question_box.frame:DoModal()
end

