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

-- RUI.Widget.Slider(parent, label, x, y, width, min, max, step, get, set)
-- → Slider frame, Refresh()
-- `width` is the TOTAL horizontal footprint of the control (track + 8px gap + 50px input).
-- Track width = width - 58. Pass SLD_W / SLD_HALF directly — no adjustment needed.
-- Total height consumed: LABEL_H(14) + SLIDER_H(8) + 12 + CONTROL_GAP(4) = 38px.

local function FormatValue(val)
    if val == math.floor(val) then
        return string.format("%d", val)
    elseif val * 10 == math.floor(val * 10) then
        return string.format("%.1f", val)
    else
        return string.format("%.2f", val)
    end
end

function RUI.Widget.Slider(parent, label, x, y, width, minVal, maxVal, stepVal, get, set)
    local trackW = width - 58  -- 8px gap + 50px input box = 58px reserved

    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(trackW)
    lbl:SetJustifyH("LEFT")
    lbl:SetText(label)

    -- Visual track (BackdropTemplate, dark background, 1px border)
    local track = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    track:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y - RUI.LABEL_H)
    track:SetSize(trackW, RUI.SLIDER_H)
    track:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    track:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
    track:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 1)

    -- Accent-colored fill (progress indicator, 1px inset from track edges)
    local fill = track:CreateTexture(nil, "ARTWORK")
    fill:SetPoint("LEFT", track, "LEFT", 1, 0)
    fill:SetHeight(RUI.SLIDER_H - 2)
    fill:SetColorTexture(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 0.8)

    -- Invisible Slider frame overlaid on track for drag input
    local sld = CreateFrame("Slider", nil, parent)
    sld:SetPoint("TOPLEFT", parent, "TOPLEFT", x, y - RUI.LABEL_H)
    sld:SetSize(trackW, RUI.SLIDER_H)
    sld:SetOrientation("HORIZONTAL")
    sld:SetMinMaxValues(minVal, maxVal)
    sld:SetValueStep(stepVal)
    sld:SetObeyStepOnDrag(true)
    sld:SetHitRectInsets(-4, -4, -8, -8)

    -- Thumb: solid accent-colored rectangle (taller than track, sits centered on it)
    local thumb = sld:CreateTexture(nil, "OVERLAY")
    thumb:SetSize(12, 16)
    thumb:SetColorTexture(RUI.C.ACCENT.r, RUI.C.ACCENT.g, RUI.C.ACCENT.b, 1)
    sld:SetThumbTexture(thumb)

    -- Value input box (editable, centered text, dark backdrop)
    local input = CreateFrame("EditBox", nil, parent, "BackdropTemplate")
    input:SetPoint("LEFT", track, "RIGHT", 8, 0)
    input:SetSize(50, 20)
    input:SetBackdrop({
        bgFile   = "Interface\\Buttons\\WHITE8x8",
        edgeFile = "Interface\\Buttons\\WHITE8x8",
        edgeSize = 1,
    })
    input:SetBackdropColor(RUI.C.ELEMENT.r, RUI.C.ELEMENT.g, RUI.C.ELEMENT.b, 1)
    input:SetBackdropBorderColor(RUI.C.BORDER.r, RUI.C.BORDER.g, RUI.C.BORDER.b, 1)
    input:SetFontObject(GameFontHighlightSmall)
    input:SetJustifyH("CENTER")
    input:SetAutoFocus(false)
    input:SetTextInsets(2, 2, 0, 0)
    input:SetMaxLetters(10)

    local function UpdateFill()
        local val = sld:GetValue()
        local pct = (maxVal > minVal) and ((val - minVal) / (maxVal - minVal)) or 0
        fill:SetWidth(math.max(1, pct * (trackW - 2)))
    end

    local suppressed = false

    sld:SetScript("OnValueChanged", function(_, val)
        if suppressed then return end
        if stepVal >= 1 then
            val = math.floor(val + 0.5)
        else
            val = math.floor(val / stepVal + 0.5) * stepVal
        end
        set(val)
        if not input:HasFocus() then
            input:SetText(FormatValue(val))
        end
        UpdateFill()
    end)

    input:SetScript("OnEnterPressed", function(self)
        local val = tonumber(self:GetText())
        if val then
            val = math.max(minVal, math.min(maxVal, val))
            set(val)
            suppressed = true
            sld:SetValue(val)
            suppressed = false
            self:SetText(FormatValue(val))
            UpdateFill()
        end
        self:ClearFocus()
    end)

    input:SetScript("OnEscapePressed", function(self)
        self:SetText(FormatValue(get()))
        self:ClearFocus()
    end)

    local function Refresh()
        suppressed = true
        local val = get()
        sld:SetValue(val)
        input:SetText(FormatValue(val))
        UpdateFill()
        suppressed = false
    end
    Refresh()
    return sld, Refresh
end
