local E, L = unpack(ElvUI)
local _G = _G
local DT = E:GetModule("DataTexts")
local RANGED_ATTACK_POWER = _G.RANGED_ATTACK_POWER
local HIT = _G.HIT
local ITEM_MOD_HIT_RANGED_RATING_SHORT = _G.ITEM_MOD_HIT_RANGED_RATING_SHORT
local STAT_HASTE = _G.STAT_HASTE
local ATTACK_POWER = _G.ATTACK_POWER
local SPEC_FRAME_PRIMARY_STAT_STRENGTH = _G.SPEC_FRAME_PRIMARY_STAT_STRENGTH
local SPEC_FRAME_PRIMARY_STAT_AGILITY = _G.SPEC_FRAME_PRIMARY_STAT_AGILITY
local SPEC_FRAME_PRIMARY_STAT_INTELLECT = _G.SPEC_FRAME_PRIMARY_STAT_INTELLECT
local MELEE_ATTACK_POWER = _G.MELEE_ATTACK_POWER
local ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT = _G.ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT
local ITEM_MOD_SPIRIT_SHORT = _G.ITEM_MOD_SPIRIT_SHORT
local STAT_EXPERTISE = _G.STAT_EXPERTISE
local CRIT_ABBR = _G.CRIT_ABBR
local STAT_MASTERY = _G.STAT_MASTERY
local STAT_VERSATILITY = _G.STAT_VERSATILITY
local STAT_RESILIENCE = _G.STAT_RESILIENCE
local STAT_CRITICAL_STRIKE = _G.STAT_CRITICAL_STRIKE
local ITEM_MOD_HIT_SPELL_RATING_SHORT = _G.ITEM_MOD_HIT_SPELL_RATING_SHORT
local STAT_SPEED = _G.STAT_SPEED
local STAT_LIFESTEAL = _G.STAT_LIFESTEAL
local STAT_AVOIDANCE = _G.STAT_AVOIDANCE
local string = _G.string
local LE_UNIT_STAT_STRENGTH = _G.LE_UNIT_STAT_STRENGTH
local LE_UNIT_STAT_AGILITY = _G.LE_UNIT_STAT_AGILITY
local LE_UNIT_STAT_INTELLECT = _G.LE_UNIT_STAT_INTELLECT
local ITEM_MOD_SPELL_POWER_SHORT = _G.ITEM_MOD_SPELL_POWER_SHORT
local ITEM_MOD_HIT_MELEE_RATING_SHORT = _G.ITEM_MOD_HIT_MELEE_RATING_SHORT
local STAT_CATEGORY_ENHANCEMENTS = _G.STAT_CATEGORY_ENHANCEMENTS
local ITEM_MOD_SPELL_PENETRATION_SHORT = _G.ITEM_MOD_SPELL_PENETRATION_SHORT
local GetCombatRatingBonus = _G.GetCombatRatingBonus
local GetSpellBonusDamage = _G.GetSpellBonusDamage
local GetVersatilityBonus = _G.GetVersatilityBonus
local GetMasteryEffect = _G.GetMasteryEffect
local GetSpellBonusHealing = _G.GetSpellBonusHealing
local UnitAttackPower = _G.UnitAttackPower
local UnitRangedAttackPower = _G.UnitRangedAttackPower
local math = _G.math
local UnitStat = _G.UnitStat
local GetHaste = _G.GetHaste
local GetCritChance = _G.GetCritChance
local GetAvoidance = _G.GetAvoidance
local GetLifesteal = _G.GetLifesteal
local GetSpeed = _G.GetSpeed
local GetSpellCritChance = _G.GetSpellCritChance
local GetRangedCritChance = _G.GetRangedCritChance
local GetExpertise = _G.GetExpertise
local GetArmorPenetration = _G.GetArmorPenetration
local GetMeleeHaste= _G.GetMeleeHaste
local GetRangedHaste = _G.GetRangedHaste
local UnitLevel = _G.UnitLevel
local tostring = _G.tostring
local GetHitModifier = _G.GetHitModifier
local GetSpellHitModifier = _G.GetSpellHitModifier
local GetSpellPenetration = _G.GetSpellPenetration
local GetCombatRating = _G.GetCombatRating
local select = _G.select
local GetBlockChance = _G.GetBlockChance
local GetParryChance = _G.GetParryChance
local GetDodgeChance = _G.GetDodgeChance
local UnitArmor = _G.UnitArmor

