MsgN( "# Hey! This server is allowing the control panel to be run! Woo! Rejoice! #" )
surface.PlaySound( "npc/roller/remote_yes.wav" )

local afkinput
local afkmsg
local nextUse = CurTime( )
local queue = { }
local statusbox
local GetSelectedPlayerSteamID
local volume
local mics
local afk
local steam
local name
local avatar
local panelsection
local panel
local panelheader
local stopsounds
local loopback
local netgraph
local developer
local status = false

local wGUI = { }
	wGUI.Debug = true
	wGUI.Settings = { }
	wGUI.Derma = { }
		wGUI.Derma.MainControlPanel = nil
		wGUI.Derma.AFKMsg = nil

hook.Add( "OnEntityCreated", "WhenPlayerSpawns", function( ent )
	if ent:IsValid( ) and ent:GetClass( ) == "player" and ent == LocalPlayer( ) then
		wGUI.Derma.MainControlPanel = vgui.Create( "DFrame" )
		wGUI.Derma.MainControlPanel:SetSize( 512, 256 )
		wGUI.Derma.MainControlPanel:SetTitle( "waut3r.control.panel." )
		wGUI.Derma.MainControlPanel:SetVisible( false )
		wGUI.Derma.MainControlPanel:SetDraggable( false )
		wGUI.Derma.MainControlPanel:Center( )
		wGUI.Derma.MainControlPanel:SetDeleteOnClose( false ) -- So it doesn't remove the panel every time it's removed...
		wGUI.Derma.MainControlPanel:ShowCloseButton( true )
		wGUI.Derma.MainControlPanel.Paint = function( )
			surface.SetDrawColor( 0, 25, 50, 125 )
			surface.DrawRect( 0, 0, wGUI.Derma.MainControlPanel:GetWide( ), wGUI.Derma.MainControlPanel:GetTall( ) )
		end
			panel = vgui.Create( "DPanel", wGUI.Derma.MainControlPanel )
				panel:SetPos( 4, 25 )
				panel:SetSize( 504, 227 )
				panel.Paint = function( )
					surface.SetDrawColor( 0, 100, 200, 50 )
					surface.DrawRect( 0, 0, panel:GetWide( ), panel:GetTall( ) )
				end
			
			panelheader = vgui.Create( "DPanel", panel )
				panelheader:SetPos( 2, 2 )
				panelheader:SetSize( 500, 36 )
				panelheader.Paint = function( )
					surface.SetDrawColor( 0, 75, 150, 255 )
					surface.DrawRect( 0, 0, panel:GetWide( ), panel:GetTall( ) )
					surface.SetTextColor( 255, 255, 255, 255 ); surface.SetFont( "UiBold" ); surface.SetTextPos( 42, 2 ); surface.DrawText( LocalPlayer( ):Nick( ) );
					surface.SetTextColor( 255, 255, 255, 255 ); surface.SetFont( "UiBold" ); surface.SetTextPos( 42, 18 ); surface.DrawText( LocalPlayer( ):SteamID( ) );

				end
			
			panelsection = vgui.Create( "DPanel", panel )
				panelsection:SetPos( 368, 40 )
				panelsection:SetSize( 134, 185 )
				panelsection.Paint = function( )
					surface.SetDrawColor( 50, 50, 50, 255 )
					surface.DrawRect( 0, 0, panel:GetWide( ), panel:GetTall( ) )
				end

			avatar = vgui.Create( "AvatarImage", panel )
				avatar:SetPos( 4, 4 )
				avatar:SetSize( 32, 32 )
				avatar:SetPlayer( LocalPlayer( ), 32 )
			
			afk = vgui.Create( "DButton", panel )
				afk:SetPos( 434, 8 )
				afk:SetSize( 64, 24 )
				afk:SetText( "Reset" )
			
			afk.DoClick = function( ) afkmsg = nil end
			
				afkinput = vgui.Create( "DTextEntry", panel )
				afkinput:SetPos( 170, 8 )
				afkinput:SetWide( 256 )
				afkinput:SetTall( 24 )
				afkinput:SetEnterAllowed( true )
				afkinput:SetText( "Set your AFK message here and it will auto-respond." )
				afkinput.OnEnter = function( )
					wGUI.Derma.MainControlPanel:SetVisible( false ); wGUI.Derma.MainControlPanel:Close( ); 
					if ( afkinput:GetValue( ) )
						then AFKEnabled = true
						afkmsg = afkinput:GetValue( )
					end
				end
			
			volume = vgui.Create( "DNumSlider", panel )
				volume:SetPos( 372, 146 )
				volume:SetSize( 128, 100 )
				volume:SetText( "General Volume" )
				volume:SetMin( 0 )
				volume:SetMax( 1 )
				volume:SetDecimals( 1 )
				volume:SetConVar( "volume" )
			
			mics = vgui.Create( "DNumSlider", panel )
				mics:SetPos( 372, 186 )
				mics:SetSize( 128, 100 )
				mics:SetText( "Voice Chat Volume" )
				mics:SetMin( 0 )
				mics:SetMax( 1 )
				mics:SetDecimals( 1 )
				mics:SetConVar( "voice_scale" )
			
			stopsounds = vgui.Create( "DButton", panel )
				stopsounds:SetText( "Stop All Sounds" )
				stopsounds:SetPos( 369, 117 )
				stopsounds:SetSize( 132, 24 )
				stopsounds.DoClick = function( ) RunConsoleCommand( "stopsounds" ) end
			
			loopback = vgui.Create( "DNumSlider", panel )
				loopback:SetPos( 372, 92 )
				loopback:SetSize( 128, 20 )
				loopback:SetText( "voice_loopback" )
				loopback:SetMin( 0 )
				loopback:SetMax( 1 )
				loopback:SetDecimals( 0 )
				loopback:SetConVar( "voice_loopback" )
			
			netgraph = vgui.Create( "DNumSlider", panel )
				netgraph:SetPos( 372, 68 )
				netgraph:SetSize( 128, 20 )
				netgraph:SetText( "net_graph" )
				netgraph:SetMin( 0 )
				netgraph:SetMax( 3 )
				netgraph:SetDecimals( 0 )
				netgraph:SetConVar( "net_graph" )
			
			developer = vgui.Create( "DNumSlider", panel )
				developer:SetPos( 372, 44 )
				developer:SetSize( 128, 20 )
				developer:SetText( "developer" )
				developer:SetMin( 0 )
				developer:SetMax( 2 )
				developer:SetDecimals( 0 )
				developer:SetConVar( "developer" )
		 
			status = vgui.Create("DListView", panel)
				status:SetPos( 1, 40 )
				status:SetSize( 365, 161 )
				status:SetMultiSelect( false )
				status:AddColumn( "Name" )
				status:AddColumn( "SteamID" )

			statusbox = vgui.Create( "DTextEntry", panel )
				statusbox:SetPos( 1, 202 )
				statusbox:SetWide( 366 )
				statusbox:SetTall( 24 )
				statusbox:SetEnterAllowed( true )
				statusbox:SetText( "Click above and the info for the player selected will be placed here." )

			GetSelectedPlayerSteamID = ""
			status.OnRowSelected = function( _, line )
                for _, ply in pairs( player.GetAll( )) do
                if ply:Nick( ) == status:GetLine( line ):GetValue( 1 ) then
                GetSelectedPlayerSteamID = ply:Nick( ).." - "..ply:SteamID( )
				statusbox:SetText( GetSelectedPlayerSteamID )
					end
				end
			end
		end
	end )
	

	local function RefreshStatusListOfPlayers( )
		if !status then
			return
		end
		status:Clear( )
		local people = player.GetAll()
		table.sort( people, function( a, b ) return a:Nick( ) < b:Nick( ) end )
			for k,v in pairs( people ) do
			status:AddLine( v:Nick( ), v:SteamID( ) )
		end	
	end; RefreshStatusListOfPlayers( );

