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

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

local function GetCurrencyInfo(id)
    return id and C_CurrencyInfo.GetCurrencyInfo(id)
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 Picker

local function CreatePicker()
    if Picker then return Picker end

    local f = CreateFrame("Frame", "AhUI_CurrencyPicker", UIParent, "BackdropTemplate")
    f:SetSize(320, 220)
    f:SetFrameStrata("DIALOG")
    f:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 2,
    })
    f:SetBackdropColor(0, 0, 0, 0.95)
    f:SetBackdropBorderColor(1, 1, 1, 1)
    f:Hide()

    f:SetMovable(true)
    f:EnableMouse(true)
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", f.StartMoving)
    f:SetScript("OnDragStop", f.StopMovingOrSizing)

    local title = f:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
    title:SetPoint("TOP", 0, -8)
    title:SetText(L["CURRENCY_PICKER_TITLE"])

    local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
    close:SetPoint("TOPRIGHT", -4, -4)

    local scroll = CreateFrame("ScrollFrame", nil, f)
    scroll:SetPoint("TOPLEFT", 10, -30)
    scroll:SetPoint("BOTTOMRIGHT", -10, 10)
    scroll:EnableMouseWheel(true)

    local content = CreateFrame("Frame", nil, scroll)
    content:SetSize(1, 1)
    scroll:SetScrollChild(content)

    scroll:SetScript("OnMouseWheel", function(self, delta)
        local cur = self:GetVerticalScroll()
        self:SetVerticalScroll(math.max(0, cur - delta * 24))
    end)

    f.scroll = scroll
    f.content = content
    f.rows = {}

    Picker = f
    return f
end

function IP:OpenCurrencyPicker(element)
    if not element or not element.db then return end

    local f = CreatePicker()
    f.element = element

    f:ClearAllPoints()

    local ef = element.frame
    local pickerH = f:GetHeight() or 420

    local _, _, _, _, elemY = ef:GetPoint()
    elemY = elemY or 0

    if elemY < pickerH + 40 then
        f:SetPoint("BOTTOM", ef, "TOP", 0, 6)
    else
        f:SetPoint("TOP", ef, "BOTTOM", 0, -6)
    end

    f:Show()

    for _, r in ipairs(f.rows) do
        r:Hide()
    end
    wipe(f.rows)

    local y = -2
    local rowH = 24

    for _, id in ipairs(GetAllCurrencies()) do
        local info = GetCurrencyInfo(id)
        if info then
            local r = CreateFrame("Button", nil, f.content)
            r:SetSize(280, rowH)
            r:SetPoint("TOPLEFT", 0, y)

            local bg = r:CreateTexture(nil, "BACKGROUND")
            bg:SetAllPoints()
            bg:SetTexture("Interface\\Buttons\\WHITE8x8")
            bg:SetColorTexture(1, 1, 1, 0)

            r:SetScript("OnEnter", function()
                bg:SetColorTexture(1, 1, 1, 0.15)
            end)
            r:SetScript("OnLeave", function()
                bg:SetColorTexture(1, 1, 1, 0)
            end)

            local icon = r:CreateTexture(nil, "ARTWORK")
            icon:SetSize(16, 16)
            icon:SetPoint("LEFT", 4, 0)
            icon:SetTexture(info.iconFileID)

            local name = r:CreateFontString(nil, "OVERLAY", "GameFontNormal")
            name:SetPoint("LEFT", icon, "RIGHT", 6, 0)
            name:SetText(info.name)

            local amount = r:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
            amount:SetPoint("RIGHT", -6, 0)
            amount:SetText(BreakUpLargeNumbers(info.quantity or 0))

            r:SetScript("OnClick", function()
                element.db.currencyId = id
                if element.Update then
                    element:Update()
                end
                f:Hide()
            end)

            table.insert(f.rows, r)
            y = y - rowH
        end
    end

    f.content:SetHeight(-y + 4)
end
