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

local FONT = AhUI.FONT
local CHECKED_ATLAS = "common-icon-checkmark"

function PT:CreateDropdownLikeButton(parent, width, height)
    width = width or 220

    local dd = CreateFrame("Frame", nil, parent, "UIDropDownMenuTemplate")
    UIDropDownMenu_SetWidth(dd, width)

    local overlay = CreateFrame("Button", nil, dd)
    overlay:SetAllPoints(dd)
    overlay:RegisterForClicks("LeftButtonUp")
    overlay:SetFrameLevel((dd:GetFrameLevel() or 0) + 5)
    dd.overlay = overlay

    function dd:SetText(t)
        UIDropDownMenu_SetText(self, t or "")
    end

    function dd:GetText()
        if self.Text and self.Text.GetText then return self.Text:GetText() or "" end
        return ""
    end

    function dd:SetOnClick(fn)
        overlay:SetScript("OnClick", fn)
    end

    return dd
end


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.694, 0.612, 0.851, 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
    local maxActionsWidth = 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

            if type(item.actionButtons) == "table" then
                maxActionsWidth = math.max(maxActionsWidth, #item.actionButtons * 18)
            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)

        if item.checked then
            if icon.SetAtlas then
                icon:SetAtlas(item.checkedAtlas or CHECKED_ATLAS, false)
            else
                icon:SetTexture("Interface\\Buttons\\UI-CheckBox-Check")
            end

            icon:SetSize(16, 16)
            icon:SetVertexColor(0.35, 1, 0.35, 1)
            icon:Show()

        elseif item.atlas and icon.SetAtlas then
            icon:SetAtlas(item.atlas, false)
            icon:SetSize(16, 16)
            icon:SetVertexColor(1, 1, 1, 1)
            icon:Show()

        elseif item.icon then
            icon:SetSize(16, 16)
            icon:SetTexture(item.icon)
            icon:SetVertexColor(1, 1, 1, 1)
            icon:Show()

        else
            icon:SetTexture(nil)
            icon:Hide()
        end

        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
        if maxActionsWidth > 0 then
            w = w + maxActionsWidth + 8
        end

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

        if row.amount and maxActionsWidth > 0 then
            row.amount:ClearAllPoints()
            row.amount:SetPoint("RIGHT", row, "RIGHT", -(maxActionsWidth + 10), 0)
        end

        if type(item.actionButtons) == "table" then
            local right = -2

            for i = #item.actionButtons, 1, -1 do
                local spec = item.actionButtons[i]
                local btn = CreateFrame("Button", nil, row)
                btn:SetSize(16, 16)
                btn:SetPoint("RIGHT", row, "RIGHT", right, 0)
                btn:EnableMouse(true)

                local visual

                if spec.atlas then
                    visual = btn:CreateTexture(nil, "OVERLAY")
                    visual:SetSize(12, 12)
                    visual:SetPoint("CENTER")
                    visual:SetAtlas(spec.atlas)
                    if spec.rotation then visual:SetRotation(spec.rotation) end
                elseif spec.icon then
                    visual = btn:CreateTexture(nil, "OVERLAY")
                    visual:SetSize(12, 12)
                    visual:SetPoint("CENTER")
                    visual:SetTexture(spec.icon)
                else
                    visual = btn:CreateFontString(nil, "OVERLAY")
                    visual:SetFont(FONT, 12, "OUTLINE")
                    visual:SetPoint("CENTER")
                    visual:SetText(spec.text or "")
                end

                if spec.disabled then
                    if visual.SetTextColor then
                        visual:SetTextColor(0.35, 0.35, 0.35)
                    else
                        visual:SetAlpha(0.25)
                    end
                else
                    if visual.SetTextColor then
                        visual:SetTextColor(1, 1, 1)
                    else
                        visual:SetAlpha(1)
                    end

                    btn:SetScript("OnClick", function()
                        if spec.onClick then
                            spec.onClick()
                        end

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

                    if visual.SetVertexColor then
                        btn:SetScript("OnEnter", function() visual:SetVertexColor(1.3, 1.3, 1.3) end)
                        btn:SetScript("OnLeave", function()
                            visual:SetVertexColor(1, 1, 1)
                            visual:ClearAllPoints()
                            visual:SetPoint("CENTER")
                        end)
                        btn:HookScript("OnMouseDown", function()
                            visual:ClearAllPoints()
                            visual:SetPoint("CENTER", 1, -1)
                            visual:SetVertexColor(0.7, 0.7, 0.7)
                        end)
                        btn:HookScript("OnMouseUp", function(self)
                            visual:ClearAllPoints()
                            visual:SetPoint("CENTER")
                            local v = self:IsMouseOver() and 1.3 or 1
                            visual:SetVertexColor(v, v, v)
                        end)
                    end
                end

                right = right - 18
            end
        end

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

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

    if opts.clampToScreen == false then
        f:SetClampedToScreen(false)
    else
        f:SetClampedToScreen(true)
    end

    f:ClearAllPoints()
    if opts.owner then
        local p = opts.anchorPoint or "TOPLEFT"
        local rp = opts.anchorRelativePoint or "BOTTOMLEFT"
        local x = opts.anchorX or 0
        local y = opts.anchorY or -4
        f:SetPoint(p, opts.owner, rp, x, y)
    else
        f:SetPoint("CENTER")
    end
end