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

-- RUI.Widget.Button(parent, label, width, height, onClick) → Frame
-- Dark-themed push button with hover/press states.
-- Caller must SetPoint. height defaults to RUI.BTN_H.
function RUI.Widget.Button(parent, label, width, height, onClick)
    local btn = CreateFrame("Button", nil, parent)
    btn:SetSize(width, height or RUI.BTN_H)

    local bg = btn:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, RUI.C.ELEMENT.a)
    btn._bg = bg

    RUI.Draw.Border(btn, RUI.C.BORDER)

    local fs = RUI.Draw.MakeLabel(btn)
    fs:SetTextColor(RUI.C.TEXT.r, RUI.C.TEXT.g, RUI.C.TEXT.b)
    fs:SetAllPoints()
    fs:SetJustifyH("CENTER")
    fs:SetJustifyV("MIDDLE")
    fs:SetText(label or "")
    btn._label = fs

    btn:SetScript("OnEnter", function()
        bg:SetColorTexture(RUI.C.HOVER.r, RUI.C.HOVER.g, RUI.C.HOVER.b, RUI.C.HOVER.a)
    end)
    btn:SetScript("OnLeave", function()
        bg:SetColorTexture(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, RUI.C.ELEMENT.a)
    end)
    btn:SetScript("OnMouseDown", function()
        bg:SetColorTexture(RUI.C.PRESS.r, RUI.C.PRESS.g, RUI.C.PRESS.b, RUI.C.PRESS.a)
    end)
    btn:SetScript("OnMouseUp", function(self)
        if self:IsMouseOver() then
            bg:SetColorTexture(RUI.C.HOVER.r, RUI.C.HOVER.g, RUI.C.HOVER.b, RUI.C.HOVER.a)
        else
            bg:SetColorTexture(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, RUI.C.ELEMENT.a)
        end
    end)

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

-- RUI.Widget.ToggleButton(parent, label, width, height, onChange[, iconName]) → Button
-- Two-state button matching DF's test-button pattern (UpdateTestButtonState).
--   Inactive: ELEMENT bg + ACCENT border/0.5 + TEXT_DIM label (and icon)
--   Active:   ACCENT*0.3 bg + ACCENT border/1.0 + ACCENT label (and icon)
--   Hover (inactive only): HOVER bg
-- iconName (optional): base name of an icon in RUI/Media/Icons/ (e.g. "visibility").
--   When provided, icon is anchored LEFT+6, label is anchored LEFT of icon+4 (DF pattern).
--   When absent, label fills the button centered.
-- btn:SetActive(bool)  btn:IsActive()  onChange(active) called on toggle.
function RUI.Widget.ToggleButton(parent, label, width, height, onChange, iconName)
    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    btn:SetSize(width, height or RUI.BTN_H)
    btn:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })

    local icon
    if iconName then
        icon = btn:CreateTexture(nil, "OVERLAY")
        icon:SetPoint("LEFT", btn, "LEFT", 6, 0)
        icon:SetSize(14, 14)
        icon:SetTexture("Interface\\AddOns\\" .. RUI._addonName .. "\\libs\\RUI\\Media\\Icons\\" .. iconName)
        btn._icon = icon
    end

    local fs = btn:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    if icon then
        fs:SetPoint("LEFT", icon, "RIGHT", 4, 0)
    else
        fs:SetAllPoints()
        fs:SetJustifyH("CENTER")
    end
    fs:SetJustifyV("MIDDLE")
    fs:SetText(label or "")
    btn._label = fs

    local active = false

    local function Refresh(hovered)
        if active then
            btn:SetBackdropColor(RUI.C.ACCENT.r * 0.3, RUI.C.ACCENT.g * 0.3, RUI.C.ACCENT.b * 0.3, 1)
            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)
            if icon then icon:SetVertexColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b) end
        elseif hovered then
            btn:SetBackdropColor(RUI.C.HOVER.r, RUI.C.HOVER.g, RUI.C.HOVER.b, 1)
            btn:SetBackdropBorderColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 0.5)
            fs:SetTextColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)
            if icon then icon:SetVertexColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b) end
        else
            btn:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
            btn:SetBackdropBorderColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 0.5)
            fs:SetTextColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)
            if icon then icon:SetVertexColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b) end
        end
    end

    btn:SetScript("OnEnter", function() Refresh(true) end)
    btn:SetScript("OnLeave", function() Refresh(false) end)
    btn:SetScript("OnClick", function()
        active = not active
        Refresh(btn:IsMouseOver())
        if onChange then onChange(active) end
    end)

    function btn:SetActive(state)
        active = state
        Refresh(btn:IsMouseOver())
    end
    function btn:IsActive() return active end

    Refresh(false)
    return btn
