-- UI/Controls/Slider.lua
--
-- A reusable slider control that matches the Toolkit's visual style.

local addonName, SinsToolkit = ...

-- Ensure the namespace exists.
SinsToolkit.UI = SinsToolkit.UI or {}
SinsToolkit.UI.Controls = SinsToolkit.UI.Controls or {}

local Slider = {}
SinsToolkit.UI.Controls.Slider = Slider

--[[
    Creates a new Slider control.
    @param parent The parent frame for this control.
    @param text The label text for the slider.
    @param min The minimum value of the slider.
    @param max The maximum value of the slider.
    @param step The increment step of the slider.
    @param tooltip (Optional) The tooltip text to display on hover.
    @return A table representing the new control, with its own API.
--]]
function Slider:Create(parent, text, min, max, step, tooltip)
    local control = {}
    local callback
    local formatFunc

    -- Create the underlying Blizzard widget.
    local slider = CreateFrame("Slider", nil, parent, "OptionsSliderTemplate")
    slider:SetSize(180, 20)
    
    local label = _G[slider:GetName() .. "Text"]
    label:SetText(text or "")

    slider:SetMinMaxValues(min or 0, max or 1)
    slider:SetValueStep(step or 0.1)

    -- Create the text that displays the current value.
    local valueText = slider:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
    valueText:SetPoint("LEFT", slider, "RIGHT", 15, 0)
    
    -- Private handler for when the value changes.
    slider:SetScript("OnValueChanged", function(self, value)
        local text = formatFunc and formatFunc(value) or ("%.2f"):format(value)
        valueText:SetText(text)
        if callback then
            callback(value)
        end
    end)

    -- Private handler for tooltips.
    slider:SetScript("OnEnter", function(self)
        if tooltip and tooltip ~= "" then
            GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
            GameTooltip:SetText(tooltip, nil, nil, nil, nil, true)
            GameTooltip:Show()
        end
    end)
    slider:SetScript("OnLeave", function()
        GameTooltip:Hide()
    end)
    
    --[[
        Public API for the new control instance.
    --]]

    --[[
        Sets the current value of the slider.
        @param value (number) The value to set.
    --]]
    function control:SetValue(value)
        slider:SetValue(value or 0)
        local text = formatFunc and formatFunc(value) or ("%.2f"):format(value)
        valueText:SetText(text)
    end

    --[[
        Gets the current value of the slider.
        @return (number) The current value.
    --]]
    function control:GetValue()
        return slider:GetValue()
    end

    --[[
        Sets the function to be called when the slider value changes.
        @param func A function that accepts one argument: the new value (number).
    --]]
    function control:SetCallback(func)
        if type(func) == "function" then
            callback = func
        end
    end
    
    --[[
        Sets the label text for the slider.
        @param labelText (string) The new label.
    --]]
    function control:SetLabel(labelText)
        label:SetText(labelText or "")
    end
    
    --[[
        Sets the function used to format the value text.
        @param func A function that accepts the raw value and returns a formatted string.
    --]]
    function control:SetFormatFunc(func)
        if type(func) == "function" then
            formatFunc = func
        end
    end

    --[[
        Returns the underlying Frame widget for positioning.
        @return The Slider Frame.
    --]]
    function control:GetFrame()
        return slider
    end

    return control
end
