-------------------------
-- AiB Minimap Button (LibDataBroker)
-------------------------

-- Libraries laden
local LDB = LibStub and LibStub("LibDataBroker-1.1", true)

-- Fallback wenn keine Libraries
if not LDB then
    -- Fallback: Einfaches System
    local function CreateFallbackButton()
        if _G.AiBMinimapButton then return end
        
        local btn = CreateFrame("Button", "AiBMinimapButton", Minimap)
        btn:SetSize(32, 32)
        btn:SetFrameStrata("MEDIUM")
        btn:SetMovable(true)
        btn:EnableMouse(true)
        btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
        
        local icon = btn:CreateTexture(nil, "BACKGROUND")
        icon:SetTexture("Interface\\AddOns\\aib\\textures\\aib_icon.tga")
        icon:SetSize(20, 20)
        icon:SetPoint("CENTER")
        
        btn:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
        
        btn:SetScript("OnEnter", function(self)
            GameTooltip:SetOwner(self, "ANCHOR_LEFT")
            GameTooltip:AddLine("|cFFFF0000AiB|r - All into Boss", 1, 1, 1)
            GameTooltip:AddLine("Links-Klick: Öffnen", 0.8, 0.8, 0.8)
            GameTooltip:AddLine("Rechts-Klick: Menü", 0.8, 0.8, 0.8)
            GameTooltip:Show()
        end)
        btn:SetScript("OnLeave", function() GameTooltip:Hide() end)
        
        btn:RegisterForDrag("LeftButton")
        btn:SetScript("OnDragStart", function(self) self:StartMoving() end)
        btn:SetScript("OnDragStop", function(self)
            self:StopMovingOrSizing()
            local centerX, centerY = Minimap:GetCenter()
            local btnX, btnY = self:GetCenter()
            if centerX and centerY and btnX and btnY then
                local dx, dy = btnX - centerX, btnY - centerY
                local angle = math.atan2(dy, dx)
                local radius = (Minimap:GetWidth() / 2) + 5
                local x, y = radius * math.cos(angle), radius * math.sin(angle)
                self:ClearAllPoints()
                self:SetPoint("CENTER", Minimap, "CENTER", x, y)
                AiB_Settings = AiB_Settings or {}
                AiB_Settings.minimap_button = {x = x, y = y}
            end
        end)
        
        if AiB_Settings and AiB_Settings.minimap_button then
            btn:SetPoint("CENTER", Minimap, "CENTER", 
                AiB_Settings.minimap_button.x or 0,
                AiB_Settings.minimap_button.y or -65)
        else
            btn:SetPoint("CENTER", Minimap, "CENTER", 0, -65)
        end
        
        btn:Show()
    end
    
    -- SOFORT erstellen statt auf Event zu warten
    C_Timer.After(0.5, CreateFallbackButton)
    return
end

-------------------------
-- LibDataBroker System
-------------------------

-- DataBroker Object erstellen
local AiBLDB = LDB:NewDataObject("AiB", {
    type = "launcher",
    label = "AiB - All into Boss",
    icon = "Interface\\AddOns\\aib\\textures\\aib_icon.tga",
    
    OnClick = function(self, button)
        if button == "LeftButton" then
            -- Wird in aib.lua gesetzt
        elseif button == "RightButton" then
            -- Wird in aib.lua gesetzt
        end
    end,
    
    OnTooltipShow = function(tooltip)
        tooltip:AddLine("|cFFFF0000AiB|r - All into Boss")
        tooltip:AddLine("Links-Klick: Öffnen", 1, 1, 1)
        tooltip:AddLine("Rechts-Klick: Menü", 1, 1, 1)
    end,
})

-- Erstelle Button SOFORT (nicht auf Event warten!)
local function CreateSimpleButton()
    if _G.AiBMinimapButton then return end
    
    local btn = CreateFrame("Button", "AiBMinimapButton", Minimap)
    btn:SetSize(32, 32)
    btn:SetFrameStrata("MEDIUM")
    btn:SetMovable(true)
    btn:EnableMouse(true)
    btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
    
    local icon = btn:CreateTexture(nil, "BACKGROUND")
    icon:SetTexture(AiBLDB.icon)
    icon:SetSize(20, 20)
    icon:SetPoint("CENTER")
    
    btn:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
    
    btn:SetScript("OnClick", function(self, button)
        if AiBLDB.OnClick then
            AiBLDB.OnClick(self, button)
        end
    end)
    
    btn:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        if AiBLDB.OnTooltipShow then
            AiBLDB.OnTooltipShow(GameTooltip)
        end
        GameTooltip:Show()
    end)
    btn:SetScript("OnLeave", function() GameTooltip:Hide() end)
    
    btn:RegisterForDrag("LeftButton")
    btn:SetScript("OnDragStart", function(self) self:StartMoving() end)
    btn:SetScript("OnDragStop", function(self)
        self:StopMovingOrSizing()
        local centerX, centerY = Minimap:GetCenter()
        local btnX, btnY = self:GetCenter()
        if centerX and centerY and btnX and btnY then
            local dx, dy = btnX - centerX, btnY - centerY
            local angle = math.atan2(dy, dx)
            local radius = (Minimap:GetWidth() / 2) + 5
            local x, y = radius * math.cos(angle), radius * math.sin(angle)
            self:ClearAllPoints()
            self:SetPoint("CENTER", Minimap, "CENTER", x, y)
            AiB_Settings = AiB_Settings or {}
            AiB_Settings.minimap_button = {x = x, y = y}
        end
    end)
    
    if AiB_Settings and AiB_Settings.minimap_button then
        btn:SetPoint("CENTER", Minimap, "CENTER",
            AiB_Settings.minimap_button.x or 0,
            AiB_Settings.minimap_button.y or -65)
    else
        btn:SetPoint("CENTER", Minimap, "CENTER", 0, -65)
    end
    
    btn:Show()
end

-- Button SOFORT erstellen mit kleiner Verzögerung
C_Timer.After(0.5, CreateSimpleButton)

-- Exportiere für andere Dateien
_G.AiBLDB = AiBLDB
