--Players module v2 for ULX GUI -- by Stickly Man!
--Handles all user-based commands, such as kick, slay, ban, etc.

-----------------------------------------
--Test animated panel
-----------------------------------------
local PANEL = {}

function PANEL:Init()
	self.Items = {}
	self:SetMouseInputEnabled( true )
	self.lx, self.ly = self:ScreenToLocal()
	self.offset = 0
	self.maxoffset = 0
	self.height = 0
	self.width = 0
	self.selected = 0
	self.highlighted = 0
	self.curve = false
	self.active = false
	
	self.cTableCount = 0
	self.ColorTable = { Color( 25, 125, 25, 255 ), 
						Color( 0, 0, 185, 255 ),
						Color( 185, 0, 0, 255 ),
						Color( 128, 128, 128, 255 ),
						Color( 0, 128, 128, 255 ),
						Color( 155, 0, 155, 255 ),
						Color( 200, 100, 0, 255 ), 
						Color( 0, 0, 170, 255 ),
						Color( 128, 50, 50, 255 ) }
end

function PANEL:OnMousePressed( mousecode )
	if mousecode == MOUSE_LEFT then
		self.selected = self.highlighted
	elseif mousecode == MOUSE_RIGHT then
		
	elseif mousecode == MOUSE_MIDDLE then
		
	end
end

function PANEL:GetNewCategoryColor()
	self.cTableCount = self.cTableCount + 1
	if self.cTableCount > #self.ColorTable then 
		self.cTableCount = 1
	end
	return self.ColorTable[self.cTableCount]
end

function PANEL:CalculateValues()
	self.lx, self.ly = self:ScreenToLocal() --I should find a better place to put these
	self.height = self:GetTall()
	self.width = self:GetWide()
	self.offset = -self.height/2
	self.maxoffset = table.Count( self.Items )*20 - self.height/2
	for k, v in pairs( self.Items ) do
		self.maxoffset = self.maxoffset + #v * 20
	end
end

function PANEL:AddCategory( text, color )
	self.Items[text] = { color = color }
end

function PANEL:AddItem( category, text )
	if self.Items[category] == nil then
		Error( "XGUI Animated List - AddItem: Category does not exist!" )
		return
	end
	table.insert( self.Items[category], text )
end

function PANEL:Clear()
	self.Items = {}
end

function PANEL:Think()
	if gui.MouseX() > -self.lx and gui.MouseX() < -self.lx + self.width and gui.MouseY() > -self.ly and gui.MouseY() < -self.ly + self.height then
		self.active = true
	end
	if self.active then
		if gui.MouseX() < -self.lx or gui.MouseX() > -self.lx + self.width then
			self.active = false
			return
		end
		local mYpos = gui.MouseY() + self.ly - self.height/2
		if math.abs( mYpos ) > 25 then
			local calcoffset = ( mYpos < 0 ) and 25 or -25
			self.offset = self.offset + ( ( mYpos + calcoffset ) / 30 )
			if self.offset < -self.height/2 then
				self.offset = -self.height/2
			elseif self.offset > self.maxoffset then
				self.offset = self.maxoffset
			end
		end
	end
end

function PANEL:Paint()
	surface.SetDrawColor( 46, 46, 46, 255 )
	surface.DrawRect( 0, 0, self.width, self.height )
	
	local checkhighlight = false
	if gui.MouseX() > -self.lx and gui.MouseX() < -self.lx + self.width then
		checkhighlight = true
	end
	local my = gui.MouseY() + self.ly
	local y = -self.offset
	for label, data in pairs( self.Items ) do  --Categories
		if y > -20 and y < self.height then
			--Basically a WordBox, with some modified code
			local x = self.curve and 5+math.abs(y-(self.height/2))/10 or 5
			draw.RoundedBox( 4, x, y, self.width - 10, 16, data.color )
			surface.SetFont( "ChatFont" )
			surface.SetTextColor( Color(255,255,255,255 ) )
			surface.SetTextPos( x + 20, y )
			surface.DrawText( label )
		end
		y = y + 20
		for i, label in ipairs( data ) do  --Items
			local col = data.color
			local x = self.curve and 5+math.abs(y-(self.height/2))/10 or 5
			if label .. i == self.selected then
				col = Color( 0, 75, 205, 255 )
			elseif checkhighlight and my > y and my < y + 20 then
				self.highlighted = label .. i
				col = Color( 175, 150, 0, 255 )
			end
			draw.RoundedBox( 4, x+10, y, self.width - 20, 16, col )
			surface.SetFont( "DefaultFixedDropShadow" )
			surface.SetTextColor( Color(255,255,255,255 ) )
			surface.SetTextPos( x + self.width - 20 - string.len( label )*6, y + 3 )
			surface.DrawText( label )
			y = y + 20
		end
	end
end
derma.DefineControl( "XGUI_AnimatedList", "", PANEL, "DPanel" )

function x_makeanimatedlist( t )
	local xgui_temp = vgui.Create( "XGUI_AnimatedList", t.parent )
	xgui_temp:SetSize( t.w, t.h or 20 )
	xgui_temp:SetPos( t.x, t.y )
	return xgui_temp
end


local player = x_makeXpanel{ parent=xgui.null }
bob = x_makeanimatedlist{ x=0, y=30, w=150, h=300, parent=player }
bob:AddCategory( "Test", bob:GetNewCategoryColor() )
x_makebutton{ x=250, y=30, w=250, label="Add Test Item! (Click if scrolling isn't working)", parent=player }.DoClick = function()
	bob:AddItem( "Test", "Test" .. #bob.Items["Test"] )
	bob:CalculateValues()
end
x_makecheckbox{ x=10, y=340, label="Curved", parent=player, textcolor=color_black }.OnChange = function( self, bVal )
	bob.curve = bVal
end

function player.cmd_refresh()
	bob:Clear()
	bob.cTableCount = 0
	bob:AddCategory( "Test", bob:GetNewCategoryColor() )
	for cmd, data in pairs( ULib.cmds.translatedCmds ) do
		if data.opposite ~= cmd && ULib.ucl.query( LocalPlayer(), cmd ) then
			local catname = data.category
			if catname == nil or catname == "" then catname = "Uncategorized" end
			if bob.Items[catname] == nil then
				bob:AddCategory( catname, bob:GetNewCategoryColor() )
			end
			bob:AddItem( catname, string.gsub( cmd, "ulx ", "" ) )
		end
	end
	ULib.queueFunctionCall( bob.CalculateValues, bob )
end

table.insert( xgui.hook["onOpen"], player.cmd_refresh )
table.insert( xgui.modules.tab, { name="Test", panel=player, icon="gui/silkicons/user", tooltip=nil, access=nil } )