function wGUI.SetVar( var, setting )
	wGUI.Settings[var] = setting
	if ( wGUI.Debug ) then
		MsgN( "wGUI.Settings["..var.."] -> "..tostring( setting ) )
	end
end

function wGUI.GetVar( var, default ) return wGUI.Settings[var] or default end


function wGUI.Notify( msg )
	chat.AddText( msg )
	chat.PlaySound( )
end

cvars.AddChangeCallback( "voice_loopback", function( cvar, old, new ) wGUI.SetVar( "LoopBack", tobool( new ) ) end )

local function EmptyQueue( )
	for k,v in pairs( queue ) do
		timer.Simple( k/2, function( )
			RunConsoleCommand( "say", " "..v )
		end )
		table.remove( queue, k )
	end
end

local function addToQueue( msg )
	table.insert( queue, msg )
	EmptyQueue( )
end

hook.Add( "OnPlayerChat", "w_OPC", function( ply, txt )
	if ply != LocalPlayer( ) and string.find( string.lower( txt ), string.lower( LocalPlayer( ):Nick( ) ) ) then
		surface.PlaySound( "waut3r/mention.wav" )
		surface.PlaySound( "waut3r/mention.wav" )
		chat.AddText ("Your name was mentioned!")
		if ( afkmsg ) and ( CurTime( ) >= nextUse ) then
			nextUse = CurTime( ) +30
			addToQueue ( afkmsg )
		end
	end
end )

concommand.Add( "+cp", function( )
		surface.PlaySound( "Friends/friend_join.wav" )
		surface.PlaySound( "Friends/friend_join.wav" )
		wGUI.Derma.MainControlPanel:SetVisible( true ); wGUI.Derma.MainControlPanel:MakePopup( );
		--Refreshing Stuff--
		RefreshStatusListOfPlayers( )
end )

concommand.Add( "-cp", function ( )
	if ( afkinput:IsValid( ) and afkinput:HasFocus( ) ) then return end
	if ( statusbox:IsValid( ) and statusbox:HasFocus( ) ) then return end
	wGUI.Derma.MainControlPanel:SetVisible( false ); wGUI.Derma.MainControlPanel:Close( ); 
end )