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

local TITLE_H = 30  -- matches DF's titleBar:SetHeight(30)

-- RUI.Dialog(name, title, width, height) → Frame
-- Floating modal window parented to UIParent.
--   dialog.content  — Frame below the title bar; place widgets here.
--   dialog._title   — FontString if you need to update the title text at runtime.
-- Starts hidden. Caller calls dialog:Show() / dialog:Hide().
-- Drag by clicking the title bar. SetClampedToScreen is on by default.
function RUI.Dialog(name, title, width, height)
    local f = CreateFrame("Frame", name, UIParent)
    f:SetSize(width, height)
    f:SetPoint("CENTER")
    f:SetClampedToScreen(true)
    f:SetFrameStrata("HIGH")
    f:SetMovable(true)
    f:EnableMouse(true)
    f:Hide()

    RUI.Draw.Background(f, RUI.C.BG)
    RUI.Draw.Border(f, RUI.C.BORDER)

    -- Title bar stops 30px before right edge, leaving room for the close button.
    -- Matches DF: titleBar:SetPoint("TOPRIGHT", -30, 0)
    local titleBar = CreateFrame("Frame", nil, f)
    titleBar:SetPoint("TOPLEFT",  f, "TOPLEFT",  0,   0)
    titleBar:SetPoint("TOPRIGHT", f, "TOPRIGHT", -30, 0)
    titleBar:SetHeight(TITLE_H)
    titleBar:EnableMouse(true)
    titleBar:SetScript("OnMouseDown", function() f:StartMoving() end)
    titleBar:SetScript("OnMouseUp",   function() f:StopMovingOrSizing() end)
    f.titleBar = titleBar

    local titleBg = titleBar:CreateTexture(nil, "BACKGROUND")
    titleBg:SetAllPoints()
    titleBg:SetColorTexture(RUI.C.PANEL.r, RUI.C.PANEL.g, RUI.C.PANEL.b, RUI.C.PANEL.a)

    -- GameFontNormal template = exactly what DF's DFFontNormal copies from.
    -- DF sets ACCENT (GetThemeColor()) on the title FontString.
    local titleText = titleBar:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    titleText:SetTextColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b)
    titleText:SetPoint("LEFT", titleBar, "LEFT", 10, 0)
    titleText:SetText(title or "")
    f._title = titleText

    -- Close button: child of f (not titleBar), sits in the 30px strip on the right.
    -- Matches DF: closeBtn parented to frame, SetPoint("TOPRIGHT", -8, -5).
    local closeBtn = RUI.Widget.CloseButton(f, function() f:Hide() end)
    closeBtn:SetPoint("TOPRIGHT", f, "TOPRIGHT", -8, -5)

    -- Content area below the title bar
    local content = CreateFrame("Frame", nil, f)
    content:SetPoint("TOPLEFT",     f, "TOPLEFT",     0, -TITLE_H)
    content:SetPoint("BOTTOMRIGHT", f, "BOTTOMRIGHT", 0,  0)
    f.content = content

    -- f:AddHelpButton(onClick) — adds a "?" button to the left of the close button.
    -- Shifts the title bar right edge inward to make room.
    function f:AddHelpButton(onClick)
        titleBar:SetPoint("TOPRIGHT", f, "TOPRIGHT", -52, 0)

        local btn = CreateFrame("Button", nil, f, "BackdropTemplate")
        btn:SetSize(20, 20)
        btn:SetPoint("TOPRIGHT", f, "TOPRIGHT", -32, -5)
        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, 0.5)

        local fs = btn:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        fs:SetAllPoints()
        fs:SetJustifyH("CENTER")
        fs:SetJustifyV("MIDDLE")
        fs:SetText("?")
        fs:SetTextColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)

        btn:SetScript("OnEnter", function()
            btn:SetBackdropBorderColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 1)
            fs:SetTextColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b)
        end)
        btn:SetScript("OnLeave", function()
            btn:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 0.5)
            fs:SetTextColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)
        end)

        if onClick then btn:SetScript("OnClick", onClick) end
        return btn
    end

    return f
end
