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

-- ─── RUI.Draw — low-level frame decoration helpers ────────────────────────────
-- All widgets and containers call these internally. Prefer the Widget/Container
-- API; use Draw only when building custom frames not covered by the library.

-- Module-level tracking of the currently open custom dropdown menu.
-- Set by MakeMenuFrame's OnHide and by CloseOpenMenu.
-- RUI._openMenu is nil when no menu is open.

-- Fills a frame with a solid color texture on BACKGROUND layer.
-- Returns the texture.
function RUI.Draw.Background(frame, c)
    local t = frame:CreateTexture(nil, "BACKGROUND")
    t:SetAllPoints()
    t:SetColorTexture(c.r, c.g, c.b, c.a)
    return t
end

-- Draws a 1px (or `size`-px) border as four ColorTexture edges on BORDER layer.
-- Returns top, bottom, left, right textures.
function RUI.Draw.Border(frame, c, size)
    size = size or RUI.BORDER_SIZE
    local function edge()
        local t = frame:CreateTexture(nil, "BORDER")
        t:SetColorTexture(c.r, c.g, c.b, c.a)
        return t
    end
    local top, bottom, left, right = edge(), edge(), edge(), edge()

    top:SetPoint("TOPLEFT",  frame, "TOPLEFT",  0, 0)
    top:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, 0)
    top:SetHeight(size)

    bottom:SetPoint("BOTTOMLEFT",  frame, "BOTTOMLEFT",  0, 0)
    bottom:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    bottom:SetHeight(size)

    left:SetPoint("TOPLEFT",    frame, "TOPLEFT",    0, 0)
    left:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)
    left:SetWidth(size)

    right:SetPoint("TOPRIGHT",    frame, "TOPRIGHT",    0, 0)
    right:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0)
    right:SetWidth(size)

    return top, bottom, left, right
end

-- Applies a font spec table {face, size, flags} to a FontString with ARIALN fallback.
function RUI.Draw.SetFont(fs, spec)
    if not fs:SetFont(spec.face, spec.size, spec.flags) then
        fs:SetFont("Fonts\\ARIALN.TTF", spec.size, spec.flags or "")
    end
end

-- Creates a control-label FontString that inherits GameFontHighlightSmall.
-- FRIZQT 10pt + built-in 1px drop shadow (x=1,y=-1). Shadow is what makes
-- Blizzard/DF labels look thicker and wider than a bare SetFont call produces.
function RUI.Draw.MakeLabel(parent)
    return parent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
end

-- Creates a section/group heading FontString that inherits GameFontNormal.
-- FRIZQT 12pt + built-in 1px drop shadow. This is what DandersFrames uses
-- for its collapsible section titles ("Frame Modes", "Language", etc.).
function RUI.Draw.MakeHeader(parent)
    return parent:CreateFontString(nil, "OVERLAY", "GameFontNormal")
end

-- Closes the currently open custom dropdown menu (if any).
function RUI.Draw.CloseOpenMenu()
    if RUI._openMenu and RUI._openMenu:IsShown() then
        RUI._openMenu:Hide()
    end
    RUI._openMenu = nil
end

-- Creates a styled selector Button matching DandersFrames' dropdown visual:
-- BackdropTemplate button, dark ELEMENT background, 1px BORDER border,
-- left-anchored .Text FontString, small down-arrow on the right.
-- Hover changes the background to HOVER color.
-- Call SetPoint separately to position.
function RUI.Draw.MakeDropdownButton(parent, ddName, width)
    local btn = CreateFrame("Button", ddName, parent, "BackdropTemplate")
    btn:SetSize(width, RUI.DD_H)
    btn:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    btn:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
    btn:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 1)

    local fs = btn:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    fs:SetPoint("LEFT", 8, 0)
    fs:SetPoint("RIGHT", -20, 0)
    fs:SetJustifyH("LEFT")
    fs:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
    btn.Text = fs

    local arrow = btn:CreateTexture(nil, "OVERLAY")
    arrow:SetSize(12, 12)
    arrow:SetPoint("RIGHT", -8, 0)
    arrow:SetTexture("Interface\\AddOns\\" .. RUI._addonName .. "\\libs\\RUI\\Media\\Icons\\expand_more")
    arrow:SetVertexColor(RUI.C.DIM.r, RUI.C.DIM.g, RUI.C.DIM.b)

    btn:SetScript("OnEnter", function(self)
        self:SetBackdropColor(RUI.C.HOVER.r, RUI.C.HOVER.g, RUI.C.HOVER.b, 1)
    end)
    btn:SetScript("OnLeave", function(self)
        self:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
    end)

    return btn
end

