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 teleportButtons = {}
local container
local teleportContainer
local teleportButton

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.InitTeleports()
    if not RLMage.MainButton then return end
    
    teleportButton = CreateFrame("Button", "RLMageTeleportButton", RLMage.MainButton, "SecureActionButtonTemplate")
    teleportButton:SetSize(30, 30)
    teleportButton:SetNormalTexture("Interface\\Icons\\Spell_Arcane_TeleportStormWind")
    teleportButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
    RLMage.AddThinBorder(teleportButton)
    
    RLMage.TeleportButton = teleportButton
    
    teleportContainer = CreateFrame("Frame", "RLMageTeleportContainer", teleportButton)
    teleportContainer:SetSize(30, 300)
    teleportContainer:SetPoint("BOTTOM", teleportButton, "TOP", 0, 5)
    teleportContainer:Hide()
    
    teleportButton:SetScript("OnClick", function(self, button)
        if InCombatLockdown() then return end
        if teleportContainer:IsShown() then
            teleportContainer:Hide()
        else
            RLMage.CloseAllMenus()
            RLMage.UpdateTeleportButtons()
            teleportContainer:Show()
        end
    end)
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
    
    for _, btn in pairs(portalButtons) do
        btn:Hide()
        btn:ClearAllPoints()
    end

    local portalSpells = {}

    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 or not RLMageDB.hiddenSpells[id]) then
                table.insert(portalSpells, id)
            end
        end
    end

    local previousBtn = nil
    local index = 1

    for _, spellID in ipairs(portalSpells) do
        local name, _, icon = GetSpellInfo(spellID)
        if name then
            local btn = portalButtons[index]
            if not btn then
                btn = CreateFrame("Button", "RLMagePortalBtn"..index, 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[index] = 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
            index = index + 1
        end
    end
end

function RLMage.UpdateTeleportButtons()
    if not teleportContainer then return end
    
    for _, btn in pairs(teleportButtons) do
        btn:Hide()
        btn:ClearAllPoints()
    end

    if not RLMageDB.showTeleports then return end

    local previousBtn = nil
    local index = 1

    for _, spellID in ipairs(teleportIDs) do
        local known = IsSpellKnown(spellID)
        if not known and RLMage.SpellBookCache and RLMage.SpellBookCache[spellID] then
            known = true
        end
        if known and (not RLMageDB.hiddenSpells or not RLMageDB.hiddenSpells[spellID]) then
            local name = GetSpellInfo(spellID)
            if name then
                local btn = teleportButtons[index]
                if not btn then
                    btn = CreateFrame("Button", "RLMageTeleportBtn"..index, teleportContainer, "SecureActionButtonTemplate")
                    btn:SetSize(150, 20)
                    btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                    teleportButtons[index] = btn
                end

                RLMage.SetupSecureButton(btn, name, spellID)

                btn:ClearAllPoints()
                if previousBtn then
                    btn:SetPoint("BOTTOM", previousBtn, "TOP", 0, 0)
                else
                    btn:SetPoint("BOTTOM", teleportContainer, "BOTTOM", 0, 8)
                end

                btn:Show()
                previousBtn = btn
                index = index + 1
            end
        end
    end
end
