local PT = AhUI
if not PT or not PT.InfoPanel then return end
local IP = PT.InfoPanel
local Tooltip = PT.Tooltip
local Dropdown = PT.Dropdown
local L = PT.L

local function GetCharacterKey()
    local name, realm = UnitName("player")
    return name .. "-" .. (realm or "")
end

local function GetCurrencyDB(uid)
    AhUI_CharConfig = AhUI_CharConfig or {}
    AhUI_CharConfig.infopanel_currency =
        AhUI_CharConfig.infopanel_currency or {}

    local charKey = GetCharacterKey()

    AhUI_CharConfig.infopanel_currency[charKey] =
        AhUI_CharConfig.infopanel_currency[charKey] or {}

    AhUI_CharConfig.infopanel_currency[charKey][uid] =
        AhUI_CharConfig.infopanel_currency[charKey][uid] or {}

    return AhUI_CharConfig.infopanel_currency[charKey][uid]
end

local function GetSelectedCurrencyID(db)
    if not db then return nil end

    if db.currencyId and not db.currencyID then
        db.currencyID = db.currencyId
        db.currencyId = nil
    end
    return db.currencyID
end

local function GetCurrencyInfo(id)
    if not id then return end
    return C_CurrencyInfo.GetCurrencyInfo(id)
end

local function FormatAmount(v)
    return BreakUpLargeNumbers(v or 0)
end

local function GetTrackedCurrencies()
    local list = {}

    local size = C_CurrencyInfo.GetCurrencyListSize()
    if not size then return list end

    for i = 1, size do
        local info = C_CurrencyInfo.GetCurrencyListInfo(i)
        if info and not info.isHeader and info.isTracked then
            table.insert(list, info.currencyID)
        end
    end

    return list
end

local function GetAllCurrencies()
    local result = {}

    local size = C_CurrencyInfo.GetCurrencyListSize()
    if not size then return result end

    for i = 1, size do
        local info = C_CurrencyInfo.GetCurrencyListInfo(i)
        if info and not info.isHeader and info.currencyID then
            table.insert(result, info.currencyID)
        end
    end

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

    local element = self.__element
    local id = element and element.data and GetSelectedCurrencyID(element.data)
    local info = id and GetCurrencyInfo(id)

    Tooltip:Clear()

    if not info then
        Tooltip:AddLine(L["CurrencyNotSelected"], 13, 1, 0.3, 0.3)
        Tooltip:AddLine(" ")
        Tooltip:AddLine(L["CurrencySelect"], 11, 0.7, 0.7, 0.7)
        Tooltip:ShowAt(self, "BOTTOMLEFT", "TOPLEFT", 0, 6)
        return
    end

    Tooltip:AddLine(info.name, 14, 1, 1, 1)
    Tooltip:AddLine(" ")

    Tooltip:AddLine(
        string.format(L["CurrencyAmount"], FormatAmount(info.quantity)),
        12, 0.9, 0.9, 0.9
    )

    if info.maxQuantity and info.maxQuantity > 0 then
        Tooltip:AddLine(
            string.format(L["CurrencyMax"], FormatAmount(info.maxQuantity)),
            11, 0.7, 0.7, 0.7
        )
    end

    Tooltip:AddLine(" ")

    Tooltip:AddLine(L["CurrencySelect"],   11, 0.7, 0.7, 0.7)
    Tooltip:AddLine(L["CurrencyRemove"],   11, 0.7, 0.7, 0.7)
    Tooltip:AddLine(L["CurrencyAddMore"],  11, 0.7, 0.7, 0.7)

    Tooltip:ShowSmart(self)
end


local function ShowCurrencyDropdown(element)
    local items = {}

    for _, id in ipairs(GetAllCurrencies()) do
        local info = GetCurrencyInfo(id)
        if info then
table.insert(items, {
    text    = info.name,
    icon    = info.iconFileID,
    amount  = FormatAmount(info.quantity),
    checked = (element.data and GetSelectedCurrencyID(element.data) == id),
onClick = function()
    if element.data then
    element.data.currencyID = id
    element.data.currencyId = nil
end
	element.db.currencyId = nil
    element:Update()

    if PT and PT.Settings and PT.Settings.InfoPanel and PT.Settings.InfoPanel.RebuildLists then
        PT.Settings.InfoPanel:RebuildLists()
    end
end,

})

        end
    end

    Dropdown:Show({
        owner = element.frame,
        items = items,
    })
end

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

    local f = CreateFrame("Button", nil, parent)
    f:SetHeight(height)
    f:EnableMouse(true)
    f:RegisterForClicks("AnyUp")

    local icon = f:CreateTexture(nil, "ARTWORK")
    icon:SetSize(height - 4, height - 4)
    icon:SetPoint("LEFT", 0, 0)

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

    f.icon = icon
    f.text = text

local element = {
    _type = "Currency",
    id    = uid,
    frame = f,
    icon  = icon,
    text  = text,
    width = 0,
}

element.data = GetCurrencyDB(uid)

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

f:SetScript("OnClick", function(self, btn)
    local element = self.__element

    if btn == "LeftButton" and IsShiftKeyDown() then
        IP:RemoveElementFromPanel(element.id)
        return
    end

    if btn == "RightButton" or IsControlKeyDown() then
        IP:AddElementToPanel("currency")
        return
    end

    ShowCurrencyDropdown(element)
end)

    if IP.EnableDrag then
        IP:EnableDrag(element)
    end

    function element:Update()
        local id = self.data and GetSelectedCurrencyID(self.data)

if not id then
    self.text:SetText("ClickMe")
    self.icon:SetTexture(nil)
    self.frame:SetWidth(self.text:GetStringWidth() + 6)
    return
end
        local info = GetCurrencyInfo(id)
        if not info then return end

        self.icon:SetTexture(info.iconFileID)
        self.text:SetText(FormatAmount(info.quantity))

        local w = self.icon:GetWidth() + 6 + self.text:GetStringWidth()
        self.width = w
        self.frame:SetWidth(w)
    end
    element:Update()

    function element:ApplyHeight(h)
        local s = h - 4
        self.icon:SetSize(s, s)
        self.text:SetFont(PT.FONT, math.max(10, h - 8), "")
    end

    return element
end

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

local function EnsureCurrencyAvailable()
    IP:RegisterElementInfo("currency", {
        name  = L["Currency"],
        order = 70,
		multi = true,
    })
end

local ev = CreateFrame("Frame")
ev:RegisterEvent("PLAYER_LOGIN")
ev:RegisterEvent("CURRENCY_DISPLAY_UPDATE")

ev:SetScript("OnEvent", function()
    EnsureCurrencyAvailable()

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