local addonName, RLMage = ...

local teleportIDs = {
    3561, 3562, 3565, 32271, 33690, 49359, -- Allianz
    3567, 3563, 3566, 32272, 35715, 49358  -- Horde
}

local portalIDs = {
    10059, 11416, 11419, 32266, 33691, 49360, -- Allianz
    11417, 11418, 11420, 32267, 35717, 49361  -- Horde
}

-- Expose for Options
RLMage.teleportIDs = teleportIDs
RLMage.portalIDs = portalIDs

local portalButtons = {}
local container

function RLMage.InitPortals()
    if not RLMage.MainButton then return end
    
    container = CreateFrame("Frame", "RLMagePortalContainer", RLMage.MainButton)
    container:SetSize(30, 300) 
    container:SetPoint("BOTTOM", RLMage.MainButton, "TOP", 0, 5)
    
    container:Hide()
end

function RLMage.TogglePortalMenu()
    if not container then return end
    if container:IsShown() then
        container:Hide()
    else
        RLMage.CloseAllMenus() -- Close other menus first
        RLMage.UpdatePortalButtons() 
        container:Show()
    end
end

function RLMage.UpdatePortalButtons()
    if not container then return end
    
    -- Alte Buttons verstecken
    for _, btn in pairs(portalButtons) do
        btn:Hide()
        btn:ClearAllPoints()
    end

    local spellsToShow = {}
    
    -- Füge Teleporte hinzu, wenn aktiviert
    if RLMageDB.showTeleports then
        for _, id in ipairs(teleportIDs) do
            local known = IsSpellKnown(id)
            if not known and RLMage.SpellBookCache and RLMage.SpellBookCache[id] then
                known = true
            end
            
            if known and not RLMageDB.hiddenSpells[id] then
                table.insert(spellsToShow, id)
            end
        end
    end

    -- Füge Portale hinzu, wenn aktiviert
    if RLMageDB.showPortals then
        for _, id in ipairs(portalIDs) do
            local known = IsSpellKnown(id)
            
            if not known and RLMage.SpellBookCache and RLMage.SpellBookCache[id] then
                known = true
            end

            if known and not RLMageDB.hiddenSpells[id] then
                table.insert(spellsToShow, id)
            end
        end
    end

    local previousBtn = nil
    
    for i, spellID in ipairs(spellsToShow) do
        local name, _, icon = GetSpellInfo(spellID)
        if name then
            local btn = portalButtons[i]
            if not btn then
                btn = CreateFrame("Button", "RLMagePortalBtn"..i, container, "SecureActionButtonTemplate")
                btn:SetSize(150, 20) 
                -- btn:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square") -- Handled in Utils
                btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                -- RLMage.AddThinBorder(btn) -- No border for gossip style
                portalButtons[i] = btn
            end
            
            RLMage.SetupSecureButton(btn, name, spellID)
            
            btn:ClearAllPoints()
            if previousBtn then
                btn:SetPoint("BOTTOM", previousBtn, "TOP", 0, 0)
            else
                btn:SetPoint("BOTTOM", container, "BOTTOM", 0, 8) -- Padding bottom
            end
            
            btn:Show()
            previousBtn = btn
        end
    end
end
