-- Icon.lua
--
-- Eigenständiges Minimap-Icon für ChatTranslate — OHNE Abhängigkeit von
-- LibStub/LibDataBroker/LibDBIcon (die waren vorher referenziert, aber nie
-- gebündelt und hätten beim Laden zu einem nil-Fehler geführt).
--
-- Linksklick  -> Menü (ChatTranslateMainFrame) öffnen/schließen
-- Rechtsklick -> Schnellmenü (Ein/Aus, Menü öffnen)
-- Ziehen      -> Icon um die Minimap herum neu positionieren

ChatTranslateDB = ChatTranslateDB or {}
ChatTranslateDB.minimap = ChatTranslateDB.minimap or { hide = false, angle = 215 }

local minimapButton = CreateFrame("Button", "ChatTranslateMinimapButton", Minimap)
minimapButton:SetSize(31, 31)
minimapButton:SetFrameStrata("MEDIUM")
minimapButton:SetFrameLevel(8)
minimapButton:RegisterForClicks("LeftButtonUp", "RightButtonUp")
minimapButton:RegisterForDrag("LeftButton")

local overlay = minimapButton:CreateTexture(nil, "OVERLAY")
overlay:SetSize(53, 53)
overlay:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
overlay:SetPoint("TOPLEFT")

local icon = minimapButton:CreateTexture(nil, "BACKGROUND")
icon:SetSize(20, 20)
icon:SetTexture("Interface\\AddOns\\ChatTranslate\\Media\\Icon")
icon:SetPoint("CENTER", 0, 0)

local highlight = minimapButton:CreateTexture(nil, "HIGHLIGHT")
highlight:SetSize(30, 30)
highlight:SetTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
highlight:SetPoint("CENTER", 0, 0)

local function UpdatePosition()
    local angle = math.rad(ChatTranslateDB.minimap.angle or 215)
    local radius = 105
    local x = math.cos(angle) * radius
    local y = math.sin(angle) * radius
    minimapButton:ClearAllPoints()
    minimapButton:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

minimapButton:SetScript("OnDragStart", function(self)
    self:SetScript("OnUpdate", function()
        local mx, my = Minimap:GetCenter()
        local px, py = GetCursorPosition()
        local scale = Minimap:GetEffectiveScale()
        px, py = px / scale, py / scale
        local angle = math.deg(math.atan2(py - my, px - mx))
        ChatTranslateDB.minimap.angle = angle
        UpdatePosition()
    end)
end)
minimapButton:SetScript("OnDragStop", function(self)
    self:SetScript("OnUpdate", nil)
end)

minimapButton:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:AddLine("|cffe8c24aChatTranslate|r")
    GameTooltip:AddLine("Linksklick: Menü öffnen/schließen", 0.9, 0.9, 0.9)
    GameTooltip:AddLine("Rechtsklick: Schnellmenü", 0.9, 0.9, 0.9)
    GameTooltip:Show()
end)
minimapButton:SetScript("OnLeave", function() GameTooltip:Hide() end)

-- Rechtsklick-Schnellmenü (nutzt UIDropDownMenu, keine externe Lib nötig)
local quickMenu = CreateFrame("Frame", "ChatTranslateQuickMenu", UIParent, "UIDropDownMenuTemplate")

local function QuickMenu_Initialize(self, level)
    local info = UIDropDownMenu_CreateInfo()

    info.text = "ChatTranslate"
    info.isTitle = true
    info.notCheckable = true
    UIDropDownMenu_AddButton(info, level)

    info = UIDropDownMenu_CreateInfo()
    info.text = "Übersetzung aktiviert"
    info.checked = ChatTranslateDB.enabled
    info.func = function()
        ChatTranslateDB.enabled = not ChatTranslateDB.enabled
    end
    info.notCheckable = false
    UIDropDownMenu_AddButton(info, level)

    info = UIDropDownMenu_CreateInfo()
    info.text = "Menü öffnen"
    info.notCheckable = true
    info.func = function()
        if ChatTranslate_ToggleMainFrame then ChatTranslate_ToggleMainFrame() end
    end
    UIDropDownMenu_AddButton(info, level)

    info = UIDropDownMenu_CreateInfo()
    info.text = "Schließen"
    info.notCheckable = true
    info.func = function() CloseDropDownMenus() end
    UIDropDownMenu_AddButton(info, level)
end

minimapButton:SetScript("OnClick", function(self, clickedButton)
    if clickedButton == "LeftButton" then
        if ChatTranslate_ToggleMainFrame then
            ChatTranslate_ToggleMainFrame()
        else
            print("|cffe8c24aChatTranslate:|r Menü konnte nicht geladen werden.")
        end
    elseif clickedButton == "RightButton" then
        UIDropDownMenu_Initialize(quickMenu, QuickMenu_Initialize, "MENU")
        ToggleDropDownMenu(1, nil, quickMenu, self, 0, 0)
    end
end)

if not ChatTranslateDB.minimap.hide then
    minimapButton:Show()
else
    minimapButton:Hide()
end

UpdatePosition()
