local RUI = LibStub("RUI-1.0")
if not RUI then return end

-- RUI.Widget.Dropdown(parent, label, x, y, width, getEntries, get, set [,onSet] [,ddName])
-- → Button, Refresh()
-- getEntries() must return {{text=displayName, value=storedValue}, ...}
-- Total height consumed: LABEL_H(14) + DD_H(24) + CONTROL_GAP(4) = 42px.
-- Menu: custom BackdropTemplate rows, accent text for selected, accent highlight on hover.
-- No scroll — if entries can be large use a scrollable widget instead.
local MENU_ROW_H   = 22
local MENU_MAX_VIS = 12  -- max visible rows before scroll kicks in

function RUI.Widget.Dropdown(parent, label, x, y, width, getEntries, get, set, onSet, ddName)
    local lbl = RUI.Draw.MakeLabel(parent)
    lbl:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
    lbl:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y)
    lbl:SetWidth(width)
    lbl:SetJustifyH("LEFT")
    lbl:SetText(label)

    local btn = RUI.Draw.MakeDropdownButton(parent, ddName, width)
    btn:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y - RUI.LABEL_H)

    local menuFrame, scrollFrame, scrollChild = RUI.Draw.MakeMenuFrame()

    -- Row width = menu interior minus scrollbar column (20px)
    local rowW = width - 22

    local rows = {}

    local function UpdateButtonText()
        for _, e in ipairs(getEntries()) do
            if e.value == get() then
                btn.Text:SetText(e.text)
                return
            end
        end
        btn.Text:SetText("")
    end

    local function BuildRows()
        for _, r in ipairs(rows) do r:Hide(); r:SetParent(nil) end
        wipe(rows)

        local entries = getEntries()
        local curVal  = get()
        local n       = #entries
        local visRows = math.min(n, MENU_MAX_VIS)

        menuFrame:SetHeight(visRows * MENU_ROW_H + 4)
        scrollChild:SetSize(rowW, n * MENU_ROW_H)

        if scrollFrame.ScrollBar then
            if n > MENU_MAX_VIS then scrollFrame.ScrollBar:Show()
            else scrollFrame.ScrollBar:Hide() end
        end

        for i, e in ipairs(entries) do
            local row = CreateFrame("Button", nil, scrollChild)
            row:SetPoint("TOPLEFT", 0, -(i - 1) * MENU_ROW_H)
            row:SetSize(rowW, MENU_ROW_H)

            local fs = row:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
            fs:SetPoint("LEFT", 8, 0)
            fs:SetPoint("RIGHT", 0, 0)
            fs:SetJustifyH("LEFT")
            row.Text = fs

            local hl = row:CreateTexture(nil, "HIGHLIGHT")
            hl:SetAllPoints()
            hl:SetColorTexture(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 0.3)

            if e.value == curVal then
                fs:SetTextColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b)
            else
                fs:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
            end
            fs:SetText(e.text)

            local val = e.value
            local txt = e.text
            row:SetScript("OnClick", function()
                set(val)
                if onSet then onSet(val) end
                btn.Text:SetText(txt)
                menuFrame:Hide()
            end)

            rows[i] = row
        end
    end

    local function OpenMenu()
        RUI.Draw.CloseOpenMenu()
        BuildRows()
        menuFrame:ClearAllPoints()
        menuFrame:SetPoint("TOPLEFT",  btn, "BOTTOMLEFT",  0, -2)
        menuFrame:SetPoint("TOPRIGHT", btn, "BOTTOMRIGHT", 0, -2)
        menuFrame:Show()
        RUI._openMenu = menuFrame
    end

    btn:SetScript("OnClick", function()
        if menuFrame:IsShown() then menuFrame:Hide()
        else OpenMenu() end
    end)

    local function Refresh()
        UpdateButtonText()
        if menuFrame:IsShown() then BuildRows() end
    end
    Refresh()
    return btn, Refresh, lbl
end
