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:SetFontObject("PC_FontOutline")
		unitFrame.LevelText:SetFontHeight(16)
		unitFrame.LevelText:SetSmoothScaling(false)
	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);

	unitFrame.LevelText:ClearAllPoints()
    -- 寻找最左侧的参考物
    local anchor = unitFrame.healthBar
	if unitFrame.ClassificationFrame and unitFrame.ClassificationFrame:IsShown() then
        anchor = unitFrame.ClassificationFrame
    end
	unitFrame.LevelText:SetPoint("RIGHT",anchor,"LEFT",- 3,0)
	unitFrame.LevelText:SetVertexColor(color.r, color.g, color.b);
	unitFrame.LevelText:SetText(LevelText)
end

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

ns.event("NAME_PLATE_UNIT_ADDED", function(event, unit)
	local namePlate = C_NamePlate.GetNamePlateForUnit(unit,false)
	local unitFrame = namePlate.UnitFrame
	ns.CteatLevelText(unitFrame)
end)