local AddName, Ns = ...

------------------------------
--		Options Equates		--
------------------------------
local Damage, Percent, Live = 1, 1, 1
local DamageTaken, Rating, Static = 2, 2, 2
local Both = 3
-- SpeedBar droplist equates (match ListData.SpeedBar values)
local Auto, SteadyFlying, Skyriding, OnFoot, Custom = 1, 2, 3, 4, 5

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

	local EB = options.Percent_Rating
	local totalCrit, rating = 0, 0
	local returnText, decimals, statusbar
	local critChance = GetCritChance()
	local critSpellChance = GetSpellCritChance()
	local critRangedChance = GetRangedCritChance()
	local meleeRating, rangedRating, spellRating = GetCombatRating(CR_CRIT_MELEE), GetCombatRating(CR_CRIT_RANGED), GetCombatRating(CR_CRIT_SPELL)
	totalCrit = max(critChance, critSpellChance, critRangedChance)
	local baseCrit = totalCrit
	rating = max(meleeRating, rangedRating, spellRating)
	local capColor, endColor, rateColor = "", "", ""

	totalCrit = totalCrit + Ns.combustion + Ns.betweenEyes + Ns.overflowEnergy

	if options.Display_Basic then totalCrit = baseCrit end

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if totalCrit >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if rating >= options.Cap_Rating then rateColor = Ns.greenText end

	rating = BreakUpLargeNumbers(rating)

	if options.Cap_Percent == 0 then capColor = "" end
	if options.Cap_Rating == 0 then rateColor = "" end

	if Ns.Band(EB, Percent) then returnText = capColor .. (decimals):format(totalCrit); statusbar = totalCrit end
	if Ns.Band(EB, Rating) then returnText = rateColor .. rating; statusbar = rating end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(totalCrit) .. endColor .. "/" .. rateColor .. rating .. endColor; statusbar = totalCrit end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Percent_Rating
	local haste = GetHaste()
	local rating = GetCombatRating(CR_HASTE_MELEE)
	local returnText, decimals, statusbar
	local capColor, endColor, rateColor = "", "", ""

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if haste >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if rating >= options.Cap_Rating then rateColor = Ns.greenText end

	if options.Cap_Percent == 0 then capColor = "" end
	if options.Cap_Rating == 0 then rateColor = "" end

	rating = BreakUpLargeNumbers(rating)

	if Ns.Band(EB, Percent) then returnText = capColor .. (decimals):format(haste); statusbar = haste end
	if Ns.Band(EB, Rating) then returnText = rateColor .. rating; statusbar = rating end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(haste) .. endColor .. "/" .. rateColor .. rating .. endColor; statusbar = haste end

	-- if options.Stat_Color then
	-- 	Ns.StatColors = CreateColor(options.Stat_Color.r, options.Stat_Color.g, options.Stat_Color.b)
	-- 	Ns.StatColors = Ns.StatColors:GenerateHexColorMarkup()
	-- end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Percent_Rating
	local mastery = GetMasteryEffect()
	local rating = GetCombatRating(CR_MASTERY)
	local returnText, decimals, statusbar
	local capColor, endColor, rateColor = "", "", ""

	if options.Display_Basic then mastery = mastery end

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if mastery >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if rating >= options.Cap_Rating then rateColor = Ns.greenText end

	if options.Cap_Percent == 0 then capColor = "" end
	if options.Cap_Rating == 0 then rateColor = "" end

	rating = BreakUpLargeNumbers(rating)

	if Ns.Band(EB, Percent) then returnText = capColor .. (decimals):format(mastery); statusbar = mastery end
	if Ns.Band(EB, Rating) then returnText = rateColor .. rating; statusbar = rating end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(mastery) .. endColor .. "/" .. rateColor .. rating .. endColor; statusbar = mastery end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Damage_Taken
	local dmg = GetVersatilityBonus(29) + GetCombatRatingBonus(29)
	local mitigate = GetVersatilityBonus(31) + GetCombatRatingBonus(31)
	local rating = ""
	local returnText, decimals, statusbar
	local capColor, endColor = "", ""

	if options.Display_Rating then 
		rating = GetCombatRating(29)
		rating = BreakUpLargeNumbers(rating)
		rating = " (" .. rating .. ")"
	end

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if dmg >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if options.Cap_Percent == 0 then capColor = "" end

	if Ns.Band(EB, Damage) then returnText = capColor .. (decimals):format(dmg) .. endColor .. rating; statusbar = dmg end
	if Ns.Band(EB, DamageTaken) then returnText = capColor .. (decimals):format(mitigate) .. endColor .. rating; statusbar = mitigate end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(dmg) .. endColor .. "/" .. (decimals):format(mitigate) .. rating; statusbar = dmg end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Percent_Rating
	local avoidance = GetAvoidance()
	local rating = GetCombatRating(CR_AVOIDANCE)
	local returnText, decimals, statusbar
	local capColor, endColor, rateColor = "", "", ""

	avoidance = avoidance + Ns.martial

	if options.Display_Basic then avoidance = avoidance end

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if avoidance >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if rating >= options.Cap_Rating then rateColor = Ns.greenText end

	if options.Cap_Percent == 0 then capColor = "" end
	if options.Cap_Rating == 0 then rateColor = "" end

	rating = BreakUpLargeNumbers(rating)

	if Ns.Band(EB, Percent) then returnText = capColor .. (decimals):format(avoidance); statusbar = avoidance end
	if Ns.Band(EB, Rating) then returnText = capColor .. rating; statusbar = rating end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(avoidance) .. endColor .. "/" .. rateColor .. rating .. endColor; statusbar = avoidance end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Percent_Rating
	local leech = GetLifesteal()
	local rating = GetCombatRating(CR_LIFESTEAL)
	local returnText, decimals, statusbar
	local capColor, endColor, rateColor = "", "", ""

	decimals = Ns.DecimalFormat(options.Decimals_Two, "%%")

	if leech >= options.Cap_Percent then capColor = Ns.greenText end
	if capColor ~= "" then endColor = "|r" end

	if rating >= options.Cap_Rating then rateColor = Ns.greenText end

	if options.Cap_Percent == 0 then capColor = "" end
	if options.Cap_Rating == 0 then rateColor = "" end

	rating = BreakUpLargeNumbers(rating)

	if Ns.Band(EB, Percent) then returnText = capColor .. (decimals):format(leech); statusbar = leech end
	if Ns.Band(EB, Rating) then returnText = rateColor .. rating; statusbar = rating end
	if Ns.Band(EB, Both) then returnText = capColor .. (decimals):format(leech) .. endColor .. "/" .. rateColor .. rating .. endColor; statusbar = leech end

	HUD:UpdateText(data, returnText)
	HUD:UpdateBar(data, statusbar)
