
----------------------
-- Aggro loss / gain notification

-- This stuff is just debug material, to print out your threat when you gain or lose aggro.
----------------------

KLHTM_NotifyAggro = false
KLHTM_TargetName = "nil"
KLHTM_TargetTargetName = "nil"

function klhtest()

	KLHTM_NotifyAggro = not KLHTM_NotifyAggro
	
	if KLHTM_NotifyAggro == true then
		KLHTM_Print("Now notifying you of aggro changes.")
	else
		KLHTM_Print("No longer notifying you of aggro changes.")
	end
	
end

KLHTM_ThreatBuffer = 0
KLHTM_AnnounceWaiting = false

function KLHTM_ResetAndAnnounce(buffer)

	KLHTM_ResetMyData()

	KLHTM_AnnounceWaiting = true
	KLHTM_ThreatBuffer = buffer

	KLHTM_Print(string.format("You will announce when the threat buffer reaches %d.", buffer))
end

function KLHTM_AlertOnUpdate()
	
	KLHTM_CheckAggro()
	
	if (KLHTM_AnnounceWaiting == true) then
		
		-- find the smallest buffer
		local minbuffer = KLHTM_ThreatParameters.meleeaggrogain / math.max(KLHTM_ThreatParameters.rogue, KLHTM_ThreatParameters.battlestance, KLHTM_ThreatParameters.berserkerstance)
		minbuffer = math.min(minbuffer, KLHTM_ThreatParameters.rangeaggrogain)
		
		if KLHTM_MyData["Total"].threat * minbuffer > KLHTM_ThreatBuffer then
			KLHTM_AnnounceWaiting = false
			KLHTM_ThreatBuffer = 0

			local damagelimit = KLHTM_MyData["Total"].threat * minbuffer
			local target = UnitName("target")
			if target == nil then
				target = "<?? the bad guy>"
			end
			
			KLHTM_Announce(string.format("DPS, KILL %s! KILL KILL KILL! [Buffer = %d]", target, damagelimit))
		end
	end
	
end

function KLHTM_CheckAggro()
	
	if KLHTM_NotifyAggro == false then
		return
	end
	
	-- get current ID's. If you are targetting friend, then use HIS targets
	local targetid = "target"
	local doubletargetid = "targettarget"
	
	if UnitIsFriend("player", "target") then
		targetid = targetid .. "target"
		doubletargetid = doubletargetid .. "target"
	end
	
	-- check for valid targets
	if UnitIsFriend("player", targetid) then
		-- there are no enemies around. don't do anything
		KLHTM_TargetName = "nil"
		return
	end
	
	-- now see if the target has changed
	local targetnow = UnitName(targetid)
	
	if targetnow == nil or targetnow == "Unknown Entity" then
		-- no mob targetted. ignore
		targetnow = "nil"
		return
	end
	
	-- get target target name
	local doubletargetnow = UnitName(doubletargetid)
	if doubletargetnow == nil then
		doubletargetnow = "nil"
	end
	
	-- check for target change
	if targetnow ~= KLHTM_TargetName then
		
		-- target change. ignore targettarget therefore
		KLHTM_TargetName = targetnow
		KLHTM_TargetTargetName = doubletargetnow
		return
	end
	
	-- to get here, target is valid and is the same. we want to see if targettarget is changed
	
	-- is change?
	if doubletargetnow ~= KLHTM_TargetTargetName then
	
		-- changed to nil
		if doubletargetnow == "nil" then
			if UnitIsDead(targetid) then
				-- target lost its target, because it died.
				KLHTM_TargetName = "nil"
		
			else
				-- target temporarily lost its target. probably it is stunned
				-- so just don't update targettarget to nil
			end
	
			return
		end
		
		if KLHTM_TargetTargetName == "nil" then
			-- picked it up from noone. no announce.
			
		elseif KLHTM_TargetTargetName == UnitName("Player") then
			-- we lost aggro
			KLHTM_Announce("I lost aggro to " .. doubletargetnow .. ". His threat should be at least " .. math.ceil(KLHTM_MyData["Total"].threat * 1.1) .. ". " .. KLHTM_EnumerateThreat())
		
		elseif doubletargetnow == UnitName("Player") then
			-- we gained aggro
			KLHTM_Announce("I gained aggro from " .. KLHTM_TargetTargetName .. ". My threat is " .. math.ceil(KLHTM_MyData["Total"].threat) .. ". " .. KLHTM_EnumerateThreat(true))
		end
		
		KLHTM_TargetTargetName = doubletargetnow
	end
	
end

function KLHTM_EnumerateThreat(LastMessage)

	local message = "Damage = " .. KLHTM_MyData["Total"].damage
	
	local key;	local value
	
	for key, value in KLHTM_MyData do
		if key == "Healing" then
			message = message .. ", Healing = " .. value.damage
		
		elseif key ~= "Total" and key ~= "White Damage" then
			if value.hits > 0 then
				message = message .. ", " .. key .. " " .. value.hits .. " hits"
			end
		end
	end
		
	if LastMessage then
	
		local threat = KLHTM_MyData["Total"].threat
		message = message .. ". Threat = " .. KLHTM_FormatDecimal(threat)
	
		if KLHTM_LastAttack then
			threat = threat - KLHTM_LastAttack.threat
			message = message .. ", then " .. KLHTM_FormatDecimal(threat)
		end
		
		if KLHTM_SecondLastAttack then
			threat = threat - KLHTM_SecondLastAttack.threat
			message = message .. ", then " .. KLHTM_FormatDecimal(threat)
		end
	end
	
	message = message .. "."
	
	return message
	
end

function KLHTM_AttackDataToString(Attack)
	
	return "{" .. Attack.name ..", dam = " .. Attack.damage .. ", threat = " .. Attack.threat .. "}"
	
end
