local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local addon, ns = ...

--Lua functions
local next, type = next, type
local gsub, format = gsub, format

--WoW API / Variables
local UnitFactionGroup = UnitFactionGroup
local GetNumGroupMembers = GetNumGroupMembers
local UnitBattlePetLevel = UnitBattlePetLevel
local UnitClass = UnitClass
local UnitClassification = UnitClassification
local UnitHealth = UnitHealth
local UnitHealthMax = UnitHealthMax
local UnitIsAFK = UnitIsAFK
local UnitIsBattlePetCompanion = UnitIsBattlePetCompanion
local UnitIsConnected = UnitIsConnected
local UnitIsDead = UnitIsDead
local UnitIsGhost = UnitIsGhost
local UnitIsPlayer = UnitIsPlayer
local UnitIsPVP = UnitIsPVP
local UnitIsWildBattlePet = UnitIsWildBattlePet
local UnitReaction = UnitReaction

local mTextureSkullGray = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\skull4.tga")
local mTextureSkullBlue = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\skull3.tga")
local mTextureSkullWith = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\skull1.tga")
local mTextureSkullRed = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\skull2.tga")
local mTextureGhost = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\skull5.tga")
local mTextureAFK = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\afk.tga")
local mTextureDND = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\dnd.tga")
local mTextureOffline = format("|T%s:16:16:0:0:32:32|t", "Interface\\AddOns\\ElvUI_mMediaTag\\media\\misc\\offline.tga")

