local _, Ns = ...


local SameLevel, BossLevel, Both = 1, 2, 3

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

function Ns.MeleeUpdate()

	Ns.targetLevel = Ns.playerLevel + 3
	Ns.targetLvlBase = Ns.playerLevel

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

	Ns.targetLvlBase = UnitLevel("target")
	if Ns.targetLvlBase == -1 then
		Ns.targetLvlBase = Ns.playerLevel + 3
	elseif Ns.targetLvlBase == 0 then
		Ns.targetLvlLive = Ns.playerLevel
	end
	end

	Ns.mhID = GetInventoryItemID("player", 16);
	Ns.ohID = GetInventoryItemID("player", 17);

	Ns.hitRating = GetCombatRatingBonus(CR_HIT_MELEE)

	if Ns.hitRating == nil then
		Ns.hitRating = 0
	end
	Ns.hitChance = GetHitModifier("player")
	if Ns.hitChance == nil then
		Ns.hitChance = 0
	end

	Ns.hitChance = Ns.hitChance + Ns.hitRating + Ns.heroicPresence

end
------------------------------------------------

-- Attack Power
function Ns.FunctionList.AP(HUD, data, options, ...)

	local base, posBuff, negBuff = UnitAttackPower("player")
	local effectiveAP = base + posBuff + negBuff
	local returnText

	if negBuff < 0 then
		returnText = "|cffC41E3A" .. effectiveAP .. "|r"
	else
		returnText = effectiveAP
	end

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

-- Damage
function Ns.FunctionList.DMG(HUD, data, options, ...)


	local lowDmg, hiDmg, offlowDmg, offhiDmg, posBuff, negBuff, percentmod = UnitDamage("player")
	local dmgeffect = hiDmg
	local debuffColor = ""
	local returnText

	dmgeffect = dmgeffect + (dmgeffect * Ns.bloodFrenzy) + Ns.hemoDmg

	if negBuff < 0 then
		debuffColor = "|cffC41E3A"
	end

	returnText = debuffColor .. ("%.0f"):format(dmgeffect)

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

-- DPS
function Ns.FunctionList.mDPS(HUD, data, options, ...)

	local lowDmg, hiDmg, _, _, _, negBuff, _ = UnitDamage("player")
	local mainSpeed = UnitAttackSpeed("player")
	local dmgeffect = hiDmg
	local returnText
	local debuffColor = ""
	local meleeDPS = 0

	dmgeffect = dmgeffect + (dmgeffect * Ns.bloodFrenzy) + Ns.hemoDmg

	if mainSpeed ~= 0 then
		meleeDPS = ((lowDmg + dmgeffect) / 2) / mainSpeed
	end
	if negBuff < 0 then
		debuffColor = "|cffC41E3A"
	end

	returnText = debuffColor .. ("%.1f"):format(meleeDPS)

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

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

	local critChance = GetCritChance("player")
	local returnText

	if Ns.crusader then
		critChance = critChance + Ns.crusaderTalent
	end

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

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

-- Critical vs Boss
function Ns.FunctionList.CritBoss(HUD, data, options, ...)

	local critChance = GetCritChance("player")
	local returnText

	if Ns.crusader then
		critChance = critChance + Ns.crusaderTalent
	end

	local levelDifference = 0
	local critAura = 1.84

	levelDifference = Ns.targetLevel - Ns.playerLevel
	if levelDifference <= 0 then
		levelDifference = 0
		critAura = 0
	end
	critChance = critChance - levelDifference - critAura
	if critChance < 0 then
		critChance = 0
	end

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

	HUD:UpdateText(data, returnText)

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

