-----------------------------------
--		Targeting Behaviour      --
-----------------------------------
--[[

True = True target. Who the mob would have aggro on, if we discount secondary targetting and taunts, etc.
Curr = Current target. Who the mob's target unitid is
New  = New target. If the mob's current target has changed

x, y = players with known threat values
nil  = no target
?    = player with unknown threat value


True	Curr	New		Result
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
nil		nil		x		true and curr become x

x		x		y		curr -> y. In 2 seconds with no change, true -> y. also, if their threat goes above 110 of yours in that time, put them up.
x		x		?		curr -> ?. In 2 seconds with no change, true -> ?
x		x		nil		curr -> nil.

x		y		z		curr -> z. In 2 secs, true -> z
x		y		nil		curr -> nil.
x		y		?		curr -> ?. In 2 secs, true -> ?
x		y		x		curr -> x

x		nil		y		If it's been nil for more than 1 second, true = y. Otherwise true -> y after 2 - (secs at nil) secs.
x		nil		x		curr -> x
x		nil		?		same as x - nil - y

?		?		x		true -> x. Easy enough.

Taunt Behaviour:

Gives you as much as 

]]

-- Master Target Variables
KLHTM_MTTrueTarget = nil	-- The Name of the player who this mod thinks is the true target
KLHTM_MTCurrentTarget = nil	-- The Name of the player the mob is currently targetting
KLHTM_MTTargetSwapTime = 0	-- The time when the mob last changed its target
KLHTM_UnknownTarget = "#unknown"

KLHTM_MasterTargetTarget = nil

function KLHTM_TargettingOnUpdate()

	if KLHTM_MasterTarget == nil then
		KLHTM_RecalculateAggroGain()
	
	else
		KLHTM_UpdateTrueAggroTarget()
	end

end

function KLHTM_UpdateTrueAggroTarget()	
		
	-- 1) find a UnitID for the master target
	local mastertargetid = nil
	
	if UnitName("target") == KLHTM_MasterTarget then
		mastertargetid = "target"
		
	else
		-- check everyone in the raid
		local x
		
		for x = 1, 40 do
			if UnitName("raid" .. x .. "target") == KLHTM_MasterTarget then
				mastertargetid = "raid" .. x .. "target"
				break
			end
		end
	end
	
	-- 2) If noone can see the mob, give up.
	if mastertargetid == nil then
		
		KLHTM_MTTrueTarget = KLHTM_UnknownTarget
		KLHTM_MTCurrentTarget = KLHTM_UnknownTarget
		
		return
	end
	
	-- 3) Get the boss' current target
	local targetnow = UnitName(mastertargetid .. "target")
	 
	-- 4) Reevaluate True, Current, Time
	
	-- a) Transitions from true=unknown	
	if (KLHTM_MTTrueTarget == nil) or (KLHTM_MTTrueTarget == KLHTM_UnknownTarget) or (KLHTM_RaidData[KLHTM_MTTrueTarget] == nil) then
		
		-- debug print
		if targetnow ~= KLHTM_MTTrueTarget then
			if targetnow == nil then
				KLHTM_PrintTrace("TargetDebug", "Target changed from bad to nil.")
			else
				KLHTM_PrintTrace("TargetDebug", "Target changed from bad to " .. targetnow .. ".")
				
				if (KLHTM_KnockbackDiscoveryIsActive == true) and (targetnow == UnitName("player")) then
					KLHTM_SendMessage("aggrogain " .. KLHTM_MyRaidThreat())
				end
			end
		end
		
		KLHTM_MTTrueTarget = targetnow
		KLHTM_MTCurrentTarget = targetnow
	
	-- b) Transitions from true = known
	elseif targetnow ~= KLHTM_MTCurrentTarget then
	
		if KLHTM_MTCurrentTarget ~= nil then
			KLHTM_MTTargetSwapTime = GetTime()
		end
	
		KLHTM_MTCurrentTarget = targetnow
		
		if targetnow == nil then
			KLHTM_PrintTrace("TargetDebug", "CurrentTarget changed to nil.")
		else
			KLHTM_PrintTrace("TargetDebug", "CurrentTarget changed to " .. targetnow .. ".")
			
			if (KLHTM_KnockbackDiscoveryIsActive == true) and (targetnow == UnitName("player")) then
				KLHTM_SendMessage("aggrogain " .. KLHTM_MyRaidThreat())
			end
		end
	end
	
	-- 5) Check if CurrentTarget should become Truetarget
	if KLHTM_MTTrueTarget ~= KLHTM_MTCurrentTarget then
		-- to get here, true target is known.
		
		if KLHTM_MTCurrentTarget == nil then
			-- do nothing
			
		elseif KLHTM_RaidData[KLHTM_MTCurrentTarget] == nil then
			-- switch to unknown if it's been more than 2 seconds
			if GetTime() - KLHTM_MTTargetSwapTime > 2 then
				KLHTM_MTTrueTarget = KLHTM_MTCurrentTarget
				KLHTM_PrintTrace("TargetDebug", "TrueTarget switches to the unknown " .. KLHTM_MTTrueTarget .. " after 2 seconds.")
			end
			
		else -- current target is a known user
			if GetTime() - KLHTM_MTTargetSwapTime > 2 then
				KLHTM_MTTrueTarget = KLHTM_MTCurrentTarget
				KLHTM_PrintTrace("TargetDebug", "TrueTarget switches to the known player " .. KLHTM_MTTrueTarget .. " after 2 seconds.")
				
			elseif KLHTM_RaidData[KLHTM_MTCurrentTarget] > KLHTM_ThreatParameters.meleeaggrogain * KLHTM_RaidData[KLHTM_MTTrueTarget] then
				KLHTM_MTTrueTarget = KLHTM_MTCurrentTarget
				KLHTM_PrintTrace("TargetDebug", "TrueTarget switches to the known player " .. KLHTM_MTTrueTarget .. " due to high threat.")				
			end
		end
	end
	
end

function KLHTM_RecalculateAggroGain()

	-- update aggro, and such
	local newaggrogain
	local targetname = ""
	local maxdepth = 5
	local i
	local targetacquired = false
	
	for i = 1, maxdepth do
		targetname = targetname .. "target"
		
		if UnitName(targetname) == nil then
			break
		
		elseif UnitIsFriend("player", targetname) == nil then
			targetacquired = true
			break
		end
	end
	
	if targetacquired == false then
		-- remove aggro gain
		newaggrogain = nil
		
	else
		local mobtarget = UnitName(targetname .. "target")
		if mobtarget == nil then
			mobtarget = "<nil>"
		end
		
		if KLHTM_RaidData[mobtarget] then
			-- aggro target has a known threat value
			
			if UnitName("player") == mobtarget then
				newaggrogain = KLHTM_RaidData[mobtarget]
			
			else
				-- now check our range to the mob
				if CheckInteractDistance(targetname, 1) then
					-- we're in melee range
					newaggrogain = math.ceil(KLHTM_RaidData[mobtarget] * KLHTM_ThreatParameters.meleeaggrogain)
					
				else
					-- there's a small region where we might be in melee range. for now, assume not
					newaggrogain = math.ceil(KLHTM_RaidData[mobtarget] * KLHTM_ThreatParameters.rangeaggrogain)
				end
			end
		else
			newaggrogain = nil
		end
	end
	
	local currentaggrogain = KLHTM_RaidData[KLHTM_String("misc", "aggrogain")]
	if newaggrogain ~= currentaggrogain then
		KLHTM_RaidData[KLHTM_String("misc", "aggrogain")] = newaggrogain
		KLHTM_RedrawTable(KLHTM_RaidWindow)
	end

end
