-- EmoteMenuOptions.lua
-- A compact options strip shown below the category area in the main UI.

local ADDON_NAME, EmoteMenu = ...

local Options = {}
EmoteMenu.Options = Options

-- Public registration so UI can add items without tight coupling
local items = {}
function Options.Register(id, label, onClick, tooltip)
    if not id or items[id] then return end
    table.insert(items, { id = id, label = label, onClick = onClick, tooltip = tooltip })
end

-- No default buttons for now; keep the strip focused on the launcher mode dropdown.

-- Factory to create the options strip under the category panel
function Options.Create(parent)
    if not parent then return end
    local frame = CreateFrame("Frame", nil, parent, "BackdropTemplate")
    frame:SetHeight(64)
    frame:SetPoint("BOTTOMLEFT", parent, "BOTTOMLEFT", 0, 0)
    frame:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, 0)

    if frame.SetBackdrop then
        frame:SetBackdrop({
            bgFile = "Interface/Buttons/WHITE8x8",
            edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
            tile = true, tileSize = 8, edgeSize = 12,
            insets = { left = 2, right = 2, top = 2, bottom = 2 }
        })
        frame:SetBackdropColor(0.08, 0.08, 0.08, 0.85)
        frame:SetBackdropBorderColor(0.3, 0.3, 0.3, 1)
    end

    -- Launcher mode selection (use native dropdown when available; no fallback button)
    local labelFor = {
        chat = "Chat",
        floating = "Floating",
        none = "None",
    }

    local function currentMode()
        if EmoteMenu.GetLauncherMode then return EmoteMenu.GetLauncherMode() end
        return "chat"
    end

    local dropdownBottomAnchor
    if type(UIDropDownMenu_CreateInfo) == "function" and type(UIDropDownMenu_SetWidth) == "function" then
        local launcherDropdown = CreateFrame("Frame", "EmoteMenuLauncherModeDropdown", frame, "UIDropDownMenuTemplate")
        launcherDropdown:SetPoint("TOPLEFT", frame, "TOPLEFT", -16, 0)
        UIDropDownMenu_SetWidth(launcherDropdown, 160)

        local function refresh()
            local mode = currentMode()
            UIDropDownMenu_SetText(launcherDropdown, "Button: " .. (labelFor[mode] or mode))
        end

        UIDropDownMenu_Initialize(launcherDropdown, function(self, level, menuList)
            local modeNow = currentMode()
            local function add(text, value)
                local info = UIDropDownMenu_CreateInfo()
                info.text = text
                info.checked = (modeNow == value)
                info.func = function()
                    if EmoteMenu.SetLauncherMode then EmoteMenu.SetLauncherMode(value) end
                    refresh()
                end
                UIDropDownMenu_AddButton(info, level)
            end
            add("Chat", "chat")
            add("Floating", "floating")
            add("None", "none")
        end)

        launcherDropdown:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_TOP")
            GameTooltip:SetText("Select how to show the button for Emote Menu")
            GameTooltip:Show()
        end)
        launcherDropdown:SetScript("OnLeave", function()
            if GameTooltip:IsShown() then GameTooltip:Hide() end
        end)

        refresh()
        dropdownBottomAnchor = launcherDropdown
    end

    -- Checkbox: Show Labels (placed below the launcher dropdown or at top if dropdown missing)
    local check = CreateFrame("CheckButton", "EmoteMenuShowLabelsCheck", frame, "InterfaceOptionsCheckButtonTemplate")
    local anchor = dropdownBottomAnchor or frame
    if dropdownBottomAnchor then
        check:SetPoint("TOPLEFT", dropdownBottomAnchor, "BOTTOMLEFT", 20, 0)
    else
        check:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -4)
    end
    _G[check:GetName() .. "Text"]:SetText("Show Emote Labels")
    if EmoteMenu.AreEmoteLabelsShown and EmoteMenu.AreEmoteLabelsShown() then
        check:SetChecked(true)
    else
        check:SetChecked(false)
    end
    check:SetScript("OnClick", function(self)
        if EmoteMenu.ToggleEmoteLabels then EmoteMenu.ToggleEmoteLabels(self:GetChecked()) end
    end)
    check:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_TOP")
        GameTooltip:SetText("Toggle showing text labels next to each emote icon.")
        GameTooltip:Show()
    end)
    check:SetScript("OnLeave", function()
        if GameTooltip:IsShown() then GameTooltip:Hide() end
    end)

    return frame
end
