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

*/

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

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 )
	
	// Store as a global so controls can 'baseclass' easier
	_G[ strName ] = strTable
	
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
	
	return 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

