usermessage.Hook( "CPHF", function( um )
	LocalPlayer().Flags[ um:ReadString() ] = 1
end );

local function CheckPlayerHasFlag( flag )
	return LocalPlayer().Flags[ flag ] != nil;
end;

local MainMenu = nil
local MainMenuOpen = nil
local Commands = 
{
	"[ Chat Commands ]\n",
	"// - OOC",
	".// - Local OOC",
	"/me - Me",
	--"/r | /radio - Radio( If you have a radio item )",
	"/ad | /advert - Advert( Costs you 20 credits )",
	"/w - Whisper",
	"/y - Yell\n",
	
	"/givemoney < amount > - Give the one you're looking at money.",
	"/settitle < text > - Set your title.",
	"/setrpname < text > - Set your rp name.",
	"/setflag < name > < flag > - Set a players flag( Admin only )",
	"!setoocdelay < number > - Set the OOC delay( Admin only )\n",
	
	"[ Console Commands ]\n"
};

local PANEL = {}

function PANEL:Init()
	self:SetSize( 500, 480 )
	self:SetPos( ScrW() / 2 - 250, ScrH() / 2 - 240 );
	
	self.CharacterInfo = vgui.Create( "DPanelList" );
	
	self.CharInfoSpawnIcon = vgui.Create( "SpawnIcon", self.CharacterInfo );
	self.CharInfoSpawnIcon:SetIconSize( 64 );
	self.CharInfoSpawnIcon:SetPos( 10, 10 );
	self.CharInfoSpawnIcon:SetModel( LocalPlayer():GetModel() );
	self.CharInfoSpawnIcon:SetToolTip();
	
	local StatsDForm = vgui.Create( "DForm", self.CharacterInfo );
	StatsDForm:SetSize( 270, 155 )
	StatsDForm:SetPos( self:GetWide() - 285, self:GetTall() - 230 );
	StatsDForm:SetName( "Stats" );
	
	local StatsDPanelList = vgui.Create( "DPanelList" );
	StatsDPanelList:SetSize( StatsDForm:GetWide(), StatsDForm:GetTall() );
	StatsDPanelList:SetPadding( 4 );
	StatsDPanelList:SetSpacing( 4 );
	StatsDPanelList:EnableVerticalScrollbar( true );
	StatsDPanelList.PaintOver = function() draw.DrawSimpleOutlined( 0, 0, StatsDPanelList:GetWide(), StatsDPanelList:GetTall(), Colour_Black ) end;
	StatsDForm:AddItem( StatsDPanelList );
	
	self.TestStat = vgui.Create( "eps_drainbar" );
	self.TestStat:SetSize( 0, 25 );
	self.TestStat:SetLabelText( "Strength: 59" );
	self.TestStat:SetDrainW( 10 / 20 );
	StatsDPanelList:AddItem( self.TestStat );
	
	self.Classes = vgui.Create( "DPanelList" );
	self.Classes:SetPadding( 3 );
	self.Classes:SetSpacing( 3 );
	self.Classes:EnableVerticalScrollbar( true );
	
	self.CommandsHelp = vgui.Create( "DPanelList" );
	self.CommandsHelp:SetPadding( 5 );
	self.CommandsHelp:EnableVerticalScrollbar();
	
	for _, v in pairs( Commands ) do
		local HelpLabel = SimpleLabel( nil, v, Color( 255, 255, 255, 255 ), "DefaultBold", nil, nil );
		self.CommandsHelp:AddItem( HelpLabel );
	end;
	
	self.InfoHelp = vgui.Create( "DPanelList" );
	self.Settings = vgui.Create( "DPanelList" );
	
	self:AddSheet( "Character Info", self.CharacterInfo, "gui/silkicons/user", false, false, "Your Character Info" );
	self:AddSheet( "Classes", self.Classes, "gui/silkicons/wrench", false, false, "Select Class" );
	self:AddSheet( "Commands", self.CommandsHelp, "gui/silkicons/shield", false, false, "Commands Info" );
	self:AddSheet( "Info/Help", self.InfoHelp, "gui/silkicons/exclamation", false, false, "Info about the gamemode/Scheme" );
	self:AddSheet( "Settings", self.Settings, "gui/silkicons/brick_add", false, false, "Set your settings!" );
end;

function PANEL:Update()
	self.CharInfoSpawnIcon:SetModel( LocalPlayer():GetModel() );
	self.CharInfoSpawnIcon:SetToolTip();
	
	if ( self.Classes:GetItems() != nil or self.Classes:GetItems() != "" ) then self.Classes:Clear() end;
	
	for k, v in pairs( epsilonScript.Classes.Stored ) do
		local DPanel = vgui.Create( "DPanel" );
		DPanel:SetTall( 66 );
		DPanel.Paint = function() draw.DrawSimpleRect( 0, 0, DPanel:GetWide(), DPanel:GetTall(), Color( 90, 90, 90, 255 ) ) draw.DrawSimpleOutlined( 0, 0, DPanel:GetWide(), DPanel:GetTall(), Colour_Black ); end;
		
		local SpawnIcon = vgui.Create( "SpawnIcon", DPanel );
		SpawnIcon:SetIconSize( 64 );
		SpawnIcon:SetPos( 1, 1 );
		SpawnIcon:SetModel( v.Icon );
		
		if ( !CheckPlayerHasFlag( v.Flag ) and !v.Public ) then
			SpawnIcon.PaintOver = function()
				draw.DrawSimpleRect( 0, 0, SpawnIcon:GetWide(), SpawnIcon:GetTall(), Color( 255, 50, 50, 100 ) );
			end;
			
			SpawnIcon.OnCursorEntered = function() return; end;
			SpawnIcon.PaintOverHovered = function() return; end;
			
			SpawnIcon:SetToolTip( "Requires flag: '" .. v.Flag .. "'" );
		else
			SpawnIcon:SetToolTip();
		end;
			
		SpawnIcon.DoClick = function()
			if ( !CheckPlayerHasFlag( v.Flag ) and !v.Public ) then return; end;
			
			LocalPlayer():ConCommand( "eps_flag " .. v.Flag );
			
			MainMenuOpen = !MainMenuOpen;
			gui.EnableScreenClicker( MainMenuOpen );
			self:SetVisible( MainMenuOpen );
		end;
		
		local NameLabel = SimpleLabel( DPanel, v.Name, Colour_White, "TargetID", 67, 3 );
		local DescriptionLabel = SimpleLabel( DPanel, v.Description, Colour_White, "TargetID", 67, 30 );
		
		self.Classes:AddItem( DPanel );
	end;
end;
vgui.Register( "eps_mainmenuframe", PANEL, "DPropertySheet" );

usermessage.Hook( "eps_mainmenu", function( um )
	MainMenuOpen = !MainMenuOpen;

	gui.EnableScreenClicker( MainMenuOpen );
	
	if ( MainMenu ) then
		MainMenu:SetVisible( MainMenuOpen );
		MainMenu:Update();
	else
		MainMenu = vgui.Create( "eps_mainmenuframe" );
		MainMenu:Update();
	end;
end );