-- Critical Cap
function Ns.FunctionList.CritCap(HUD, data, options, ...)

	local mainBase, mainMod, offBase, offMod = UnitAttackBothHands("player")
	local critChance = GetCritChance("player")
	local mWeaponSkill = (mainBase + mainMod)
	local returnText

	if Ns.ohID and itemType == Ns.weaponTag then 
		mWeaponSkill = math.min((mainBase + mainMod), (offBase + offMod)) 
		
	end	

	local skillDiff = 365 - mWeaponSkill
	local critSupp = 4.8	
	local dodgeBoss = 5 + (skillDiff * 0.1)
	local glancingBoss = 40	
	local extraWeaponSkill = mWeaponSkill - (Ns.playerLevel * 5)
	local mcritCap = 100 - Ns.missMod - dodgeBoss - glancingBoss + critSupp + (extraWeaponSkill * 0.04)
	local debuffColor = ""

	if critChance > mcritCap then
		debuffColor = "|cffC41E3A"
	elseif critChance == mcritCap then
		debuffColor = "|cffFF7C0A"
	else
		debuffColor = "|cff00f26d"
	end

	if mcritCap > 100 then
		mcritCap = 100
	end

	returnText = debuffColor .. ("%.1f"):format(mcritCap) .. "%" .. "|r"

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

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

	local returnText

	returnText = ("%.2f"):format(Ns.hitChance) .. "%"

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

-- Haste
function Ns.FunctionList.HasteMelee(HUD, data, options, ...)

	local haste = GetCombatRatingBonus(CR_HASTE_MELEE)
	local returnText

	returnText = Ns.meleeHaste + Ns.hasteHands + Ns.serpentSwift + ("%.1f"):format(haste) .. "%"

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

-- Attack Speed
function Ns.FunctionList.weaponSpeed(HUD, data, options, ...)

	local mainSpeed, offSpeed = UnitAttackSpeed("player")
	local returnText

	if offSpeed == nil then
		offSpeed = ""
	else
		offSpeed = "/" .. ("%.1f"):format(offSpeed)
	end

	returnText = ("%.1f"):format(mainSpeed) .. offSpeed

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

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

	local armorPen = GetArmorPenetration()
	local returnText

	returnText = armorPen .. "%"

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

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

	local targetArmorReduced = (Ns.targetArmor - Ns.sunderArmor - Ns.exposeArmor - Ns.faerieFire - Ns.curseOfReck - Ns.shatterArmor) or 0
	local returnText
	local debuffColor = ""

	if (Ns.sunderArmor or Ns.exposeArmor or Ns.faerieFire or Ns.curseOfReck or Ns.shatterArmor) > 0 then
		debuffColor = "|cff00f26d"
	end

	returnText = debuffColor .. targetArmorReduced

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

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

	local expertisePercent, offhandExpertisePercent = GetExpertisePercent()
	local returnText

	if Ns.ohID and offhandExpertisePercent > 0 then
		offhandExpertisePercent = "/" .. offhandExpertisePercent
	else
		offhandExpertisePercent = ""
	end

	returnText = expertisePercent .. offhandExpertisePercent .. "%"

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

-- Damage Reduction
function Ns.FunctionList.BossReduc(HUD, data, options, ...)

	local targetArmorReduced = (Ns.targetArmor - Ns.sunderArmor - Ns.exposeArmor - Ns.faerieFire - Ns.curseOfReck - Ns.shatterArmor) or 0
	local damageReduction = (targetArmorReduced / ((467.5 * Ns.targetLevel) + targetArmorReduced - 22167.5)) * 100
	local returnText
	local debuffColor, valueSign = "", ""

	if (Ns.sunderArmor or Ns.exposeArmor or Ns.faerieFire or Ns.curseOfReck or Ns.shatterArmor) > 0 then
		debuffColor = "|cff00f26d"
	end
		if damageReduction < 0 then
			damageReduction = math.abs(damageReduction)
			valueSign = "+"
		end

		returnText = debuffColor .. valueSign .. ("%.1f"):format(damageReduction) .. "%"

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