end

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

	local EB = options.Speed_Static
	local speedColor, staticColor, endColor = "", "", ""
	local vehicleSpeed = GetUnitSpeed("vehicle")
	local fullSpeed, runSpeed, flightSpeed, swimSpeed = GetUnitSpeed("player")
	local isGliding, canGlide, forwardSpeed = C_PlayerInfo.GetGlidingInfo()
	local staticSpeed
	local returnText, decimals, statusbar

	if IsFlying() then staticSpeed = flightSpeed
	elseif IsSwimming() then staticSpeed = swimSpeed
	elseif vehicleSpeed > 0 then staticSpeed = vehicleSpeed
	elseif UnitOnTaxi("player") then staticSpeed = fullSpeed
	else staticSpeed = runSpeed end

	if fullSpeed == 0 and vehicleSpeed > 0 then fullSpeed = vehicleSpeed end

	fullSpeed = fullSpeed / 0.07
	staticSpeed = staticSpeed / 0.07

	if canGlide then
		local base = isGliding and forwardSpeed or GetUnitSpeed("player")
		local movespeed = Round(base / BASE_MOVEMENT_SPEED * 100)
		fullSpeed = movespeed
		staticSpeed = movespeed
	end

	if fullSpeed == 0 or fullSpeed == 100 then speedColor = ""
	elseif fullSpeed <= 99 then speedColor = Ns.redText
	elseif fullSpeed > 100 then speedColor = Ns.greenText end

	if staticSpeed == 0 or staticSpeed == 100 then staticColor = ""
	elseif staticSpeed <= 99 then staticColor = Ns.redText
	elseif staticSpeed > 100 then staticColor = Ns.greenText end

	if speedColor ~= "" then endColor = "|r" end

	decimals = Ns.DecimalFormat(options.Decimals_Default, "%%")

	if Ns.Band(EB, Live) then
		returnText = speedColor .. (decimals):format(fullSpeed)
		statusbar = fullSpeed
	end
	if Ns.Band(EB, Static) then
		returnText = staticColor .. (decimals):format(staticSpeed)
		statusbar = staticSpeed
	end
	if Ns.Band(EB, Both) then
		returnText = speedColor .. (decimals):format(fullSpeed) .. endColor .. " (" .. staticColor .. (decimals):format(staticSpeed) .. ")"
		statusbar = fullSpeed
	end

	HUD:UpdateText(data, returnText)
	-- SpeedBar mode determines the bar's max value
	local mode = options.SpeedBar or Auto
	local barMax = 107
	if mode == SteadyFlying then barMax = 570
	elseif mode == Skyriding then barMax = 1220
	elseif mode == OnFoot then barMax = 110
	elseif mode == Custom and options.BarMaxCustom and options.BarMaxCustom > 0 then
		barMax = options.BarMaxCustom
	elseif mode == Auto then
		barMax = 107
		if IsFlying() then
			barMax = 570
			if canGlide then barMax = 1220 end
		end
	end
	HUD:UpdateBar(data, statusbar, barMax)
end