local _, ns = ...

--等级文本
function ns.CteatLevelText(unitFrame)
	if not PlateColorDB.levelText then return end
	if not unitFrame then return end
	if not unitFrame.LevelText then
		unitFrame.LevelText = unitFrame.healthBar:CreateFontString(nil, "ARTWORK")
		unitFrame.LevelText:SetFont(ns.fonts, 16, "OUTLINE")
	end
	if not unitFrame.unit then return end
	local NpLevel = UnitLevel(unitFrame.unit)
	local PlayerLevel = UnitLevel("player")
	local LevelText = (NpLevel > 0 and (NpLevel < PlayerLevel or NpLevel > PlayerLevel + 2)) and NpLevel or ""
	local difficulty = C_PlayerInfo.GetContentDifficultyCreatureForPlayer(unitFrame.unit)
	local color = GetDifficultyColor(difficulty);
	local anchor
	if unitFrame.RaidTargetFrame:IsShown() then
		anchor = unitFrame.RaidTargetFrame
	elseif unitFrame.ClassificationFrame:IsShown() then
		anchor = unitFrame.ClassificationFrame
	else
		anchor = unitFrame.healthBar
	end
	unitFrame.LevelText:SetPoint("RIGHT",anchor,"LEFT",- 3,0)
	unitFrame.LevelText:SetVertexColor(color.r, color.g, color.b);
	unitFrame.LevelText:SetText(LevelText)
end

hooksecurefunc("CompactUnitFrame_UpdateName", function(unitFrame)
	if unitFrame:IsForbidden() then return end
	if not string.match(unitFrame.unit,"nameplate") then return end
	ns.CteatLevelText(unitFrame)
end)