local addonName, RLMage = ...

local buffIDs = {
    intellect = {27080, 10157, 10156, 1461, 1460, 1459, 1458}, -- Arkaner Intellekt
    brilliance = {27126, 23028}, -- Arkane Brillanz
    molten = {30482}, -- Glühende Rüstung
    mage = {27125, 22783, 22782, 6117}, -- Magische Rüstung
    ice = {27124, 10220, 10219, 7320, 7302, 7301, 7300, 168}, -- Eisrüstung / Frostrüstung
    dampen = {33944, 10174, 10173, 8451, 8450, 604}, -- Magie dämpfen
    amplify = {33946, 27130, 10170, 10169, 8455, 1008}, -- Magie verstärken
    wardFire = {27128, 10225, 10223, 8458, 8457, 543}, -- Feuerzauberschutz
    wardFrost = {32796, 28609, 10177, 8462, 8461, 6143}, -- Frostzauberschutz
}

-- Expose for Options
RLMage.buffIDs = buffIDs

local buffButtons = {}
local buffButton
local buffContainer

function RLMage.InitBuffs()
    if not RLMage.MainButton then return end
    
    -- Create Main Buff Button (Icon: Mage Armor)
    -- Parent is MainButton to ensure it hides with it, but anchor is managed dynamically
    buffButton = CreateFrame("Button", "RLMageBuffButton", RLMage.MainButton, "SecureActionButtonTemplate")
    buffButton:SetSize(30, 30)
    buffButton:SetNormalTexture("Interface\\Icons\\Spell_Magic_MageArmor") 
    buffButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
    RLMage.AddThinBorder(buffButton)
    
    RLMage.BuffButton = buffButton
    
    buffContainer = CreateFrame("Frame", "RLMageBuffContainer", buffButton)
    buffContainer:SetSize(170, 300)
    buffContainer:SetPoint("BOTTOM", buffButton, "TOP", 0, 5)
    
    buffContainer:Hide()
    
    buffButton:SetScript("OnClick", function(self, button)
        if InCombatLockdown() then return end
        if buffContainer:IsShown() then
            buffContainer:Hide()
        else
            RLMage.CloseAllMenus() -- Close other menus first
            RLMage.UpdateBuffButtons()
            buffContainer:Show()
        end
    end)
end

function RLMage.UpdateBuffButtons()
    if not buffContainer then return end

    -- Verstecken
    for _, btn in pairs(buffButtons) do
        btn:Hide()
        btn:ClearAllPoints()
    end

    if not RLMageDB.showBuffs then return end

    local spellsToShow = {}

    -- Reihenfolge der Buffs
    local order = {
        buffIDs.intellect,
        buffIDs.brilliance,
        buffIDs.molten,
        buffIDs.mage,
        buffIDs.ice,
        buffIDs.dampen,
        buffIDs.amplify,
        buffIDs.wardFire,
        buffIDs.wardFrost
    }

    for _, idList in ipairs(order) do
        local best = RLMage.FindBest(idList)
        if best then table.insert(spellsToShow, best) end
    end

    local previousBtn = nil
    for i, spellID in ipairs(spellsToShow) do
        local name, _, icon = GetSpellInfo(spellID)
        if name then
            local btn = buffButtons[i]
            if not btn then
                btn = CreateFrame("Button", "RLMageBuffSubBtn"..i, buffContainer, "SecureActionButtonTemplate")
                btn:SetSize(150, 20)
                -- btn:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
                btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                -- RLMage.AddThinBorder(btn)
                buffButtons[i] = btn
            end

            RLMage.SetupSecureButton(btn, name, spellID)

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