-- Miss
function Ns.FunctionList.MeleeMiss(HUD, data, options, ...)

	local EB = options.Level_Same_Boss
	local meleeSameLevel = 0
	local meleeBossLevel = 0
	local mainBase, mainMod, offBase, offMod = UnitAttackBothHands("player")
	local levelDefense = Ns.playerLevel * 5
	local levelDiffDef = 0
	levelDiffDef = Ns.targetLevel - Ns.playerLevel
	local liveDefense = Ns.playerLevel * 5
	local bossDefense = (Ns.playerLevel + levelDiffDef) * 5
	local mWeaponSkill = (mainBase + mainMod)
	local returnText

	local meleeSkillDiff = liveDefense - mWeaponSkill
	local bossSkillDiff = bossDefense - mWeaponSkill
	local bossSkillMod = bossDefense - mWeaponSkill
	Ns.missMod = 0
	local missModboss = 0

	missModboss = meleeBossLevel

	if Ns.ohID then
		_,_,_,_,_,itemType = GetItemInfo(ohID)
	end

	if (Ns.mhID and (Ns.ohID and itemType == Ns.weaponTag)) or (not Ns.mhID and (itemType == Ns.weaponTag)) then
		if (Ns.classFilename == "WARRIOR") or (Ns.classFilename == "ROGUE") or (Ns.classFilename == "HUNTER") then
			if meleeSkillDiff > 10 then
				meleeSameLevel = (26 + ((meleeSkillDiff - 10) * 0.4))
				meleeSameLevel = meleeSameLevel - Ns.hitChance
			elseif meleeSkillDiff <= 10 then
				meleeSameLevel = (24 + ((meleeSkillDiff) * 0.1))
				meleeSameLevel = meleeSameLevel - Ns.hitChance
			end

			if bossSkillDiff > 10 then
				meleeBossLevel = (26 + ((bossSkillDiff - 10) * 0.4))
				meleeBossLevel = meleeBossLevel - Ns.hitChance
				Ns.missMod = (26 + ((bossSkillMod - 10) * 0.4))
				Ns.missMod = Ns.missMod - Ns.hitChance
			elseif bossSkillDiff <= 10 then
				meleeBossLevel = (24 + ((bossSkillDiff) * 0.1))
				meleeBossLevel = meleeBossLevel - Ns.hitChance
				Ns.missMod = (26 + ((bossSkillMod - 10) * 0.4))
				Ns.missMod = Ns.missMod - Ns.hitChance
			end
		end

	elseif (Ns.mhID and (Ns.ohID and itemType ~= Ns.weaponTag)) or (not Ns.mhID and (itemType ~= Ns.weaponTag)) or (Ns.mhID and not Ns.ohID) or (not Ns.mhID and not Ns.ohID) then
		if Ns.druidFormChk() then
			meleeSameLevel = 5 - Ns.hitChance
			meleeBossLevel = 9 - Ns.hitChance
		else
			if meleeSkillDiff > 10 then
				meleeSameLevel = (7 + ((meleeSkillDiff - 10) * 0.4))
				meleeSameLevel = meleeSameLevel - Ns.hitChance
			elseif meleeSkillDiff <= 10 then
				meleeSameLevel = (5 + ((meleeSkillDiff) * 0.1))
				meleeSameLevel = meleeSameLevel - Ns.hitChance
			end

			if bossSkillDiff > 10 then
				meleeBossLevel = (7 + ((bossSkillDiff - 10) * 0.4))
				meleeBossLevel = meleeBossLevel - Ns.hitChance
				Ns.missMod = (7 + ((bossSkillMod - 10) * 0.4))
				Ns.missMod = Ns.missMod - Ns.hitChance
			elseif bossSkillDiff <= 10 then
				meleeBossLevel = (5 + ((bossSkillDiff) * 0.1))
				meleeBossLevel = meleeBossLevel - Ns.hitChance
				Ns.missMod = (5 + ((bossSkillMod - 10) * 0.1))
				Ns.missMod = Ns.missMod - Ns.hitChance
			end
		end

	end

	if meleeSameLevel < 0 then
		meleeSameLevel = 0
	elseif meleeSameLevel > 100 then
		meleeSameLevel = 100
	end
	if meleeBossLevel < 0 then
		meleeBossLevel = 0
	elseif meleeBossLevel > 100 then
		meleeBossLevel = 100
	end

	if Ns.Band(EB, SameLevel) then returnText = ("%.1f"):format(meleeSameLevel) .. "%" end
	if Ns.Band(EB, BossLevel) then returnText = ("%.1f"):format(meleeBossLevel) .. "%" end
	if Ns.Band(EB, Both) then returnText = ("%.1f"):format(meleeSameLevel) .. "%" .. " / " .. ("%.1f"):format(meleeBossLevel) .. "%" end

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