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 = ""
	local decimals
	if options.Dec_Def_Armor == 0 then decimals = "%.0f"
	elseif options.Dec_Def_Armor == 1 then decimals = "%.1f"
	elseif options.Dec_Def_Armor == 2 then decimals = "%.2f"
	else decimals = "%.3f" end

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

	returnText = debuffColor .. (decimals):format(Ns.armorValue)

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

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

	local defense = Ns.defenseRating
	local decimals
	if options.Dec_Def_Def == 0 then decimals = "%.0f"
	elseif options.Dec_Def_Def == 1 then decimals = "%.1f"
	elseif options.Dec_Def_Def == 2 then decimals = "%.2f"
	else decimals = "%.3f" end

	HUD:UpdateText(data, (decimals):format(defense))
	HUD:UpdateBar(data, defense, 490)
end
------------------------------------------------

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

	local Resilience = GetCombatRatingBonus(15)
	local resiDamage = GetCombatRatingBonus(16)
	local returnText
	local decimals
	if options.Dec_Def_Res == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Res == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Res == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	resiDamage = resiDamage + Resilience

	returnText = (decimals):format(Resilience) .. "/" .. (decimals):format(resiDamage)

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

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

	local returnText
	local debuffColor = ""
	local decimals
	if options.Dec_Def_Mit == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Mit == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Mit == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	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 .. (decimals):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
	local decimals
	if options.Dec_Def_Crit == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Crit == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Crit == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	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 .. (decimals):format(chanceGetCrit) .. "|r"

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, chanceGetCrit, 5.6)
end
------------------------------------------------

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

	local dodge = Ns.dodgeChance
	local returnText
	local decimals
	if options.Dec_Def_Dodge == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Dodge == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Dodge == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	returnText = (decimals):format(dodge)

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

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

	local parry = Ns.parryChance
	local returnText
	local decimals
	if options.Dec_Def_Parry == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Parry == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Parry == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	returnText = (decimals):format(parry)

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

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

	local block = Ns.blockChance
	local returnText
	local decimals
	if options.Dec_Def_Block == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Block == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Block == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	returnText = (decimals):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
	local decimals
	if options.Dec_Def_Avoidance == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Avoidance == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Avoidance == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	avoid = avoid - Ns.scorpidSting - Ns.insectSwarm


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

	returnText = avoidColor .. (decimals):format(avoid) .. "|r"

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, avoid, 102.4)
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
	local decimals
	if options.Dec_Def_Crush == 0 then decimals = "%.0f%%"
	elseif options.Dec_Def_Crush == 1 then decimals = "%.1f%%"
	elseif options.Dec_Def_Crush == 2 then decimals = "%.2f%%"
	else decimals = "%.3f%%" end

	avoid = avoid - Ns.scorpidSting - Ns.insectSwarm

	local crush = 102.4 - avoid

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

	returnText = crushColor .. (decimals):format(crush)

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, 102.4 - crush, 102.4)
end
------------------------------------------------