ElvUF.Tags.Events["mClass"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format("%sRare|r", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format("%sRare Elite|r", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format("%sElite|r", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format("%sBoss|r", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mClass:space-left"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass:space-left"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format(" %sRare|r", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format(" %sRare Elite|r", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format(" %sElite|r", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format(" %sBoss|r", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mClass:space-right"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass:space-right"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format("%sRare|r ", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format("%sRare Elite|r ", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format("%sElite|r ", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format("%sBoss|r ", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mClass:short"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass:short"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format("%sR|r", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format("%sR+|r", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format("%s+|r", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format("%sB|r", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mClass:short-space-left"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass:short-space-left"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format(" %sR|r", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format(" %sR+|r", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format(" %s+|r", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format(" %sB|r", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mClass:short-space-right"] = "UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mClass:short-space-right"] = function(unit)
	local c = UnitClassification(unit)
	
	if(c == "rare") then
		return format("%sR|r ", E.db[mPlugin].cClassRare.color)
	elseif(c == "rareelite") then
		return format("%sR+|r ", E.db[mPlugin].cClassRareElite.color)
	elseif(c == "elite") then
		return format("%s+|r ", E.db[mPlugin].cClassElite.color)
	elseif(c == "worldboss") then
		return format("%sB|r ", E.db[mPlugin].cClassBoss.color)
	end
end

ElvUF.Tags.Events["mColor"] = "UNIT_NAME_UPDATE UNIT_FACTION INSTANCE_ENCOUNTER_ENGAGE_UNIT UNIT_CLASSIFICATION_CHANGED"
ElvUF.Tags.Methods["mColor"] = function(unit)
	local c = UnitClassification(unit)
	local unitReaction = UnitReaction(unit, 'player')
	local unitPlayer = UnitIsPlayer(unit)
	
	if (unitPlayer) then
		local _, unitClass = UnitClass(unit)
		local class = ElvUF.colors.class[unitClass]
		if not class then return "" end
		return Hex(class[1], class[2], class[3])
	elseif (unitReaction) then
		if (unitReaction > 3) then
			local reaction = ElvUF.colors.reaction[unitReaction]
			return Hex(reaction[1], reaction[2], reaction[3])
		else
			if(c == "rare") then
				return E.db[mPlugin].cClassRare.color
			elseif(c == "rareelite") then
				return E.db[mPlugin].cClassRareElite.color
			elseif(c == "elite") then
				return E.db[mPlugin].cClassElite.color
			elseif(c == "worldboss") then
				return E.db[mPlugin].cClassBoss.color
			end
		end
	end
end

ElvUF.Tags.Events["mColor:target"] = "UNIT_TARGET"
ElvUF.Tags.Methods["mColor:target"] = function(unit)
	local target = unit.."target"
	local c = UnitClassification(target)
	local unitReaction = UnitReaction(target, 'player')
	local unitPlayer = UnitIsPlayer(target)
	
	if (unitPlayer) then
		local _, unitClass = UnitClass(target)
		local class = ElvUF.colors.class[unitClass]
		if not class then return "" end
		return Hex(class[1], class[2], class[3])
	elseif (unitReaction) then
		if (unitReaction > 3) then
			local reaction = ElvUF.colors.reaction[unitReaction]
			return Hex(reaction[1], reaction[2], reaction[3])
		else
			if(c == "rare") then
				return E.db[mPlugin].cClassRare.color
			elseif(c == "rareelite") then
				return E.db[mPlugin].cClassRareElite.color
			elseif(c == "elite") then
				return E.db[mPlugin].cClassElite.color
			elseif(c == "worldboss") then
				return E.db[mPlugin].cClassBoss.color
			end
		end
	end
end


ElvUF.Tags.Events["mHealth"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return _TAGS.mAFK(unit)
	else
		if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
			return _TAGS.mStatus(unit)
		else
			local currentHealth = UnitHealth(unit)
			local deficit = UnitHealthMax(unit) - currentHealth
			
			if (deficit > 0 and currentHealth > 0) then
				return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
			else
				return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
			end
		end
	end
end

ElvUF.Tags.Events["mHealth:nodeath"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:nodeath"] = function(unit)
	if(not UnitIsDead(unit)) or (not UnitIsGhost(unit))then
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mHealth:icon"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:icon"] = function(unit)
	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit)) or (UnitIsAFK(unit)) or (UnitIsDND(unit)) then
		return _TAGS["mStatus:icon"](unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mHealth:short"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return _TAGS.mAFK(unit)
	else
		if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
			return _TAGS.mStatus(unit)
		else
			local currentHealth = UnitHealth(unit)
			local deficit = UnitHealthMax(unit) - currentHealth
			
			if (deficit > 0 and currentHealth > 0) then
				return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
			else
				return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
			end
		end
	end
end

ElvUF.Tags.Events["mHealth:short:nodeath"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short:nodeath"] = function(unit)
	if (not UnitIsDead(unit)) or (not UnitIsGhost(unit)) then
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
		end
	end
end

ElvUF.Tags.Events["mHealth:short:icon"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short:icon"] = function(unit)
	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit)) or (UnitIsAFK(unit)) or (UnitIsDND(unit)) then
		return _TAGS["mStatus:icon"](unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
		end
	end
end

ElvUF.Tags.Events["mStatus"] = "UNIT_HEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED"
ElvUF.Tags.Methods["mStatus"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return _TAGS.mAFK(unit)
	elseif UnitIsDND(unit) then
		return format("[%sDND|r]", E.db[mPlugin].cGeneralAFK.color)
	else
		if(not UnitIsConnected(unit)) then
			return L["Offline"]
		elseif(UnitIsDead(unit)) then
			return L["Dead"]
		elseif(UnitIsGhost(unit)) then
			return L["Ghost"]
		end
	end
end

ElvUF.Tags.Events["mStatus:icon"] = "UNIT_HEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED"
ElvUF.Tags.Methods["mStatus:icon"] = function(unit)
	if UnitIsAFK(unit) then
		return mTextureAFK
	elseif UnitIsDND(unit) then
		return mTextureDND
	else
		if(not UnitIsConnected(unit)) then
			return mTextureOffline
		elseif(UnitIsDead(unit)) then
			return mTextureSkullRed
		elseif(UnitIsGhost(unit)) then
			return mTextureGhost
		end
	end
end

ElvUF.Tags.Events["mDeath:gray"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeath:gray"] = function(unit)
	if (UnitIsDead(unit)) then
		return mTextureSkullGray
	elseif (UnitIsGhost(unit)) then
		return mTextureGhost
	end
end

ElvUF.Tags.Events["mDeath:white"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeath:white"] = function(unit)
	if (UnitIsDead(unit)) then
		return mTextureSkullWith
	elseif (UnitIsGhost(unit)) then
		return mTextureGhost
	end
end

ElvUF.Tags.Events["mDeath:red"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeath:red"] = function(unit)
	if (UnitIsDead(unit)) then
		return mTextureSkullRed
	elseif (UnitIsGhost(unit)) then
		return mTextureGhost
	end
end

ElvUF.Tags.Events["mDeath:blue"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeath:blue"] = function(unit)
	if (UnitIsDead(unit)) then
		return mTextureSkullBlue
	elseif (UnitIsGhost(unit)) then
		return mTextureGhost
	end
end

local unitStatus = {}
ElvUF.Tags.OnUpdateThrottle["mStatustimer"] = 1
ElvUF.Tags.Methods["mStatustimer"] = function(unit)
	if not UnitIsPlayer(unit) then return end

	local guid = UnitGUID(unit)
	local status = unitStatus[guid]

	if UnitIsAFK(unit) then
		if not status or status[1] ~= 'AFK' then
			unitStatus[guid] = {'AFK', GetTime()}
		end
	elseif UnitIsDND(unit) then
		if not status or status[1] ~= 'DND' then
			unitStatus[guid] = {'DND', GetTime()}
		end
	elseif UnitIsDead(unit) or UnitIsGhost(unit) then
		if not status or status[1] ~= 'Dead' then
			unitStatus[guid] = {'Dead', GetTime()}
		end
	elseif not UnitIsConnected(unit) then
		if not status or status[1] ~= 'Offline' then
			unitStatus[guid] = {'Offline', GetTime()}
		end
	else
		unitStatus[guid] = nil
	end

	if status ~= unitStatus[guid] then
		status = unitStatus[guid]
	end

	if status then
		local timer = GetTime() - status[2]
		local mins = floor(timer / 60)
		local secs = floor(timer - (mins * 60))
		return format('%01.f:%02.f', mins, secs)
	end
end


local mDeathReset = false
local mID = 0
local FramemDeathReset = CreateFrame('Frame')
FramemDeathReset:RegisterEvent('UPDATE_INSTANCE_INFO')
FramemDeathReset:RegisterEvent('PLAYER_ENTERING_WORLD')
FramemDeathReset:SetScript('OnEvent', function(self, event)
	local inInstance, _ = IsInInstance()
	local iniId = tostring(({GetInstanceInfo()})[8])
	if mDeathReset == false and inInstance == true and iniId ~= mID then
		mID = iniId
		mDeathReset = true
	end
end)

local UnitmDeathCount = {}
ElvUF.Tags.Events["mDeathCount"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeathCount"] = function(unit)
	if not UnitIsPlayer(unit) then return end

	local guid = UnitGUID(unit)
	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		if (not UnitmDeathCount[guid]) then
			UnitmDeathCount[guid] = {true, 1}
		else
			if UnitmDeathCount[guid][1] ~= true then
				UnitmDeathCount[guid] = {true, UnitmDeathCount[guid][2] + 1}
			end
		end
	else
		if (UnitmDeathCount ~= nil) then
			if (UnitmDeathCount[guid] ~= nil) then
				UnitmDeathCount[guid][1] = false
			end
		end

		if mDeathReset == true then
			UnitmDeathCount = {}
			mDeathReset = false
		end
	end

	if UnitmDeathCount[guid] and (UnitmDeathCount[guid][2] >=1) then
		return UnitmDeathCount[guid][2]
	end
end

ElvUF.Tags.Events["mDeathCount:hide"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeathCount:hide"] = function(unit)
	if not UnitIsPlayer(unit) then return end
	_TAGS.mDeathCount(unit)

	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS.mDeathCount(unit)
	end
end

ElvUF.Tags.Events["mDeathCount:color"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeathCount:color"] = function(unit)
	if not UnitIsPlayer(unit) then return end

	local guid = UnitGUID(unit)

	if UnitmDeathCount[guid] then
		local r, g, b = E:ColorGradient(UnitmDeathCount[guid][2] * .20, 0, 1, 0, 1, 1, 0, 1, 0, 0)
		return Hex(r, g, b)
	end
end

ElvUF.Tags.Events["mDeathCount:text"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeathCount:text"] = function(unit)
	if not UnitIsPlayer(unit) then return end

	return L["Deaths"] .. ": "
end

ElvUF.Tags.Events["mDeathCount:hide:text"] = "UNIT_HEALTH"
ElvUF.Tags.Methods["mDeathCount:hide:text"] = function(unit)
	if not UnitIsPlayer(unit) then return end

	local guid = UnitGUID(unit)

	if UnitmDeathCount[guid] then
		if UnitmDeathCount[guid][2] >= 1 then
			return L["Deaths"] .. ": "
		end
	end
end

ElvUF.Tags.Events["mAFK"] = "PLAYER_FLAGS_CHANGED"
ElvUF.Tags.Methods["mAFK"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return format("[%sAFK|r]", E.db[mPlugin].cGeneralAFK.color)
	end
end

ElvUF.Tags.Events["mHealth:current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:current-percent"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return _TAGS.mAFK(unit)
	else
		if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
			return _TAGS.mStatus(unit)
		else
			local currentHealth = UnitHealth(unit)
			local deficit = UnitHealthMax(unit) - currentHealth
			
			if (deficit > 0 and currentHealth > 0) then
				return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit)), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
			else
				return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
			end
		end
	end
end

ElvUF.Tags.Events["mHealth:nodeath:current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:nodeath:current-percent"] = function(unit)
	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return ""
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit)), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mHealth:short-current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short-current-percent"] = function(unit)
	local isAFK = UnitIsAFK(unit)
	
	if isAFK then
		return _TAGS.mAFK(unit)
	else
		if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
			return _TAGS.mStatus(unit)
		else
			local currentHealth = UnitHealth(unit)
			local deficit = UnitHealthMax(unit) - currentHealth
			
			if (deficit > 0 and currentHealth > 0) then
				return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
			else
				return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
			end
		end
	end
end

ElvUF.Tags.Events["mHealth:short:nodeath-current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short:nodeath-current-percent"] = function(unit)
	if (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return ""
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
		end
	end
end

ElvUF.Tags.Events["mHealth:NoAFK"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:NoAFK"] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
		return _TAGS.mStatus(unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mHealth:short-NoAFK"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short-NoAFK"] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
		return _TAGS.mStatus(unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true)
		end
	end
end

ElvUF.Tags.Events["mHealth:NoAFK-current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:NoAFK-current-percent"] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
		return _TAGS.mStatus(unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit)), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mHealth:short-NoAFK-current-percent"] = "UNIT_HEALTH UNIT_MAXHEALTH UNIT_CONNECTION PLAYER_FLAGS_CHANGED PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mHealth:short-NoAFK-current-percent"] = function(unit)
	if(UnitIsDead(unit)) or (UnitIsGhost(unit)) or (not UnitIsConnected(unit))then
		return _TAGS.mStatus(unit)
	else
		local currentHealth = UnitHealth(unit)
		local deficit = UnitHealthMax(unit) - currentHealth
		
		if (deficit > 0 and currentHealth > 0) then
			return format("%s | %s", E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit), nil, true), E:GetFormattedText("PERCENT", UnitHealth(unit), UnitHealthMax(unit)))
		else
			return E:GetFormattedText("CURRENT", UnitHealth(unit), UnitHealthMax(unit))
		end
	end
end

ElvUF.Tags.Events["mRole"] = "PLAYER_ROLES_ASSIGNED GROUP_ROSTER_UPDATE"
ElvUF.Tags.Methods["mRole"] = function(unit)
	local Role = UnitGroupRolesAssigned(unit)
	
	if Role == "TANK" then
		return format("%s%s|r", E.db[mPlugin].cGeneralTank.color, TANK)
	elseif Role == "HEALER" then
		return format("%s%s|r", E.db[mPlugin].cGeneralTank.color, HEALER)
	end
end

ElvUF.Tags.Events["mLevel"] = "UNIT_LEVEL PLAYER_LEVEL_UP PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mLevel"] = function(unit)
	if(unit == "player" and IsResting()) then
		return format("%sZzz|r", E.db[mPlugin].cGeneralZzz.color)
	else
		local level = UnitLevel(unit)
		if ( UnitIsWildBattlePet(unit) or UnitIsBattlePetCompanion(unit) ) then
			return UnitBattlePetLevel(unit);
		elseif(level > 0) then
			return level
		else
			return format("%s??|r", E.db[mPlugin].cGeneralLevel.color)
		end
	end
end

ElvUF.Tags.Events["mLevelSmart"] = "UNIT_LEVEL PLAYER_LEVEL_UP PLAYER_UPDATE_RESTING"
ElvUF.Tags.Methods["mLevelSmart"] = function(unit)
	if(unit == "player" and IsResting()) then
		return format("%sZzz|r", E.db[mPlugin].cGeneralZzz.color)
	else
		local level = UnitLevel(unit)
		if ( UnitIsWildBattlePet(unit) or UnitIsBattlePetCompanion(unit) ) then
			return UnitBattlePetLevel(unit);
		elseif level == UnitLevel("player") then
			return ""
		elseif(level > 0) then
			return level
		else
			return format("%s??|r", E.db[mPlugin].cGeneralLevel.color)
		end
	end
end

ElvUF.Tags.Events["mGroup"] = "GROUP_ROSTER_UPDATE"
ElvUF.Tags.Methods["mGroup"] = function(unit)
	local name, server = UnitName(unit)
	if(server and server ~= '') then
		name = string.format('%s-%s', name, server)
	end
	
	for i=1, GetNumGroupMembers() do
		local raidName, _, group = GetRaidRosterInfo(i)
		if( raidName == name ) then
			return L["Group "] .. group
		end
	end
end

ElvUF.Tags.Events["mGroup:short"] = "GROUP_ROSTER_UPDATE"
ElvUF.Tags.Methods["mGroup:short"] = function(unit)
	local name, server = UnitName(unit)
	if(server and server ~= '') then
		name = string.format('%s-%s', name, server)
	end
	
	for i=1, GetNumGroupMembers() do
		local raidName, _, group = GetRaidRosterInfo(i)
		if( raidName == name ) then
			return L["Grp. "] .. group
		end
	end
end

ElvUF.Tags.Events["mPvP:left"] = "UNIT_FACTION"
ElvUF.Tags.Methods["mPvP:left"] = function(unit)
	local factionGroup = UnitFactionGroup(unit)
	if(UnitIsPVP(unit)) then
		if (factionGroup == 'Horde' or factionGroup == 'Alliance') then
			return CreateTextureMarkup("Interface\\FriendsFrame\\PlusManz-"..factionGroup, 16, 16, 16, 16, 0, 1, 0, 1, 0, 0) .. ' PvP'
		else
			return 'PvP'
		end
	end
end

ElvUF.Tags.Events["mPvP:right"] = "UNIT_FACTION"
ElvUF.Tags.Methods["mPvP:right"] = function(unit)
	local factionGroup = UnitFactionGroup(unit)
	if(UnitIsPVP(unit)) then
		if (factionGroup == 'Horde' or factionGroup == 'Alliance') then
			return 'PvP ' .. CreateTextureMarkup("Interface\\FriendsFrame\\PlusManz-"..factionGroup, 16, 16, 16, 16, 0, 1, 0, 1, 0, 0)
		else
			return 'PvP'
		end
	end
end

ElvUF.Tags.Events["mPvP:icon"] = "UNIT_FACTION"
ElvUF.Tags.Methods["mPvP:icon"] = function(unit)
	local factionGroup = UnitFactionGroup(unit)
	if(UnitIsPVP(unit)) and (factionGroup == 'Horde' or factionGroup == 'Alliance') then
		return CreateTextureMarkup("Interface\\FriendsFrame\\PlusManz-"..factionGroup, 16, 16, 16, 16, 0, 1, 0, 1, 0, 0)
	end
end

ElvUF.Tags.Events["mTarget"] = 'UNIT_TARGET'
ElvUF.Tags.Methods["mTarget"] = function(unit)
	local targetName = UnitName(unit..'target')
	if targetName then
		return E.TagFunctions.Abbrev(targetName)
	end
end

ElvUF.Tags.Events[format('mName:veryshort:statusicon', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:veryshort:statusicon', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus:icon"](unit)
	elseif name then
		return E:ShortenString(name, 5)
	end
end

ElvUF.Tags.Events[format('mName:short:statusicon', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:short:statusicon', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus:icon"](unit)
	elseif name then
		return E:ShortenString(name, 10)
	end
end

ElvUF.Tags.Events[format('mName:medium:statusicon', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:medium:statusicon', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus:icon"](unit)
	elseif name then
		return E:ShortenString(name, 15)
	end
end

ElvUF.Tags.Events[format('mName:long:statusicon', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:long:statusicon', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus:icon"](unit)
	elseif name then
		return E:ShortenString(name, 20)
	end
end

ElvUF.Tags.Events[format('mName:statusicon', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:statusicon', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus:icon"](unit)
	elseif name then
		return name
	end
end

ElvUF.Tags.Events[format('mName:veryshort:status', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:veryshort:status', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus"](unit)
	elseif name then
		return E:ShortenString(name, 5)
	end
end

ElvUF.Tags.Events[format('mName:short:status', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:short:status', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus"](unit)
	elseif name then
		return E:ShortenString(name, 10)
	end
end

ElvUF.Tags.Events[format('mName:medium:status', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:medium:status', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus"](unit)
	elseif name then
		return E:ShortenString(name, 15)
	end
end

ElvUF.Tags.Events[format('mName:long:status', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:long:status', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus"](unit)
	elseif name then
		return E:ShortenString(name, 20)
	end
end

ElvUF.Tags.Events[format('mName:status', textFormat)] = 'UNIT_NAME_UPDATE UNIT_CONNECTION PLAYER_FLAGS_CHANGED UNIT_HEALTH INSTANCE_ENCOUNTER_ENGAGE_UNIT'
ElvUF.Tags.Methods[format('mName:status', textFormat)] = function(unit)
	local name = UnitName(unit)
	if UnitIsAFK(unit) or UnitIsDND(unit) or (not UnitIsConnected(unit)) or (UnitIsDead(unit)) or (UnitIsGhost(unit)) then
		return _TAGS["mStatus"](unit)
	elseif name then
		return name
	end
end


E:AddTagInfo("mClass", ns.mName, L["Classes are fully written."], 1)
E:AddTagInfo("mClass:space-left", ns.mName, L["Classes are fully written with spac on left side."], 2)
E:AddTagInfo("mClass:space-right", ns.mName, L["Classes are fully written with spac on right side."], 3)
E:AddTagInfo("mClass:short", ns.mName, L["Classes are short written."], 4)
E:AddTagInfo("mClass:short-space-left", ns.mName, L["Classes are short written with spac on left side."], 5)
E:AddTagInfo("mClass:short-space-right", ns.mName, L["Classes are short written with spac on right side."], 6)

E:AddTagInfo("mColor", ns.mName, L["Colors Text by Class."], 7)
E:AddTagInfo("mColor:target", ns.mName, L["Colors Text by Target Class."], 8)

E:AddTagInfo("mHealth", ns.mName, L["Health changes between Max Health and Percent in fight."], 9)
E:AddTagInfo("mHealth:nodeath", ns.mName, L["Health changes between Max Health and Percent in fight and no Death Status."], 10)
E:AddTagInfo("mHealth:icon", ns.mName, L["Health changes between Max Health and Percent in fight and status icons."], 11)
E:AddTagInfo("mHealth:short", ns.mName, L["Health changes between Max Health and Percent in fight, with short numbers."], 12)
E:AddTagInfo("mHealth:short:nodeath", ns.mName, L["Health changes between Max Health and Percent in fight, with short numbers and no Death Status."], 13)
E:AddTagInfo("mHealth:short:icon", ns.mName, L["Health changes between Max Health and Percent in fight, with short numbers and status icons."], 14)

E:AddTagInfo("mHealth:NoAFK", ns.mName, L["Health changes between Max Health and Percent in fight, without AFK."], 15)
E:AddTagInfo("mHealth:short-NoAFK", ns.mName, L["Health changes between Max Health and Percent in fight, without AFK, with short numbers."], 16)
E:AddTagInfo("mHealth:current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight."], 17)
E:AddTagInfo("mHealth:nodeath:current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight and no Death Status."], 18)
E:AddTagInfo("mHealth:short-current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight, with short numbers."], 19)
E:AddTagInfo("mHealth:short:nodeath-current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight, with short numbers and no Death Status."], 20)
E:AddTagInfo("mHealth:NoAFK-current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight, without AFK."], 21)
E:AddTagInfo("mHealth:short-NoAFK-current-percent", ns.mName, L["Health changes between Max Health and Current - Percent in fight, without AFK, with short numbers."], 22)

E:AddTagInfo("mStatus", ns.mName, L["Coloured Statustext."], 23)
E:AddTagInfo("mStatus:icon", ns.mName, L["Status icons."], 24)
E:AddTagInfo("mStatustimer", ns.mName, L["Coloured Statustext, with timer for Dead and AFK."], 25)
E:AddTagInfo("mAFK", ns.mName, L["Coloured AFK text."], 26)

E:AddTagInfo("mRole", ns.mName, L["Tank and Heal Role written."], 27)

E:AddTagInfo("mLevel", ns.mName, L["Level changes to resting in the City."], 28)
E:AddTagInfo("mLevelSmart", ns.mName, L["Same as mLevel (hides Level if it is equal)."], 29)

E:AddTagInfo("mGroup", ns.mName, L["Group number with full text (Group 3)."], 30)
E:AddTagInfo("mGroup:short", ns.mName, L["Group number with abbreviated text (Grp. 3)."], 31)

E:AddTagInfo("mPvP:left", ns.mName, L["PvP text with symbol left."], 32)
E:AddTagInfo("mPvP:right", ns.mName, L["PvP text with symbol right."], 33)
E:AddTagInfo("mPvP:icon", ns.mName, L["Shows the faction icon when PvP is active."], 34)

E:AddTagInfo("mTarget", ns.mName, L["Shows the Abbrev Target Name."], 35)

E:AddTagInfo("mDeath:gray", ns.mName, L["Death icons."], 36)
E:AddTagInfo("mDeath:white", ns.mName, L["Death icons."], 37)
E:AddTagInfo("mDeath:red", ns.mName, L["Death icons."], 38)
E:AddTagInfo("mDeath:blue", ns.mName, L["Death icons."], 39)

E:AddTagInfo("mDeathCount", ns.mName, L["Death Counter, resets in new Instances."], 40)
E:AddTagInfo("mDeathCount:hide", ns.mName, L["Death Counter, will show if Unit is Dead"], 41)
E:AddTagInfo("mDeathCount:color", ns.mName, L["Death Counter texte color."], 42)
E:AddTagInfo("mDeathCount:text", ns.mName, L["Death Counter text."], 43)
E:AddTagInfo("mDeathCount:hide:text", ns.mName, L["Death Counter Text, will show if Death Counter >=1"], 44)

E:AddTagInfo("mName:status", ns.mName, L["Replace the name of the unit with Status if applicable"], 45)
E:AddTagInfo("mName:veryshort:status", ns.mName, L["Replace the name of the unit with Status if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 46)
E:AddTagInfo("mName:short:status", ns.mName, L["Replace the name of the unit with Status if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 47)
E:AddTagInfo("mName:medium:status", ns.mName, L["Replace the name of the unit with Status if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 48)
E:AddTagInfo("mName:long:status", ns.mName, L["Replace the name of the unit with Status if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 49)

E:AddTagInfo("mName:statusicon", ns.mName, L["Replace the name of the unit with Statusicon if applicable"], 45)
E:AddTagInfo("mName:veryshort:statusicon", ns.mName, L["Replace the name of the unit with Statusicon if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 50)
E:AddTagInfo("mName:short:statusicon", ns.mName, L["Replace the name of the unit with Statusicon if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 51)
E:AddTagInfo("mName:medium:statusicon", ns.mName, L["Replace the name of the unit with Statusicon if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 52)
E:AddTagInfo("mName:long:statusicon", ns.mName, L["Replace the name of the unit with Statusicon if applicable (limited to veryshort = 5, short = 10, medium = 15, long = 20 letters)"], 53)