end

-- RUI.Widget.TabGroup(parent, options, totalWidth, height, onChange) → Frame
-- Row of tab buttons matching DF's PARTY/RAID/BINDS pattern (UpdateThemeColors).
--   options = { {value=, label=}, ... }  4px gap between buttons.
--   Selected:   ACCENT bg + ACCENT border/1.0 + white text
--   Unselected: ELEMENT bg + BORDER/0.5 + TEXT_DIM text
--   Hover (unselected): HOVER bg
-- container:SetSelected(value)  container:GetSelected()  onChange(value) on click.
function RUI.Widget.TabGroup(parent, options, totalWidth, height, onChange)
    local n   = #options
    local GAP = 4
    local btnH = height or RUI.BTN_H
    local btnW = math.floor((totalWidth - GAP * (n - 1)) / n)

    local container = CreateFrame("Frame", nil, parent)
    container:SetSize(totalWidth, btnH)

    local selected = options[1] and options[1].value
    local disabled = {}  -- set of values that are disabled
    local buttons  = {}

    local function Refresh()
        for _, entry in ipairs(options) do
            local b = buttons[entry.value]
            if b then
                if disabled[entry.value] then
                    b:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
                    b:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 0.5)
                    b._label:SetTextColor(0.3, 0.3, 0.3)
                elseif entry.value == selected then
                    b:SetBackdropColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 1)
                    b:SetBackdropBorderColor(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 1)
                    b._label:SetTextColor(1, 1, 1)
                else
                    b:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
                    b:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 0.5)
                    b._label:SetTextColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)
                end
            end
        end
    end

    for i, entry in ipairs(options) do
        local b = CreateFrame("Button", nil, container, "BackdropTemplate")
        b:SetSize(btnW, btnH)
        b:SetPoint("TOPLEFT", container, "TOPLEFT", (i - 1) * (btnW + GAP), 0)
        b:SetBackdrop({
            bgFile   = "Interface\\Buttons\\WHITE8x8",
            edgeFile = "Interface\\Buttons\\WHITE8x8",
            edgeSize = 1,
        })

        local fs = b:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
        fs:SetAllPoints()
        fs:SetJustifyH("CENTER")
        fs:SetJustifyV("MIDDLE")
        fs:SetText(entry.label or "")
        b._label = fs

        local val = entry.value
        b:SetScript("OnEnter", function()
            if disabled[val] then return end
            if val ~= selected then
                b:SetBackdropColor(RUI.C.HOVER.r, RUI.C.HOVER.g, RUI.C.HOVER.b, 1)
            end
        end)
        b:SetScript("OnLeave", function() Refresh() end)
        b:SetScript("OnClick", function()
            if disabled[val] then return end
            selected = val
            Refresh()
            if onChange then onChange(val) end
        end)

        buttons[entry.value] = b
    end

    container.buttons = buttons
    function container:SetSelected(value) selected = value; Refresh() end
    function container:GetSelected() return selected end
    function container:SetButtonEnabled(value, enabled)
        disabled[value] = not enabled
        Refresh()
    end

    Refresh()
    return container
end

-- RUI.Widget.CloseButton(parent, onClick) → Button
-- 20×20 close button matching DF exactly: ELEMENT bg + BORDER border at rest;
-- hover changes border to red (1, 0.3, 0.3) and icon to red — bg stays unchanged.
function RUI.Widget.CloseButton(parent, onClick)
    local btn = CreateFrame("Button", nil, parent, "BackdropTemplate")
    btn:SetSize(20, 20)
    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 icon = btn:CreateTexture(nil, "OVERLAY")
    icon:SetPoint("CENTER")
    icon:SetSize(12, 12)
    icon:SetTexture("Interface\\AddOns\\" .. RUI._addonName .. "\\libs\\RUI\\Media\\Icons\\close")
    icon:SetVertexColor(RUI.C.TEXT_DIM.r, RUI.C.TEXT_DIM.g, RUI.C.TEXT_DIM.b)

    btn:SetScript("OnEnter", function(self)
        self:SetBackdropBorderColor(1, 0.3, 0.3, 1)
        icon:SetVertexColor(1, 0.3, 0.3)
    end)
    btn:SetScript("OnLeave", function(self)
        self:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 0.5)
        icon:SetVertexColor(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