--GetTalentTabInfo is going to be removed, use C_SpecializationInfo.GetSpecializationInfo instead TODO

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------stats datatext
local function EltruismStatsDatatextOnEnter()
	local basestat1, currentstat1, statbuff1, statnerf1 = UnitStat('player', LE_UNIT_STAT_STRENGTH)
	local basestat2, currentstat2, statbuff2, statnerf2 = UnitStat('player', LE_UNIT_STAT_AGILITY)
	local basestat3, currentstat3, statbuff3, statnerf3 = UnitStat('player', LE_UNIT_STAT_INTELLECT)
	local basestatlabel, basestat, currentstat,statbuff,statnerf

	if currentstat1 > currentstat2 and currentstat1 > currentstat3 then
		basestatlabel = SPEC_FRAME_PRIMARY_STAT_STRENGTH
		basestat = basestat1
		currentstat = currentstat1
		statbuff = statbuff1
		statnerf = statnerf1
	elseif currentstat2 > currentstat1 and currentstat2 > currentstat3 then
		basestatlabel = SPEC_FRAME_PRIMARY_STAT_AGILITY
		basestat = basestat2
		currentstat = currentstat2
		statbuff = statbuff2
		statnerf = statnerf2
	elseif currentstat3 > currentstat2 and currentstat3 > currentstat1 then
		basestatlabel = SPEC_FRAME_PRIMARY_STAT_INTELLECT
		basestat = basestat3
		currentstat = currentstat3
		statbuff = statbuff3
		statnerf = statnerf3
	else
		basestatlabel = SPEC_FRAME_PRIMARY_STAT_STRENGTH
		basestat = basestat1
		currentstat = currentstat1
		statbuff = statbuff1
		statnerf = statnerf1
	end

	--GetSpellCritChance(value or none at all)

	if E.Retail then
		local retailhaste = GetHaste()
		local retailcrit = math.max(GetCritChance(),GetSpellCritChance())
		local versdmg = GetCombatRatingBonus(29) + GetVersatilityBonus(29)
		local versdef = GetCombatRatingBonus(31) + GetVersatilityBonus(31)
		local avoidance = GetAvoidance()
		local leech = GetLifesteal()
		local speed = GetSpeed()

		DT.tooltip:ClearLines()
		DT.tooltip:AddDoubleLine(basestatlabel..":", ElvUI[1].media.hexvaluecolor..currentstat.."|r",1,1,1)

		--add line here
		DT.tooltip:AddLine(' ')

		DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", retailcrit).."|r",1,1,1)
		DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", retailhaste).."|r",1,1,1)
		DT.tooltip:AddDoubleLine(STAT_MASTERY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r",1,1,1)
		DT.tooltip:AddDoubleLine(STAT_VERSATILITY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", math.max(versdef,versdmg)).."|r",1,1,1)

		--add line here
		DT.tooltip:AddLine(' ')

		DT.tooltip:AddDoubleLine(STAT_AVOIDANCE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", avoidance).."|r",1,1,1)
		DT.tooltip:AddDoubleLine(STAT_LIFESTEAL..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", leech).."|r",1,1,1)
		DT.tooltip:AddDoubleLine(STAT_SPEED..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", speed).."|r",1,1,1)
		DT.tooltip:Show()
	else

		--power
		local rangedbasepower, rangedbuff, rangednerf = UnitRangedAttackPower('player')
		local totalranged = rangedbasepower+rangedbuff+rangednerf

		local meleebasepower, meleebuff, meleenerf = UnitAttackPower('player')
		local totalmelee = meleebasepower+meleebuff+meleenerf

		local spellpower = math.max(GetSpellBonusDamage(2),GetSpellBonusDamage(3),GetSpellBonusDamage(4),GetSpellBonusDamage(5),GetSpellBonusDamage(6),GetSpellBonusDamage(7),GetSpellBonusHealing())
		local spellcrit = math.max(GetSpellCritChance(2),GetSpellCritChance(3),GetSpellCritChance(4),GetSpellCritChance(5),GetSpellCritChance(6),GetSpellCritChance(7))
		if E.Mists or E.TBC or E.Wrath then
			spellcrit = GetCritChance()
		end

		DT.tooltip:ClearLines()
		DT.tooltip:AddDoubleLine(basestatlabel..":", ElvUI[1].media.hexvaluecolor..currentstat.."|r ".."(|cffFFFFFF"..(basestat-statbuff+statnerf).."|r + |cff09ff00"..statbuff.."|r - |cfff44336"..statnerf.."|r)",1,1,1)

		--add line here
		DT.tooltip:AddLine(' ')
		if E.myclass == "HUNTER" then
			if E.Classic or E.TBC or E.Wrath then
				DT.tooltip:AddDoubleLine(RANGED_ATTACK_POWER..":", ElvUI[1].media.hexvaluecolor..totalranged.."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_RANGED_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetHitModifier()).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_RANGED)
			else
				DT.tooltip:AddDoubleLine(STAT_MASTERY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_RANGED_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCombatRatingBonus(7)).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_RANGED)
			end
			DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetRangedHaste()).."|r",1,1,1)
			DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetRangedCritChance()).."|r",1,1,1)
			if E.Classic or E.TBC or E.Wrath then
				DT.tooltip:AddLine(' ')
				DT.tooltip:AddDoubleLine(ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetArmorPenetration()).."|r",1,1,1)
			end
		elseif E.myclass == "WARLOCK" or E.myclass == "MAGE" or E.myclass == "PRIEST" then
			if E.Classic or E.TBC or E.Wrath then
				DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_POWER_SHORT..":", ElvUI[1].media.hexvaluecolor..spellpower.."|r", 1, 1, 1)
			else
				DT.tooltip:AddDoubleLine(STAT_MASTERY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r", 1, 1, 1)
			end
			DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_SPELL_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", (GetSpellHitModifier())).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_SPELL)
			DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetHaste()).."|r",1,1,1) --GetCombatRatingBonus(CR_HASTE_SPELL)
			DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", spellcrit).."|r",1,1,1)
			DT.tooltip:AddLine(' ')
			if E.Classic or E.TBC or E.Wrath then
				DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_PENETRATION_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetSpellPenetration()).."|r",1,1,1)
			end
			DT.tooltip:AddDoubleLine(ITEM_MOD_SPIRIT_SHORT..":", ElvUI[1].media.hexvaluecolor..UnitStat("player", 5).."|r",1,1,1)
		elseif E.myclass == "SHAMAN" or E.myclass == "DRUID" or E.myclass == "PALADIN" then
			if E.Classic or E.TBC or E.Wrath then
				local _, _, _, _, spent1 = _G.GetTalentTabInfo(1)
				local _, _, _, _, spent2 = _G.GetTalentTabInfo(2)
				local _, _, _, _, spent3 = _G.GetTalentTabInfo(3)
				if E.myclass == "SHAMAN" or E.myclass == "DRUID" then
					if spent2 > spent3 and spent2 > spent1 then
						DT.tooltip:AddDoubleLine(MELEE_ATTACK_POWER..":", ElvUI[1].media.hexvaluecolor..totalmelee.."|r", 1, 1, 1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_MELEE_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCombatRatingBonus(6)).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_MELEE)
						DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMeleeHaste()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCritChance()).."|r",1,1,1)
						DT.tooltip:AddLine(' ')
						DT.tooltip:AddDoubleLine(ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetArmorPenetration()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(STAT_EXPERTISE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetExpertise()*0.25).."|r", 1, 1, 1)
					else
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_POWER_SHORT..":", ElvUI[1].media.hexvaluecolor..spellpower.."|r", 1, 1, 1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_SPELL_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", (GetSpellHitModifier())).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_SPELL)
						DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetHaste()).."|r",1,1,1) --GetCombatRatingBonus(CR_HASTE_SPELL)
						DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", spellcrit).."|r",1,1,1)
						DT.tooltip:AddLine(' ')
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_PENETRATION_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetSpellPenetration()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPIRIT_SHORT..":", ElvUI[1].media.hexvaluecolor..UnitStat("player", 5).."|r",1,1,1)
					end
				elseif E.myclass == "PALADIN" then
					if spent1 > spent3 and spent1 > spent2 then
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_POWER_SHORT..":", ElvUI[1].media.hexvaluecolor..spellpower.."|r", 1, 1, 1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_SPELL_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", (GetSpellHitModifier())).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_SPELL)
						DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetHaste()).."|r",1,1,1) --GetCombatRatingBonus(CR_HASTE_SPELL)
						DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", spellcrit).."|r",1,1,1)
						DT.tooltip:AddLine(' ')
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPELL_PENETRATION_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetSpellPenetration()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_SPIRIT_SHORT..":", ElvUI[1].media.hexvaluecolor..UnitStat("player", 5).."|r",1,1,1)
					else
						DT.tooltip:AddDoubleLine(MELEE_ATTACK_POWER..":", ElvUI[1].media.hexvaluecolor..totalmelee.."|r", 1, 1, 1)
						DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_MELEE_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCombatRatingBonus(6)).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_MELEE)
						DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMeleeHaste()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCritChance()).."|r",1,1,1)
						DT.tooltip:AddLine(' ')
						DT.tooltip:AddDoubleLine(ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetArmorPenetration()).."|r",1,1,1)
						DT.tooltip:AddDoubleLine(STAT_EXPERTISE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetExpertise()*0.25).."|r", 1, 1, 1)
					end
				end
			else
				DT.tooltip:AddDoubleLine(STAT_MASTERY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(HIT..": ", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", (math.max(GetSpellHitModifier(),GetCombatRatingBonus(8),GetCombatRatingBonus(6)))).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_SPELL)
				DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", math.max(GetCombatRatingBonus(_G.CR_HASTE_SPELL),GetMeleeHaste())).."|r",1,1,1) --GetCombatRatingBonus(CR_HASTE_SPELL)
				DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", spellcrit).."|r",1,1,1)
				DT.tooltip:AddLine(' ')
				DT.tooltip:AddDoubleLine(STAT_EXPERTISE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetExpertise()).."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(ITEM_MOD_SPIRIT_SHORT..":", ElvUI[1].media.hexvaluecolor..UnitStat("player", 5).."|r",1,1,1)
			end
		else
			if E.Classic or E.TBC or E.Wrath then
				DT.tooltip:AddDoubleLine(MELEE_ATTACK_POWER..":", ElvUI[1].media.hexvaluecolor..totalmelee.."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(ITEM_MOD_HIT_MELEE_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCombatRatingBonus(6)).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_MELEE)
				DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMeleeHaste()).."|r",1,1,1)
				DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetCritChance()).."|r",1,1,1)
				DT.tooltip:AddLine(' ')
				DT.tooltip:AddDoubleLine(ITEM_MOD_ARMOR_PENETRATION_RATING_SHORT..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetArmorPenetration()).."|r",1,1,1)
				DT.tooltip:AddDoubleLine(STAT_EXPERTISE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetExpertise()*0.25).."|r", 1, 1, 1)
			else
				DT.tooltip:AddDoubleLine(STAT_MASTERY..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(HIT..": ", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", (math.max(GetSpellHitModifier(),GetCombatRatingBonus(8),GetCombatRatingBonus(6)))).."|r",1,1,1) --GetCombatRatingBonus(CR_HIT_SPELL)
				DT.tooltip:AddDoubleLine(STAT_HASTE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", math.max(GetCombatRatingBonus(_G.CR_HASTE_SPELL),GetMeleeHaste())).."|r",1,1,1) --GetCombatRatingBonus(CR_HASTE_SPELL)
				DT.tooltip:AddDoubleLine(STAT_CRITICAL_STRIKE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", spellcrit).."|r",1,1,1)
				DT.tooltip:AddLine(' ')
				DT.tooltip:AddDoubleLine(STAT_EXPERTISE..":", ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetExpertise()).."|r", 1, 1, 1)
				DT.tooltip:AddDoubleLine(ITEM_MOD_SPIRIT_SHORT..":", ElvUI[1].media.hexvaluecolor..UnitStat("player", 5).."|r",1,1,1)
			end
		end
		if not E.Classic then
			if E.Mists or E.TBC or E.Wrath then
				DT.tooltip:AddDoubleLine(STAT_RESILIENCE..":", ElvUI[1].media.hexvaluecolor.._G.GetModResilienceDamageReduction().."%|r", 1, 1, 1)
			else
				DT.tooltip:AddDoubleLine(STAT_RESILIENCE..":", ElvUI[1].media.hexvaluecolor..GetCombatRating(_G.COMBAT_RATING_RESILIENCE_CRIT_TAKEN).."|r", 1, 1, 1)
			end
		end
		DT.tooltip:Show()
	end
end

--haste and crit datatext
local function EltruismStatsDatatext1(dt)
	if E.Retail then
		local retailhaste = GetHaste()
		local retailcrit = math.max(GetCritChance(),GetSpellCritChance())
		local haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", retailhaste).."|r"
		local crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", retailcrit).."|r"

		dt.text:SetFormattedText('%s %s|r',crit,haste)
	else
		local haste
		local crit
		local spellcrit = math.max(GetSpellCritChance(2),GetSpellCritChance(3),GetSpellCritChance(4),GetSpellCritChance(5),GetSpellCritChance(6),GetSpellCritChance(7))
		if E.Mists or E.TBC or E.Wrath then
			spellcrit = GetCritChance()
		end

		if E.myclass == "HUNTER" then
			haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r"
			crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetRangedCritChance()).."|r"
		elseif E.myclass == "WARLOCK" or E.myclass == "MAGE" or E.myclass == "PRIEST" then
			haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r" --GetCombatRatingBonus(CR_HASTE_SPELL)
			crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", spellcrit).."|r"
		elseif E.myclass == "SHAMAN" or E.myclass == "DRUID" or E.myclass == "PALADIN" then
			if E.Classic or E.TBC or E.Wrath then
				local _, _, _, _, spent1 = _G.GetTalentTabInfo(1)
				local _, _, _, _, spent2 = _G.GetTalentTabInfo(2)
				local _, _, _, _, spent3 = _G.GetTalentTabInfo(3)
				if E.myclass == "SHAMAN" or E.myclass == "DRUID" then
					if spent2 > spent3 and spent2 > spent1 then
						haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r"
						crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetCritChance()).."|r"
					else
						haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r" --GetCombatRatingBonus(CR_HASTE_SPELL)
						crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", spellcrit).."|r"
					end
				elseif E.myclass == "PALADIN" then
					if spent1 > spent3 and spent1 > spent2 then
						haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r" --GetCombatRatingBonus(CR_HASTE_SPELL)
						crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", spellcrit).."|r"
					else
						haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r"
						crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetCritChance()).."|r"
					end
				end
			else
				haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r" --GetCombatRatingBonus(CR_HASTE_SPELL)
				crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", spellcrit).."|r"
			end
		else
			haste = STAT_HASTE..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHaste()).."|r"
			crit = CRIT_ABBR..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", math.max(GetCritChance(),spellcrit)).."|r"
		end
		dt.text:SetFormattedText('%s %s|r',crit,haste)
	end