-- Creates a themed EditBox matching MakeDropdownButton's chrome: ELEMENT fill,
-- 1px BORDER edge, GameFontHighlightSmall, 5px text insets. Caller owns the
-- position and the commit/revert scripts.
--
-- Deliberately NOT InputBoxTemplate. That template dresses the field in
-- Blizzard's tan `common-search-border-*` atlas, which reads as a foreign
-- control inside a dark RUI dialog, and it anchors its Left texture at x=-5 --
-- so the field's visible edge lands 5px left of every other control in the
-- column. DandersFrames makes the same call (GUI:StyleEditBox builds its own
-- well rather than inheriting the Blizzard template).
function RUI.Draw.MakeInputBox(parent, width)
    local eb = CreateFrame("EditBox", nil, parent, "BackdropTemplate")
    eb:SetSize(width, RUI.INPUT_H)
    eb:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    eb:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
    eb:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 1)
    eb:SetFontObject("GameFontHighlightSmall")
    eb:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
    eb:SetTextInsets(5, 5, 0, 0)
    eb:SetAutoFocus(false)

    -- Behaviours InputBoxScriptTemplate used to supply and a bare EditBox does
    -- not. Callers override OnEscapePressed/OnEditFocusLost; when they do, they
    -- own clearing the highlight (see RUI.Widget.TextInput).
    eb:SetScript("OnEscapePressed", eb.ClearFocus)
    eb:SetScript("OnEditFocusGained", function(self) self:HighlightText() end)
    eb:SetScript("OnEditFocusLost", function(self) self:HighlightText(0, 0) end)

    return eb
end

-- Styles a ScrollFrameTemplate scrollbar to match DF's minimal pill look:
-- hides track decorations and nav arrows, overlays a grey pill thumb, slims to 10px.
function RUI.Draw.StyleMenuScrollBar(scrollFrame)
    local sb = scrollFrame.ScrollBar
    if not sb then return end
    if sb.Background then sb.Background:Hide() end
    if sb.Track then
        if sb.Track.Begin  then sb.Track.Begin:Hide()  end
        if sb.Track.End    then sb.Track.End:Hide()    end
        if sb.Track.Middle then sb.Track.Middle:Hide() end
    end
    if sb.Thumb then
        if sb.Thumb.Begin  then sb.Thumb.Begin:Hide()  end
        if sb.Thumb.End    then sb.Thumb.End:Hide()    end
        if sb.Thumb.Middle then sb.Thumb.Middle:Hide() end
        if not sb.Thumb.ruiCustomBg then
            local pill = sb.Thumb:CreateTexture(nil, "ARTWORK")
            pill:SetAllPoints()
            pill:SetColorTexture(0.4, 0.4, 0.4, 0.8)
            sb.Thumb.ruiCustomBg = pill
        end
    end
    if sb.Back    then sb.Back:Hide();    sb.Back:SetSize(1, 1)    end
    if sb.Forward then sb.Forward:Hide(); sb.Forward:SetSize(1, 1) end
    sb:SetWidth(10)
end

-- Creates a dropdown menu popup frame with an internal ScrollFrame.
-- The menu is parented to UIParent at FULLSCREEN_DIALOG strata so it renders
-- above the options panel and is never clipped by a parent scroll frame.
-- Returns menuFrame, scrollFrame, scrollChild.
-- Position the menuFrame via ClearAllPoints + SetPoint at open time.
function RUI.Draw.MakeMenuFrame()
    local mf = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
    mf:SetFrameStrata("FULLSCREEN_DIALOG")
    mf:SetClampedToScreen(true)
    mf:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    mf:SetBackdropColor(RUI.C.PANEL.r, RUI.C.PANEL.g, RUI.C.PANEL.b, 0.98)
    mf:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 1)
    mf:Hide()

    -- ScrollFrame takes up full interior minus room for scrollbar on the right.
    -- ScrollFrameTemplate positions its ScrollBar 17px to the right of the scroll
    -- frame's right edge, so -20 on BOTTOMRIGHT puts the bar inside the menu border.
    local sf = CreateFrame("ScrollFrame", nil, mf, "ScrollFrameTemplate")
    sf:SetPoint("TOPLEFT",     mf, "TOPLEFT",     2, -2)
    sf:SetPoint("BOTTOMRIGHT", mf, "BOTTOMRIGHT", -20, 2)

    local sc = CreateFrame("Frame", nil, sf)
    sc:SetWidth(1)  -- will be sized in BuildRows
    sf:SetScrollChild(sc)

    RUI.Draw.StyleMenuScrollBar(sf)

    mf:SetScript("OnHide", function()
        if RUI._openMenu == mf then RUI._openMenu = nil end
    end)

    return mf, sf, sc
end

-- Creates a horizontal 1px rule texture on BORDER layer at the given y offset.
function RUI.Draw.HRule(frame, y, xLeft, xRight, c)
    c = c or RUI.C.BORDER
    local t = frame:CreateTexture(nil, "BORDER")
    t:SetColorTexture(c.r, c.g, c.b, c.a)
    t:SetHeight(1)
    t:SetPoint("TOPLEFT",  frame, "TOPLEFT",  xLeft,  y)
    t:SetPoint("TOPRIGHT", frame, "TOPRIGHT", xRight, y)
    return t
end
