-- UI/Controls/Toolbar.lua
--
-- A reusable control for creating a horizontal toolbar of consistently
-- spaced buttons.

local addonName, SinsToolkit = ...

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

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

function Toolbar:Create(parent)
    local control = {}
    local lastButton = nil
    local Theme = SinsToolkit.UI.Theme
    
    local BUTTON_SIZE = Theme.GetButtonSize()
    local BUTTON_SPACING = Theme.GetSpacing("toolbar_gap")

    -- Main container frame for the toolbar.
    local frame = CreateFrame("Frame", nil, parent)
    
    --[[
        Public API for the new control instance.
    --]]

    --[[
        Adds a new button to the toolbar.
        @param text The text to display on the button.
        @param tooltip (Optional) The tooltip text for the button.
        @return The newly created Button widget.
    --]]
    function control:AddButton(text, tooltip)
        local button = CreateFrame("Button", nil, frame, "UIPanelButtonTemplate")
        button:SetSize(BUTTON_SIZE.width, BUTTON_SIZE.height)
        button:SetText(text or "")

        if not lastButton then
            -- This is the first button.
            button:SetPoint("TOPLEFT")
        else
            -- Anchor this button to the right of the previous one.
            button:SetPoint("LEFT", lastButton, "RIGHT", BUTTON_SPACING, 0)
        end
        
        lastButton = button
        
        if tooltip and tooltip ~= "" then
            button:SetScript("OnEnter", function(self)
                GameTooltip:SetOwner(self, "ANCHOR_BOTTOM")
                GameTooltip:SetText(tooltip, nil, nil, nil, nil, true)
                GameTooltip:Show()
            end)
            button:SetScript("OnLeave", function()
                GameTooltip:Hide()
            end)
        end
        
        return button
    end

    --[[
        Returns the underlying Frame widget for positioning the entire toolbar.
        @return The Toolbar's main Frame.
    --]]
    function control:GetFrame()
        return frame
    end

    return control
end