end
DT:RegisterDatatext('Eltruism Stats 1', STAT_CATEGORY_ENHANCEMENTS, {'COMBAT_RATING_UPDATE',"UNIT_STATS","UNIT_SPELL_HASTE","UNIT_DAMAGE","UNIT_ATTACK_SPEED","SPELL_POWER_CHANGED"}, EltruismStatsDatatext1, nil, nil, EltruismStatsDatatextOnEnter, nil, L["Eltruism Stats 1"])

--mastery/vers or power/hit
local function EltruismStatsDatatext2(dt)
	if E.Retail then
		--mastery
		local mastery = STAT_MASTERY..": "..ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r"
		--versatility
		local versdmg = GetCombatRatingBonus(29) + GetVersatilityBonus(29)
		local versdef = GetCombatRatingBonus(31) + GetVersatilityBonus(31)
		local versatilitylabel = E:ShortenString(STAT_VERSATILITY, 4)
		local versatility = versatilitylabel..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", math.max(versdef,versdmg)).."|r"
		dt.text:SetFormattedText('%s %s|r',mastery,versatility)
	else
		--power
		local rangedbasepower, rangedbuff, rangednerf = UnitRangedAttackPower('player')
		local totalranged = rangedbasepower+rangedbuff+rangednerf

		local meleebasepower, meleebuff, meleenerf = UnitAttackPower('player')
		local totalmelee = meleebasepower+meleebuff+meleenerf

		local spellpower = math.max(GetSpellBonusDamage(2),GetSpellBonusDamage(3),GetSpellBonusDamage(4),GetSpellBonusDamage(5),GetSpellBonusDamage(6),GetSpellBonusDamage(7),GetSpellBonusHealing())

		local tmeleepower = ATTACK_POWER..": "..ElvUI[1].media.hexvaluecolor..totalmelee.."|r"
		local trangedpower = ATTACK_POWER..": "..ElvUI[1].media.hexvaluecolor..totalranged.."|r"
		local tspellpower = ATTACK_POWER..": "..ElvUI[1].media.hexvaluecolor..spellpower.."|r"

		--hit rating
		local tspellhit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetCombatRatingBonus(8)).."|r" --GetCombatRatingBonus(CR_HIT_SPELL)
		local tmeleehit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetCombatRatingBonus(6)).."|r" --GetCombatRatingBonus(CR_HIT_MELEE)
		local trangedhit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetCombatRatingBonus(7)).."|r" --GetCombatRatingBonus(CR_HIT_RANGED)
		if E.Classic or E.TBC or E.Wrath then
			tspellhit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", (GetSpellHitModifier())).."|r" --GetCombatRatingBonus(CR_HIT_SPELL)
			tmeleehit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", GetHitModifier()).."|r" --GetCombatRatingBonus(CR_HIT_MELEE)
			trangedhit = tmeleehit
		elseif E.Mists or E.TBC or E.Wrath then
			tspellhit = HIT..": "..ElvUI[1].media.hexvaluecolor..string.format("%.1f%%", (math.max(GetSpellHitModifier(),GetCombatRatingBonus(8),GetCombatRatingBonus(6)))).."|r" --GetCombatRatingBonus(CR_HIT_SPELL)
		end

		--mastery
		local mastery
		if E.Mists or E.TBC or E.Wrath then
			mastery = STAT_MASTERY..": "..ElvUI[1].media.hexvaluecolor..string.format("%.2f%%", GetMasteryEffect()).."|r"
		end

		if E.myclass == "HUNTER" then
			if E.Classic or E.TBC or E.Wrath then
				dt.text:SetFormattedText('%s %s|r',trangedpower,trangedhit)
			else
				dt.text:SetFormattedText('%s %s|r',mastery,trangedhit)
			end
		elseif E.myclass == "WARLOCK" or E.myclass == "MAGE" or E.myclass == "PRIEST" then
			if E.Classic or E.TBC or E.Wrath then
				dt.text:SetFormattedText('%s %s|r',tspellpower,tspellhit)
			else
				dt.text:SetFormattedText('%s %s|r',mastery,tspellhit)
			end
		elseif E.myclass == "SHAMAN" or E.myclass == "DRUID" or E.myclass == "PALADIN" then
			if E.Classic or E.TBC or E.Wrath then
				local _, _, _, _, spent1 = _G.GetTalentTabInfo(1)
				local _, _, _, _, spent2 = _G.GetTalentTabInfo(2)
				local _, _, _, _, spent3 = _G.GetTalentTabInfo(3)
				if E.myclass == "SHAMAN" or E.myclass == "DRUID" then
					if spent2 > spent3 and spent2 > spent1 then
						dt.text:SetFormattedText('%s %s|r',tmeleepower,tmeleehit)
					else
						dt.text:SetFormattedText('%s %s|r',tspellpower,tspellhit)
					end
				elseif E.myclass == "PALADIN" then
					if spent1 > spent3 and spent1 > spent2 then
						dt.text:SetFormattedText('%s %s|r',tspellpower,tspellhit)
					else
						dt.text:SetFormattedText('%s %s|r',tmeleepower,tmeleehit)
					end
				end
			else
				dt.text:SetFormattedText('%s %s|r',mastery, tspellhit)
			end
		else
			if E.Classic or E.TBC or E.Wrath then
				dt.text:SetFormattedText('%s %s|r',tmeleepower,tmeleehit)
			else
				dt.text:SetFormattedText('%s %s|r',mastery,tmeleehit)
			end
		end
	end
