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)
    
    -- Modern button style (RLTotem style - purple for arcane/magic)
    RLMage.AddModernButtonStyle(buffButton, 0, 0, 0, 1, 0.6, 0.3, 0.8, 0.6)
    
    buffButton.icon = buffButton:CreateTexture(nil, "ARTWORK")
    buffButton.icon:SetAllPoints()
    buffButton.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    buffButton.icon:SetTexture("Interface\\Icons\\Spell_Magic_MageArmor")
    
    buffButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
    
    -- Highlight effect
    buffButton:SetScript("OnEnter", function(self)
        if self.border then
            self.border:SetColorTexture(0.6, 0.3, 0.8, 1)
        end
    end)
    buffButton:SetScript("OnLeave", function(self)
        if self.border then
            self.border:SetColorTexture(0.6, 0.3, 0.8, 0.6)
        end
    end)
    
    RLMage.BuffButton = buffButton
    
    buffContainer = CreateFrame("Frame", "RLMageBuffContainer", buffButton)
    buffContainer:SetSize(30, 300)
    buffContainer:SetPoint("BOTTOM", buffButton, "TOP", 0, 5)
    buffContainer:Hide()
    
    -- Modern dark background (RLTotem style)
    RLMage.AddModernBackground(buffContainer)
    RLMage.AddModernBorder(buffContainer)
    
    buffButton:SetScript("OnClick", function(self, button)
        if InCombatLockdown() then return end
        if buffContainer:IsShown() then
            buffContainer:Hide()
        else
            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

    -- 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
    buffContainer:SetSize(30, dynamicHeight)

    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(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)
                buffButtons[i] = btn
            end

            RLMage.SetupSecureButton(btn, name, spellID)

            btn:ClearAllPoints()
            if previousBtn then
                btn:SetPoint("BOTTOM", previousBtn, "TOP", 0, spacing)
            else
                btn:SetPoint("BOTTOM", buffContainer, "BOTTOM", 0, padding/2)
            end
            
            btn:Show()
            previousBtn = btn
        end
    end
end
