--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
--|	GypsyMod Library - Gypsy_Command.lua
--|	Slash command management
--|	Respect to Dhargo, creator of Flexbar, for the meta table usage
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--

-- Isolated file version
local version = 1.0;

--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
--|	Version History
--|	1.00 - Initial library with Handler and Add methods.
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--

if (not Gypsy_Command or not Gypsy_Command.version or Gypsy_Command.version < version) then
	Gypsy_Command = {};
	Gypsy_Command.version = version;
	
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	--|	Gypsy_Command:New
	--|	Used to make this library available for use locally from script files. Requires
	--|	a name string be passed, which will be used to store the command and create it's
	--|	aliases. Also accepts the two aliases to use as command1 and command2, and a
	--|	function which it will set to call when either "help" or an unknown command are
	--|	given.
	--|		Usage: local command = Gypsy_Command:New("MYMOD", "/mymod", "/mm", myModHelpHandler);
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	
	function Gypsy_Command:New (name, command1, command2, default)			
		local object = {};
		object.handlers = {};
		object.default = default;
		
		SlashCmdList[name] = function (msg) 
			object:Handler(msg); 
		end;
		setglobal("SLASH_"..name.."1", command1);
		setglobal("SLASH_"..name.."2", command2);
		
		setmetatable(object, self);
		self.__index = self;
		return object;
	end	
	
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	--|	Gypsy_Command:Add
	--|	Adds valid sub-commands for the individual command object created in the New
	--|	method. On top of the command string, it must supply a handler function for the
	--|	command.
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	
	function Gypsy_Command:Add (command, func)
		if (self.handlers[command] or not func) then
			return;
		end
		
		self.handlers[command] = func;
	end
	
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	--|	Gypsy_Command:Handler
	--|	This is the main function which accepts input for every main command which is
	--|	supplied in the New method, and parses it out to individual commands with param-
	--|	eters.
	--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--
	
	function Gypsy_Command:Handler (msg)
		local command, param;		

		local space = strfind(msg, " ");

		if (space) then
			command = strsub(msg, 1, space - 1);
			param = strsub(msg, space + 1);
		else
			command = msg;
			param = nil;
		end

		command = string.lower(command);

		if (not command or not self.handlers[command]) then
			command = "help";
		end

		self.handlers[command](param);
	end
end