local _, Ns = ...

------------------------------
--		Global Updater		--
------------------------------

Ns.armorValue, Ns.armorDebuff, Ns.ohID = 0, 0, 0
Ns.dodgeChance, Ns.parryChance, Ns.blockChance, Ns.avoid, Ns.defAdjust, Ns.avoidBase = 0, 0, 0, 0, 0, 0

function Ns.DefenseUpdate()

 	Ns.targetLevel = Ns.playerLevel + 3
	if UnitCanAttack("player", "target") then Ns.targetLevel = UnitLevel("target")
		if Ns.targetLevel <= 0 then Ns.targetLevel = Ns.playerLevel + 3 end
	end

	-- Armor
	local _, effectiveArmor, _, _, negBuff = UnitArmor("player")
	Ns.armorValue = effectiveArmor
	Ns.armorDebuff = negBuff

	-- Block, Parry, Dodge
	Ns.blockChance = GetBlockChance()
	Ns.parryChance = GetParryChance()
	Ns.dodgeChance = GetDodgeChance()

	-- Avoidance / Crushing
	local ohID = GetInventoryItemID("player", 17)

	local miss = 5

	if ohID == nil then  Ns.blockChance = 0 end

	Ns.avoid = (miss + Ns.dodgeChance + Ns.parryChance + Ns.blockChance)
	Ns.avoidBase = (miss + Ns.dodgeChance + Ns.parryChance + Ns.blockChance)
end
------------------------------------------------

-- Armor
function Ns.FunctionList.Armor(HUD, data, options, ...)

	local returnText
	local debuffColor = ""

	if Ns.armorDebuff < 0 then
		debuffColor = "|cffC41E3A"
	end

	returnText = debuffColor .. Ns.armorValue

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Defense
function Ns.FunctionList.Defense(HUD, data, options, ...)

	local defense = Ns.defenseRating

	HUD:UpdateText(data, defense)
end
------------------------------------------------

-- Resilience
function Ns.FunctionList.Resilience(HUD, data, options, ...)

	local Resilience = GetCombatRatingBonus(15)
	local resiDamage = GetCombatRatingBonus(16)
	local returnText

	resiDamage = resiDamage + Resilience

	returnText = ("%.2f"):format(Resilience) .. "/" .. ("%.2f"):format(resiDamage) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Mitigation
function Ns.FunctionList.DMGReduc(HUD, data, options, ...)

	local returnText
	local debuffColor = ""

	local armorReduction = (Ns.armorValue/(Ns.armorValue+400+85*((5.5 * Ns.targetLevel)-265.5))) * 100
	if Ns.armorDebuff < 0 then
		debuffColor = "|cffC41E3A"
	end
	
	returnText = debuffColor .. ("%.2f"):format(armorReduction) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Crit Immunity
function Ns.FunctionList.CritReceived(HUD, data, options, ...)

	local chanceGetCrit = 19
	local getCritLevel = (Ns.targetLevel - Ns.playerLevel) * 0.2
	local getCritDefense = (Ns.defenseRating) * 0.04
	local Resilience = GetCombatRatingBonus(15)
	local debuffColor = ""
	local returnText

	chanceGetCrit = chanceGetCrit + getCritLevel - getCritDefense - Ns.survFittest - Resilience
	if chanceGetCrit > 5.6 then
		chanceGetCrit = 5 + getCritLevel - ((Ns.defenseRating - Ns.baseDefense) * 0.04) - Ns.survFittest - Resilience
	end
	if chanceGetCrit < 0 then
		chanceGetCrit = 0
		debuffColor = "|cff00f26d"
	elseif chanceGetCrit <= 1.5 then
		debuffColor = "|cffFF7C0A"
	else
		debuffColor = "|cffC41E3A"
	end

	returnText = debuffColor .. ("%.3f"):format(chanceGetCrit) .. "%" .. "|r"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Dodge
function Ns.FunctionList.Dodge(HUD, data, options, ...)

	local dodge = Ns.dodgeChance
	local returnText

	returnText = ("%.2f"):format(dodge) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Parry
function Ns.FunctionList.Parry(HUD, data, options, ...)

	local parry = Ns.parryChance
	local returnText

	returnText = ("%.2f"):format(parry) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Block
function Ns.FunctionList.Block(HUD, data, options, ...)

	local block = Ns.blockChance
	local returnText

	returnText = ("%.2f"):format(block) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Avoidance
function Ns.FunctionList.Avoidance(HUD, data, options, ...)

	local avoidColor = ""
	local defAdjust = (5 + (Ns.defenseRating - (Ns.playerLevel * 5)) * 0.04)
	local avoid = (defAdjust + Ns.dodgeChance + Ns.parryChance + Ns.blockChance)
	local returnText

	avoid = avoid - Ns.scorpidSting - Ns.insectSwarm


	if avoid >= 102.4 then
		avoidColor = "|cff00f26d+"
	else
		avoidColor = ""
	end

	returnText = avoidColor .. ("%.2f"):format(avoid) .. "%" .. "|r"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------

-- Crushing
function Ns.FunctionList.Crushing(HUD, data, options, ...)

	local crushColor = ""
	local defAdjust = (5 + (Ns.defenseRating - (Ns.playerLevel * 5)) * 0.04)
	local avoid = (defAdjust + Ns.dodgeChance + Ns.parryChance + Ns.blockChance)
	local returnText

	avoid = avoid - Ns.scorpidSting - Ns.insectSwarm

	local crush = 102.4 - avoid

	if crush <= 0 then
		crushColor = "|cff00f26d+"
	else
		crushColor = ""
	end

	returnText = crushColor .. ("%.2f"):format(crush) .. "%"

	HUD:UpdateText(data, returnText)
end
------------------------------------------------