local BUTTON = {}

function BUTTON:Init()
	self.TextLabel = vgui.Create( "DLabel", self );
	self.TextLabel.TextFont = "Default";
	self.TextLabel:SetFont( self.TextLabel.TextFont );
	self.TextLabel:SetText( "" );
	self.TextLabel:SetColor( Colour_White );
	self.TextLabel:SetMouseInputEnabled( false );
	
	self.Color = Color( 80, 80, 80, 255 );
	self.OutlineColor = Color( 0, 0, 0, 255 );
	self.HighlightColor = Color( 120, 120, 120, 255 );
	self.Outline = true;
	
	self.Round = 4;
	self.PaintHook = nil;
	self.Highlighted = false;
	self.TextColor = Color( 255, 255, 255, 255 );
	self.Disabled = false
end

function BUTTON:SetDisabled( bool )
	self.Disabled = bool;
end;

function BUTTON:SetRoundness( amt )
	self.Round = amt;
end;

function BUTTON:OnCursorEntered()
	self.Highlighted = true;
end;

function BUTTON:OnCursorExited()
	self.Highlighted = false;
end;

function BUTTON:SetPaintHook( func )
	self.PaintHook = func;
end;

function BUTTON:OnMousePressed()
	if ( self.Disabled ) then
		return;
	end;
	
	if ( self.Callback ) then
		self.Callback( self );
	end;
end;

function BUTTON:SetHighlightColor( color )
	self.HighlightColor = color;
end;

function BUTTON:SetText( str )
	self.TextLabel:SetText( str );
end;

function BUTTON:SetTextFont( font )
	self.TextLabel.TextFont = font;
	self.TextLabel:SetFont( self.TextFont );
	self:InvalidateLayout();
end;

function BUTTON:SetTextColor( color )
	self.TextColor = color;
	self:InvalidateLayout();
end;

function BUTTON:ApplySchemeSettings()
	self.TextLabel:SetFGColor( self.TextColor );
end;

function BUTTON:SetColor( color )
	self.Color = color;
end;

function BUTTON:EnableOutline( val )
	self.Outline = var;
end;

function BUTTON:SetOutlineColor( color )
	self.OutlineColor = color;
end;

function BUTTON:SetButtonText( text )
	self.TextLabel:SetText( text );
	self.TextLabel:InvalidateLayout();
end;

function BUTTON:SetCallback( func )
	self.Callback = func;
end;

function BUTTON:PerformLayout()
	surface.SetFont( self.TextLabel.TextFont );
	local xoffset, yoffset = surface.GetTextSize( self.TextLabel:GetValue() );
	xoffset = xoffset / 2;
	yoffset = yoffset / 2;

	self.TextLabel:SetPos( ( self:GetWide() / 2 ) - xoffset, ( self:GetTall() / 2 ) - yoffset );
	self.TextLabel:SetSize( self:GetWide(), self:GetTall() );
	self.TextLabel:SizeToContents();
end;

function BUTTON:Paint()
	if( !self.PaintHook or !self.PaintHook( self ) ) then
		local w, h = self:GetSize();
		local color = self.Color;
		local colourOutlined = self.OutlineColor;
		
		if ( self.Highlighted and !self.Disabled ) then
			color = self.HighlightColor;
		end
		
		
		if ( self.Disabled ) then
			draw.RoundedBox( self.Round, 0, 0, w, h, Color( colourOutlined.r, colourOutlined.g, colourOutlined.b, 100 )  );
			draw.RoundedBox( self.Round, 1, 1, w - 2, h - 2, Color( color.r, color.g, color.b, 100 ) );
			self.TextLabel:SetColor( Color( 255, 255, 255, 100 ) );
		elseif ( !self.Outline ) then
			draw.RoundedBox( self.Round, 0, 0, w, h, color );
		else
			draw.RoundedBox( self.Round, 0, 0, w, h, colourOutlined );
			draw.RoundedBox( self.Round, 1, 1, w - 2, h - 2, color );
		end
	end
	
	return true;
end;
vgui.Register( "eps_button", BUTTON, "Button" );
