
local me = { name = "ability"}
local mod = thismod
mod[me.name] = me

--[[
Abilities.lua

This module handles a few special abilities.
]]

--[[
------------------------------------------------------------------------
		Interface with Core.lua - OnEvent and OnUpdates
------------------------------------------------------------------------
]]

me.myevents = { "UNIT_SPELLCAST_SUCCEEDED", "UNIT_SPELLCAST_SENT" }

me.onevent = function()
	
	if event == "UNIT_SPELLCAST_SUCCEEDED" then
		
		if arg2 == mod.string.get("spell", "invisibility") then
			
			me.invisibility.isactive = true
			me.invisibility.casttime = GetTime()
			me.invisibility.ticks = 0
		end
				
	elseif event == "UNIT_SPELLCAST_SENT" then
		
		if arg2 == mod.string.get("spell", "misdirection") then
			me.misdirection.target = arg4
			me.misdirection.lasttrycast = GetTime()
		end
		
	end
	
end

me.myparsers =
{
	{"buffstart", "AURAADDEDSELFHELPFUL", "CHAT_MSG_SPELL_PERIODIC_SELF_BUFFS"},	-- "You gain %s."
	{"buffend", "AURAREMOVEDSELF", "CHAT_MSG_SPELL_AURA_GONE_SELF"}, -- "%s fades from you."
}

me.onparse = function(identifier, ...)
		
	if identifier == "buffstart" then
		
		if select(1, ...) == mod.string.get("spell", "invisibility") then
			me.invisibility.isactive = false
			mod.table.resetraidthreat()
			
		elseif select(1, ...) == mod.string.get("spell", "misdirection") then
			me.misdirection.isactive = true
			me.misdirection.starttime = GetTime()
			
			-- did we start it
			if me.misdirection.starttime < me.misdirection.lasttrycast + 2.0 then
				me.misdirection.iscaster = true
			else
				me.misdirection.iscaster = false
			end
			
		end
		
	elseif identifier == "buffend" then
		
		if select(1, ...) == mod.string.get("spell", "misdirection") then
			me.misdirection.isactive = false
			me.misdirection.lastfadetime = GetTime()
		end
		
	end
	
end	

me.myonupdates = 
{
	updateinvisibility = 0.0,
	updatemisdirection = 1.0,
}

me.mynetmessages = { "misdirection" }

me.onnetmessage = function(author, command, data)
	
	if command == "misdirection" then
		
		local _, _, player, value = string.find(data, "(.+) (%d+)")
		
		if player == nil then
			return "invalid"
		end
		
		if player == UnitName("player") then
			
			-- check whether misdirection is active on us
			if me.misdirection.isactive or (GetTime() < me.misdirection.fadetime + 2.0) then
				
				-- add threat			
				mod.combat.lognormalevent(mod.string.get("spell", "misdirection"), 1, 0, value)
							
			else
				return "invalid"
			end
		
		end
	end
	
end

--[[
------------------------------------------------------------------------
				Mages' Invisibility
------------------------------------------------------------------------
]]

me.invisibility = 
{
	isactive = false,
	casttime = 0,
	ticks = 0,
	
	-- these are constants
	maxticks = 5,			-- ticks until complete invisibility
	levelpertick = 0.1, 	-- fraction of threat reduced
	tickinterval = 1.0,	-- seconds between ticks
}

-- Check for invisibility ticks
me.updateinvisibility = function()

	if me.invisibility.isactive == true then
		
		local ticksnow = math.floor((GetTime() - me.invisibility.casttime) / me.invisibility.tickinterval)
		
		if ticksnow > me.invisibility.ticks then
			me.invisibility.ticks = me.invisibility.ticks + 1
			
			if me.invisibility.ticks > me.invisibility.maxticks then
				me.invisibility.isactive = false
				return
			
			else
				me.reducethreatfraction(me.invisibility.levelpertick, mod.string.get("spell", "invisibility"))

			end
		end
	end
	
end

--[[
------------------------------------------------------------------------
				Hunters' Misdirection
------------------------------------------------------------------------
]]

me.misdirection = 
{
	target = "",			-- who we targeted, if we cast it
	isactive = false,		-- whether the buff is active
	starttime = 0.0,		-- when we gained the buff
	lasttrycast = 0.0,	-- time we last tried to cast misdirection
	iscaster = false,		-- whether we cast the misdirection or received it
	fadetime = 0.0,		-- when the buff last faded
	
	-- constants
	duration = 30.0,		-- maximum buff duration
}

me.updatemisdirection = function()
	
	if me.misdirection.isactive and (GetTime() > me.misdirection.starttime + me.misdirection.duration) then
		me.misdirection.isactive = false
	end
	
end

--[[
me.reducethreatfraction(multiplier, ability)

<multiplier>	number; positive fraction, e.g. 0.2 for -20%.
<ability>		string; name of the ability used
]]
me.reducethreatfraction = function(multiplier, ability)
	
	local reduction = - mod.table.getraidthreat() * multiplier
	
	mod.combat.lognormalevent(ability, 1, 0, reduction)
	
end

