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()
    
    -- Modern dark background (RLTotem style)
    RLMage.AddModernBackground(container)
    RLMage.AddModernBorder(container)
end

function RLMage.TogglePortalMenu()
    if not container then return end
    if container:IsShown() then
        container:Hide()
    else
        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

    -- Dynamische Container-Größe berechnen
    local buttonSize = 25
    local spacing = 5
    local padding = 8
    local numButtons = #spellsToShow
    local dynamicHeight = (numButtons * buttonSize) + ((numButtons - 1) * spacing) + padding
    if numButtons == 0 then dynamicHeight = 0 end
    container:SetSize(30, dynamicHeight)

    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(25, 25)
                btn:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
                btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                RLMage.AddModernButtonStyle(btn, 0, 0, 0, 1, 0.3, 0.3, 0.35, 0.6)
                portalButtons[i] = btn
            end
            
            RLMage.SetupSecureButton(btn, name, spellID)
            
            btn:ClearAllPoints()
            if previousBtn then
                btn:SetPoint("BOTTOM", previousBtn, "TOP", 0, spacing)
            else
                btn:SetPoint("BOTTOM", container, "BOTTOM", 0, padding/2)
            end
            
            btn:Show()
            previousBtn = btn
        end
    end
end
