-- UI/Controls/Dropdown.lua
--
-- A reusable dropdown 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 Dropdown = {}
SinsToolkit.UI.Controls.Dropdown = Dropdown

--[[
    Creates a new Dropdown control.
    @param parent The parent frame for this control.
    @param width (Optional) The width of the dropdown button. Defaults to 200.
    @return A table representing the new control, with its own API.
--]]
function Dropdown:Create(parent, width)
    local control = {}
    local callback
    local items = {}
    local currentValue

    -- Create the main frame for the dropdown, styled to look like a button.
    local frame = CreateFrame("Button", nil, parent, "UIDropDownMenuTemplate")
    frame:SetSize(width or 200, 32)
    UIDropDownMenu_Initialize(frame, function()
        for _, itemData in ipairs(items) do
            local info = UIDropDownMenu_CreateInfo()
            info.text = itemData.text
            info.value = itemData.value
            info.func = function()
                UIDropDownMenu_SetSelectedValue(frame, itemData.value)
                UIDropDownMenu_SetText(frame, itemData.text)
                currentValue = itemData.value
                if callback then
                    callback(itemData.value)
                end
            end
            UIDropDownMenu_AddButton(info)
        end
    end)
    
    --[[
        Public API for the new control instance.
    --]]
    
    --[[
        Sets the list of items to display in the dropdown.
        @param itemList A table of tables, where each inner table is { text = "Display Text", value = "itemValue" }.
    --]]
    function control:SetItems(itemList)
        items = type(itemList) == "table" and itemList or {}
        -- Set a default selection if one isn't already set
        if not currentValue and #items > 0 then
            self:SetValue(items[1].value)
        end
    end

    --[[
        Sets the currently selected value of the dropdown.
        @param value The value of the item to select.
    --]]
    function control:SetValue(value)
        for _, itemData in ipairs(items) do
            if itemData.value == value then
                UIDropDownMenu_SetSelectedValue(frame, itemData.value)
                UIDropDownMenu_SetText(frame, itemData.text)
                currentValue = itemData.value
                return
            end
        end
        -- If value not found, default to first item or empty
        if #items > 0 then
             UIDropDownMenu_SetSelectedValue(frame, items[1].value)
             UIDropDownMenu_SetText(frame, items[1].text)
             currentValue = items[1].value
        else
            UIDropDownMenu_SetText(frame, "")
            currentValue = nil
        end
    end

    --[[
        Gets the currently selected value.
        @return The value of the selected item.
    --]]
    function control:GetValue()
        return currentValue
    end

    --[[
        Sets the function to be called when the selection changes.
        @param func A function that accepts one argument: the new value.
    --]]
    function control:SetCallback(func)
        if type(func) == "function" then
            callback = func
        end
    end

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

    return control
end
