-------------------------------
-- SSTRP
-- Author: MNWRP
--
-- hooks.lua
-- A hook system for plugins and other things.
-------------------------------

Hooks = {  };
TeamHooks = { };

function CallTeamHook( hook_name, ply, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10 ) -- Holy shit what a hacky method fo sho.
	
	local team = NilFix(Teams[ply:Team()], nil);
	if( team == nil ) then

		return; -- Team hasn't even been set yet!
		
	end
	
	-- Look through the Hooks table for any hooks that should be called
	for _, hook in pairs( TeamHooks ) do
	
		if( hook.hook_name == hook_name and team.flag_key == hook.flag_key) then
			
			local unique = NilFix(hook.unique_name, "");
			local func = NilFix(hook.callback, function() end);
			
			DayLog( "script.txt", "Running team hook " .. unique );
			
			local override = NilFix(func( ply, NilFix(arg1, nil), NilFix(arg2, nil), NilFix(arg3, nil), NilFix(arg4, nil), NilFix(arg5, nil), NilFix(arg6, nil), NilFix(arg7, nil), NilFix(arg8, nil), NilFix(arg9, nil), NilFix(arg10, nil)), 1);
			
			if( override == 0 ) then return 0; end
			
		end
		
	end
	
	return 1;
	
end

function AddTeamHook( hook_name, unique_name, callback, flagkey )
	
	local hook = {  };
	hook.hook_name = hook_name;
	hook.unique_name = unique_name;
	hook.callback = callback;
	hook.flag_key = flagkey;
	
	table.insert(TeamHooks, hook);
	
	DayLog( "script.txt", "Adding team hook " .. unique_name .. " ( " .. hook_name .. " | " .. flagkey .. " )" );
	
end

-- This is to be called within gamemode functions
-- It will basically run through a table of hooks and call those functions if it matches the hook name.
-- If the hook returns a value of 0, it will not call any more hooks.
function CallHook( hook_name, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10 ) -- Holy shit what a hacky method fo sho.
	
	-- Look through the Hooks table for any hooks that should be called
	for _, hook in pairs( Hooks ) do
	
		if( hook.hook_name == hook_name ) then
			
			local unique = NilFix(hook.unique_name, "");
			local func = NilFix(hook.callback, function() end);
			
			DayLog( "script.txt", "Running hook " .. unique );
			
			NilFix(func( NilFix(arg1, nil), NilFix(arg2, nil), NilFix(arg3, nil), NilFix(arg4, nil), NilFix(arg5, nil), NilFix(arg6, nil), NilFix(arg7, nil), NilFix(arg8, nil), NilFix(arg9, nil), NilFix(arg10, nil)), 1);
			
			if( override == 0 ) then return 0; end
			
		end
		
	end
	
	return 1;
	
end

function AddHook( hook_name, unique_name, callback )
	
	local hook = {  };
	hook.hook_name = hook_name;
	hook.unique_name = unique_name;
	hook.callback = callback;
	
	table.insert(Hooks, hook);
	
	DayLog( "script.txt", "Adding hook " .. unique_name .. " ( " .. hook_name .. " )" );
	
end
