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
    foodButton:SetNormalTexture("Interface\\Icons\\Inv_Drink_18") 
    foodButton:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
    RLMage.AddThinBorder(foodButton)
    
    RLMage.FoodButton = foodButton
    
    foodContainer = CreateFrame("Frame", "RLMageFoodContainer", foodButton)
    foodContainer:SetSize(170, 300)
    foodContainer:SetPoint("BOTTOM", foodButton, "TOP", 0, 5)
    
    foodContainer:Hide()
    
    foodButton:SetScript("OnClick", function(self, button)
        if InCombatLockdown() then return end
        if foodContainer:IsShown() then
            foodContainer:Hide()
        else
            RLMage.CloseAllMenus() -- Close other menus first
            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

    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(150, 20)
                -- btn:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
                btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
                -- RLMage.AddThinBorder(btn)
                foodButtons[i] = btn
            end

            RLMage.SetupSecureButton(btn, name, spellID)

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