local addonName, RLMage = ...

local conjureIDs = {
    water = {27090, 37420, 10140, 10139, 10138, 3742, 1015, 1014, 1013, 5504}, -- Wasser
    food = {33717, 28612, 27089, 10145, 10144, 8401, 1016, 1017, 1113, 1114}, -- Essen
    table = {43987, 34061}, -- Tisch (Ritual der Erfrischung)
    gem = {27101, 10054, 10053, 759, 3552} -- Mana Smaragd/Rubin etc.
}

-- Expose for Options
RLMage.conjureIDs = conjureIDs

local foodButtons = {}
local foodButton
local foodContainer

function RLMage.InitFood()
    if not RLMage.MainButton then return end
    
    -- Create Main Food Button (Icon: Water)
    foodButton = CreateFrame("Button", "RLMageFoodButton", RLMage.MainButton, "SecureActionButtonTemplate")
    foodButton:SetSize(30, 30)
    -- Positioning handled by Core.lua UpdateLayout
    
    -- Modern button style (RLTotem style - blue for water)
    RLMage.AddModernButtonStyle(foodButton, 0, 0, 0, 1, 0.2, 0.4, 0.8, 0.6)
    
    foodButton.icon = foodButton:CreateTexture(nil, "ARTWORK")
    foodButton.icon:SetAllPoints()
    foodButton.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    foodButton.icon:SetTexture("Interface\\Icons\\Inv_Drink_18")
    
    foodButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
    
    -- Highlight effect
    foodButton:SetScript("OnEnter", function(self)
        if self.border then
            self.border:SetColorTexture(0.2, 0.4, 0.8, 1)
        end
    end)
    foodButton:SetScript("OnLeave", function(self)
        if self.border then
            self.border:SetColorTexture(0.2, 0.4, 0.8, 0.6)
        end
    end)
    
    RLMage.FoodButton = foodButton
    
    foodContainer = CreateFrame("Frame", "RLMageFoodContainer", foodButton)
    foodContainer:SetSize(30, 300)
    foodContainer:SetPoint("BOTTOM", foodButton, "TOP", 0, 5)
    foodContainer:Hide()
    
    -- Modern dark background (RLTotem style)
    RLMage.AddModernBackground(foodContainer)
    RLMage.AddModernBorder(foodContainer)
    
    foodButton:SetScript("OnClick", function(self, button)
        if InCombatLockdown() then return end
        if foodContainer:IsShown() then
            foodContainer:Hide()
        else
            RLMage.UpdateFoodButtons()
            foodContainer:Show()
        end
    end)
end

function RLMage.UpdateFoodButtons()
    if not foodContainer then return end

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

    if not RLMageDB.showFood then return end

    local spellsToShow = {}

    -- Beste Items sammeln
    local bestWater = RLMage.FindBest(conjureIDs.water)
    if bestWater then table.insert(spellsToShow, bestWater) end

    local bestFood = RLMage.FindBest(conjureIDs.food)
    if bestFood then table.insert(spellsToShow, bestFood) end

    local tableSpell = RLMage.FindBest(conjureIDs.table)
    if tableSpell then table.insert(spellsToShow, tableSpell) end

    local bestGem = RLMage.FindBest(conjureIDs.gem)
    if bestGem then table.insert(spellsToShow, bestGem) 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
    foodContainer:SetSize(30, dynamicHeight)

    local previousBtn = nil
    for i, spellID in ipairs(spellsToShow) do
        local name, _, icon = GetSpellInfo(spellID)
        if name then
            local btn = foodButtons[i]
            if not btn then
                btn = CreateFrame("Button", "RLMageFoodSubBtn"..i, foodContainer, "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)
                foodButtons[i] = btn
            end

            RLMage.SetupSecureButton(btn, name, spellID)

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