
--===================================================
--									
--								GHI Area Buff
--								ghi_areaBuff.lua
--
--	Handling of buffs applied to all GHI users within a given range
--	
-- 						(c)2011 The Gryphonheart Team
--								All rights reservated
--===================================================

local class;
function GHI_AreaBuff()
	if class then
		return class;	
	end
	
	class = GHClass("AreaBuff");
	local position = GHI_Position();
	local comm = GHI_Comm();
	local RecieveAreaBuff;
	local MAX_RANGE = 50;
	
	class.Send = function(buffName,buffDetails,buffIcon,untillCanceled,filter,buffType,buffDuration,cancelable,stackable,count,delay,range)
		local buffData = {	
			buffName=buffName,
			buffDetails=buffDetails,
			buffIcon=buffIcon,
			untillCanceled=untillCanceled,
			filter=filter,
			buffType=buffType,
			buffDuration=buffDuration,
			cancelable=cancelable,
			stackable=stackable,
			count=count or 1,
			delay=delay or 0,
		};	
		
		local playerPos = position.GetPlayerPos();
		
		local players = GHI_AddOns:GetAllPlayers()
		for _,player in pairs(players) do
			comm.Send(nil,player,"AreaBuff",playerPos,range or 0,buffData)
		end			
	end
	
	RecieveAreaBuff = function(player,playerPos,range,data,...)
		if GHI_MiscData["block_area_buff"] then 
			return 
		end
		
		if playerPos.continent > 0 and position.IsPosWithinRange(playerPos,min(range,MAX_RANGE)) then
			ApplyGHIBuff(data.buffName,data.buffDetails,data.buffIcon,data.untillCanceled,data.filter,data.buffType,data.buffDuration,data.cancelable,data.stackable,data.count or 1,data.delay or 0)			
		end		
	end
	comm.AddRecieveFunc("AreaBuff",RecieveAreaBuff);
	
	return class;
end