local PT = AhUI
if not PT then return end
local catcher
PT.Dropdown = {}
local D = PT.Dropdown

local FONT = "Interface\\AddOns\\AhUI\\fonts\\GothamXNarrow-Medium.ttf"

local function CreateFrameOnce()
    if D.frame then return end

    local f = CreateFrame("Frame", "AhUIDropdown", UIParent, "BackdropTemplate")
    f:SetFrameStrata("TOOLTIP")
	f:SetFrameLevel(1001)
    f:SetClampedToScreen(true)
    f:Hide()

    f:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 2,
    })
    f:SetBackdropColor(0, 0, 0, 0.95)
    f:SetBackdropBorderColor(0.2, 0.2, 0.2, 1)

    f.items = {}
    D.frame = f

    if not catcher then
        catcher = CreateFrame("Frame", nil, UIParent)
        catcher:SetAllPoints(UIParent)
        catcher:SetFrameStrata("TOOLTIP")
        catcher:SetFrameLevel(1000)
        catcher:EnableMouse(true)
        catcher:Hide()

        catcher:SetScript("OnMouseDown", function()
            D:Hide()
        end)
    end
end

function D:Hide()
    if not self.frame then return end
self.frame:Hide()
if catcher then
    catcher:Hide()
end

end

function D:Show(opts)
    CreateFrameOnce()

    local f = self.frame
	self.keepOpen = opts and opts.keepOpen
f:Show()
if catcher then
    catcher:Show()
end

    for _, row in ipairs(f.items) do
        row:Hide()
    end
    wipe(f.items)

    local maxTextWidth   = 0
    local maxAmountWidth = 0

    if opts.items then
        for _, item in ipairs(opts.items) do
            if item.text then
                local fs = f:CreateFontString(nil, "OVERLAY")
                fs:SetFont(FONT, 12, "")
                fs:SetText(item.text)
                maxTextWidth = math.max(maxTextWidth, fs:GetStringWidth())
                fs:Hide()
            end

            if item.amount then
                local fs = f:CreateFontString(nil, "OVERLAY")
                fs:SetFont(FONT, 12, "")
                fs:SetText(item.amount)
                maxAmountWidth = math.max(maxAmountWidth, fs:GetStringWidth())
                fs:Hide()
            end
        end
    end

    local width = 0
    local y = -8

    for _, item in ipairs(opts.items or {}) do
        local row = CreateFrame("Button", nil, f)
        row:SetHeight(20)
        row:SetPoint("TOPLEFT", 8, y)
        row:EnableMouse(true)

        row.bg = row:CreateTexture(nil, "BACKGROUND")
        row.bg:SetAllPoints()
        row.bg:SetColorTexture(1, 1, 1, 0)

        row.hover = row:CreateTexture(nil, "HIGHLIGHT")
        row.hover:SetAllPoints()
        row.hover:SetColorTexture(1, 1, 1, 0.08)

        local icon = row:CreateTexture(nil, "ARTWORK")
        icon:SetSize(16, 16)
        icon:SetPoint("LEFT", 0, 0)
        icon:SetTexture(item.icon)
        row.icon = icon
		
        local text = row:CreateFontString(nil, "OVERLAY")
        text:SetFont(FONT, 12, "")
        text:SetPoint("LEFT", icon, "RIGHT", 6, 0)
        text:SetWidth(maxTextWidth)
        text:SetJustifyH("LEFT")
        text:SetText(item.text or "")
        row.text = text

        if item.amount then
            local amount = row:CreateFontString(nil, "OVERLAY")
            amount:SetFont(FONT, 12, "")
            amount:SetPoint("RIGHT", row, "RIGHT", -4, 0)
            amount:SetWidth(maxAmountWidth)
            amount:SetJustifyH("RIGHT")
            amount:SetText(item.amount)
            amount:SetTextColor(0.9, 0.9, 0.9)
            row.amount = amount
        end

        if item.header then
            row:EnableMouse(false)
            row.text:SetFont(FONT, 11, "")
            row.text:SetTextColor(1, 0.82, 0)
        else
            if item.checked then
                row.text:SetTextColor(1, 0.82, 0)
            end

			row:SetScript("OnClick", function()
				if item.onClick then item.onClick() end

				if not D.keepOpen then
					D:Hide()
				end
			end)
        end

        local w = 22 + maxTextWidth
        if row.amount then
            w = w + maxAmountWidth + 12
        end

        width = math.max(width, w)
        row:SetWidth(w)

        table.insert(f.items, row)
        y = y - 22
    end

    f:SetSize(width + 16, -y + 8)

    f:ClearAllPoints()
    if opts.owner then
        f:SetPoint("TOPLEFT", opts.owner, "BOTTOMLEFT", 0, -4)
    else
        f:SetPoint("CENTER")
    end
end
