--[[
	console/console_console.lua
	fangli :d | (STEAM_0:0:19962914)
	===DStream===
]]


PANEL.Base = "DPanel"

--
-- NOTE: We only create the HTML control when the console is shown
--		 This is because of a source engine bug when creating two
--		 HTML panels at the same time
--

local Queue = {}

--
-- HACK: For those with +toggleconsole in the command line!
--
local ConsoleMinShowTime = SysTime() + 2

function PANEL:Init()

	self:SetDrawBackground( false )
	self:InvalidateLayout()
	
	self.Input = self:Add( "DTextEntry" )
	self.Input:Dock( BOTTOM )
	self.Input:DockMargin( 0, 5, 0, 0 )
	self.Input:SetHeight( 25 )
	self.Input:SetHistoryEnabled( true )
	self.Input:SetAllowNonAsciiCharacters( true )
	self.Input.OnEnter = function( i )
		
		local command = i:GetText()
		
		self.HTML:RunJavascript( string.format( "Command( \"%s\");", command:JavascriptSafe() ) )
		
		i:SetText( "" )
		i:RequestFocus()
		
		-- A good laugh
		surface.PlaySound( "garrysmod/content_downloaded.wav" )
		
		RawConsoleCommand( command )
		i:AddHistory( command )
		
		timer.Simple( 0.01, function() self.HTML:ScrollToBottom() end )
		
	end
	self.Input.AllowInput = function( i, c ) return c == '`' end
	self.Input.GetAutoComplete = function( i, t ) return ConsoleAutoComplete( t ) end
	self.Input:SetTabbingDisabled( true )
	
end

function PANEL:Ready()

	if ( !IsValid( self.HTML ) ) then return false end
	if ( self.HTML:IsLoading() ) then return false end
	
	return true

end

function PANEL:CreateConsole()

	if ( IsValid ( self.HTML ) ) then return end
	
	self.HTML = self:Add( "DHTML" )
	self.HTML:Dock( FILL )
	self.HTML:OpenURL( "html/console.html" );
	self.HTML:SetKeyboardInputEnabled( true )

end

function PANEL:Print( str, r, g, b, a )

	if ( !self:Ready() ) then 
		table.insert( Queue, function() self:Print( str, r, g, b, a ) end )
	return end
	
	str = string.format( "Print( \"%s\", %i, %i, %i, %i );", str:JavascriptSafe(), r, g, b, a )
	
	self.HTML:RunJavascript( str )
	
	timer.Simple( 0.01, function() self.HTML:ScrollToBottom() end )
	
end

function PANEL:OnVisible()

	self.Input:RequestFocus()
	
	if ( ConsoleMinShowTime > SysTime() ) then
		return timer.Simple( 0.5, function() self:OnVisible() end )
	end

	self:CreateConsole()
	self.Input:RequestFocus()
	
	timer.Simple( 0.01, function() self:FlushBuffer() end )
		
end

function PANEL:Clear()
	
	if ( !self:Ready() ) then 
		table.insert( Queue, function() self:Clear() end )
	return end
	
	self.HTML:RunJavascript( "Clear()" )
		
end

function PANEL:OnLuaError( realm, err, stck )

	if ( !self:Ready() ) then 
		table.insert( Queue, function() self:OnLuaError( realm, err, stck ) end )
	return end

	local js = string.format( "LuaError( \"%s\", \"%s\" );", realm:JavascriptSafe(), err:JavascriptSafe() )
	self.HTML:RunJavascript( js )
	
	for k, v in pairs( stck ) do
	
		local js = string.format( "AddStack( %i, %i, \"%s\", \"%s\" );", k, v.line, v.func:JavascriptSafe(), v.file:JavascriptSafe() )
		self.HTML:RunJavascript( js )
	
	end
	
end

function PANEL:FlushBuffer()
	
	if ( !self:Ready() ) then 
		timer.Simple( 0.01, function() self:FlushBuffer() end )
	return end
	
	for k, v in pairs( Queue ) do
		v();
	end
	
	Queue = {}

end