local PT = AhUI
if not PT or not PT.InfoPanel then return end

local IP = PT.InfoPanel
local Tooltip = PT.Tooltip
local L = PT.L

IP:RegisterElementInfo("itemlevel", {
    name  = "ItemLevel",
    order = 20,
})

local function GetItemLevels()
    local total, equipped = GetAverageItemLevel()
    return total or 0, equipped or 0
end

local function FormatILvl(v)
    return string.format("%.1f", v)
end

local function ShowTooltip(self)
    if not Tooltip then return end

    local total, equipped = GetItemLevels()

    Tooltip:Clear()

    Tooltip:AddLine(L["ITEMLEVEL_TITLE"], 14, 1, 1, 1)
    Tooltip:AddLine(" ")

    Tooltip:AddLine(
        string.format(L["ITEMLEVEL_AVERAGE"], FormatILvl(total)),
        12, 0.9, 0.9, 0.9
    )

    if math.abs(total - equipped) > 0.05 then
        Tooltip:AddLine(
            string.format(L["ITEMLEVEL_EQUIPPED"], FormatILvl(equipped)),
            12, 0.7, 0.7, 0.7
        )
    end

Tooltip:AddLine(" ")
Tooltip:AddLine(
    L["ITEMLEVEL_CLICK"],
    11, 0.7, 0.7, 0.7
)
    Tooltip:ShowAt(self, "BOTTOMLEFT", "TOPLEFT", 0, 6)
end

local function Create(uid)
    local parent = (IP.frame and IP.frame.content) or UIParent
    local height = (IP.config and IP.config.appearance.height) or 24

    local f = CreateFrame("Button", nil, parent)
    f:SetHeight(height)

local s = math.max(16, height - 2)

local icon = f:CreateTexture(nil, "ARTWORK")
icon:SetTexture("Interface\\AddOns\\AhUI\\media\\icons\\ahui_ilvl.tga")
icon:SetSize(s, s)
icon:SetPoint("LEFT", 2, 0)
icon:SetVertexColor(1, 1, 1)

    local text = f:CreateFontString(nil, "OVERLAY")
    text:SetPoint("LEFT", icon, "RIGHT", 1, 0)
    text:SetFontObject(PT.fonts.normal)

    f.icon = icon
    f.text = text

    f:SetScript("OnEnter", ShowTooltip)
    f:SetScript("OnLeave", function()
		if Tooltip then Tooltip:Clear() end
	end)

    f:RegisterForClicks("LeftButtonUp")
    f:SetScript("OnClick", function(_, btn)
        if btn == "LeftButton" then
            ToggleCharacter("PaperDollFrame")
        end
    end)

    local element = {
        _type = "ItemLevel",
        id    = uid,
        frame = f,
        icon  = icon,
        text  = text,
        width = 80,
    }
if IP.EnableDrag then
    IP:EnableDrag(element)
end
function element:Update()
    local total = GetItemLevels()

    self.text:SetText(FormatILvl(total))
    self.text:SetTextColor(1, 1, 1)

    local textW = self.text:GetUnboundedStringWidth() or 0
	local w = self.icon:GetWidth() + textW + 6
    self.width = w
    self.frame:SetWidth(w)
end

element:Update()
return element

end

IP:RegisterElementType("itemlevel", function(uid)
    return Create(uid)
end)

local ev = CreateFrame("Frame")
ev:RegisterEvent("PLAYER_LOGIN")
ev:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
ev:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")

ev:SetScript("OnEvent", function()
    for _, element in pairs(IP.elements or {}) do
        if element and element._type == "ItemLevel" and element.Update then
            element:Update()
        end
    end
end)
