-- 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

    local leftOffset = 0

    -- Launcher mode selection (prefer real dropdown; fallback to EasyMenu)
    local labelFor = {
        chat = "Chat",
        floating = "Floating",
        none = "None",
    }

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

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

        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()
    else
        -- Fallback: use a button that opens EasyMenu or cycles
        local launcherModeButton = CreateFrame("Button", "EmoteMenuLauncherModeButton", frame, "UIPanelButtonTemplate")
        launcherModeButton:SetSize(130, 20)
        launcherModeButton:SetPoint("LEFT", leftOffset, 0)

        local function refreshLabel()
            local mode = currentMode()
            local text = labelFor[mode] or mode
            launcherModeButton:SetText("Launcher: " .. text)
        end

        local dropdownMenuFrame = CreateFrame("Frame", "EmoteMenuLauncherModeDropDown", frame, "UIDropDownMenuTemplate")
        local function buildLauncherMenu()
            local modeNow = currentMode()
            local menu = {}
            table.insert(menu, { text = "Chat", checked = (modeNow == "chat"), func = function()
                if EmoteMenu.SetLauncherMode then EmoteMenu.SetLauncherMode("chat") end
                refreshLabel()
            end })
            table.insert(menu, { text = "Floating", checked = (modeNow == "floating"), func = function()
                if EmoteMenu.SetLauncherMode then EmoteMenu.SetLauncherMode("floating") end
                refreshLabel()
            end })
            table.insert(menu, { text = "None", checked = (modeNow == "none"), func = function()
                if EmoteMenu.SetLauncherMode then EmoteMenu.SetLauncherMode("none") end
                refreshLabel()
            end })
            return menu
        end

        launcherModeButton:SetScript("OnClick", function(self)
            if EasyMenu then
                EasyMenu(buildLauncherMenu(), dropdownMenuFrame, self, 0, 0, "MENU")
            else
                local order = { "chat", "floating", "none" }
                local cur = currentMode()
                local idx = 1
                for i, m in ipairs(order) do if m == cur then idx = i break end end
                idx = idx + 1; if idx > #order then idx = 1 end
                if EmoteMenu.SetLauncherMode then EmoteMenu.SetLauncherMode(order[idx]) end
                refreshLabel()
            end
        end)

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

        refreshLabel()
    end

    return frame
end
