/*   _                                
    ( )                               
   _| |   __   _ __   ___ ___     _ _ 
 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |(  ___/| |   | ( ) ( ) |( (_| |
`\__,_)`\____)(_)   (_) (_) (_)`\__,_) 

*/

local table 			= table
local vgui				= vgui
local concommand		= concommand
local Msg				= Msg
local setmetatable		= setmetatable

local derma_skin = CreateClientConVar( "derma_skin", 
										"Default", 
										true /*Should Save*/, 
										false /*Accessible by server*/ )

module("derma")

Controls = {}
SkinList = {}


local SkinMetaTable = {}
SkinMetaTable.__index = function ( self, key ) 
							return GetDefaultSkin()[key]
						end

/*---------------------------------------------------------
   DefineControl
---------------------------------------------------------*/
function DefineControl( strName, strDescription, strTable, strBase )

	// Add Derma table to PANEL table.
	strTable.Derma = { ClassName 	= strName, 
						Description = strDescription, 
						BaseClass 	= strBase }

	// Register control with VGUI
	vgui.Register( strName, strTable, strBase )
	
	// Store control
	table.insert( Controls, strTable.Derma )
	
end



/*---------------------------------------------------------
   DefineSkin
---------------------------------------------------------*/
function DefineSkin( strName, strDescription, strTable )

	strTable.Name = strName
	strTable.Description = strDescription
	strTable.Base = strBase
	
	if ( strName != "Default" ) then
		setmetatable( strTable, SkinMetaTable )
	end

	SkinList[ strName ] = strTable
	
end


/*---------------------------------------------------------
   GetSkin - Returns current skin for panel
---------------------------------------------------------*/
function GetSkin( panel )

	local SkinName = derma_skin:GetString()
	local Skin = SkinList[ SkinName ]
	if ( Skin ) then return Skin end

	return GetDefaultSkin()

end

/*---------------------------------------------------------
   Returns 'Default' Skin
---------------------------------------------------------*/
function GetDefaultSkin()

	return SkinList[ "Default" ]

end

/*---------------------------------------------------------
   SkinHook( strType, strName, panel )
---------------------------------------------------------*/
function SkinHook( strType, strName, panel )

	local Skin = GetSkin( panel )

	if ( !Skin ) then return end
	local func = Skin[ strType .. strName ]
	if ( !func ) then return end
	
	func( Skin, panel )
	
end


/*---------------------------------------------------------
   SkinTexture( strName )
---------------------------------------------------------*/
function SkinTexture( strName )

	local Skin = GetSkin( panel )

	if ( !Skin ) then return end
	local Textures = Skin.Textures
	if ( !Textures ) then return end
	
	return Textures[ strName ]
	
end


local function OpenTestWindow()

	local Window = vgui.Create( "DFrame" )
		Window:SetSize( 400, 300 )
		Window:Center()
		Window:SetTitle( "Derma Initiative Tests" )
		Window:SetVisible( true )
		Window:MakePopup()
		

end


concommand.Add( "derma_test", OpenTestWindow )