end
DT:RegisterDatatext('Eltruism Stats 2', STAT_CATEGORY_ENHANCEMENTS, {'COMBAT_RATING_UPDATE',"UNIT_STATS","UNIT_RANGEDDAMAGE","UNIT_ATTACK_POWER","UNIT_RANGED_ATTACK_POWER","UNIT_ATTACK","MASTERY_UPDATE","UNIT_DAMAGE","SPELL_POWER_CHANGED","PLAYER_DAMAGE_DONE_MODS"}, EltruismStatsDatatext2, nil, nil, EltruismStatsDatatextOnEnter, nil, L["Eltruism Stats 2"])

--cata avoidance and defense,based on https://wago.io/qYKsfPe_W
local UnitDefense = _G.UnitDefense
local DEFENSE = _G.DEFENSE
local function EltruismStatsDatatext3(dt)
	if E.Retail then
		--dodge
		local dodgeChance = GetDodgeChance()
		local dodge = E:ShortenString(_G.DODGE, 5)..": "..ElvUI[1].media.hexvaluecolor..tostring(math.floor(dodgeChance*100)/100).."%".."|r"
		--armor
		local _, effectiveArmor = UnitArmor("player")
		local armor = E:ShortenString(_G.ARMOR, 5)..": "..ElvUI[1].media.hexvaluecolor..tostring(effectiveArmor).."|r"

		dt.text:SetFormattedText('%s %s|r',armor,dodge)
	else
		--defense
		local defense = E:ShortenString(DEFENSE, 3)..": "..ElvUI[1].media.hexvaluecolor..(select(1, UnitDefense("player"))) + (select(2, UnitDefense("player"))).."|r"
		--avoidance
		local miss = 5 + (((select(1, UnitDefense("player"))) + (select(2, UnitDefense("player"))) - (UnitLevel("player") * 5)) * 0.04)
		local avoid = miss+GetBlockChance()+GetParryChance()+GetDodgeChance()
		local avoidance = E:ShortenString(STAT_AVOIDANCE, 5)..": "..ElvUI[1].media.hexvaluecolor..tostring(math.floor(avoid*100)/100).."%".."|r"

		dt.text:SetFormattedText('%s %s|r',avoidance,defense)
	end
end
DT:RegisterDatatext('Eltruism Stats 3', STAT_CATEGORY_ENHANCEMENTS, {'COMBAT_RATING_UPDATE',"UNIT_STATS","PLAYER_DAMAGE_DONE_MODS"}, EltruismStatsDatatext3, nil, nil, EltruismStatsDatatextOnEnter, nil, L["Eltruism Stats 3"])
