local _, ns = ...

------------------------------------------------------------
-- Minimap button (standard WoW style)
------------------------------------------------------------
local ICON_TEXTURE = "Interface\\AddOns\\YippYappHelper\\icon"
local DEFAULT_ANGLE = 220

local button = CreateFrame("Button", "YippYappMinimapButton", Minimap)
button:SetSize(32, 32)
button:SetFrameStrata("MEDIUM")
button:SetFrameLevel(8)
button:SetMovable(true)
button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
button:RegisterForDrag("LeftButton")
ns.MinimapButton = button

-- Icon texture
local icon = button:CreateTexture(nil, "BACKGROUND")
icon:SetSize(21, 21)
icon:SetPoint("TOPLEFT", 7, -6)
icon:SetTexture(ICON_TEXTURE)
button.icon = icon

-- Border — the MiniMap-TrackingBorder graphic is offset within its texture,
-- so it needs specific anchoring to line up with the icon
local border = button:CreateTexture(nil, "OVERLAY")
border:SetSize(52, 52)
border:SetPoint("TOPLEFT", 0, 0)
border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")

-- Highlight
local highlight = button:GetHighlightTexture() or button:CreateTexture(nil, "HIGHLIGHT")
button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")

------------------------------------------------------------
-- Position around minimap edge
------------------------------------------------------------
local function UpdatePosition(angle)
    local radian = math.rad(angle)
    local radius = 80
    local x = math.cos(radian) * radius
    local y = math.sin(radian) * radius
    button:ClearAllPoints()
    button:SetPoint("CENTER", Minimap, "CENTER", x, y)
end

local function GetAngleFromCursor()
    local cx, cy = Minimap:GetCenter()
    local mx, my = GetCursorPosition()
    local scale = Minimap:GetEffectiveScale()
    mx, my = mx / scale, my / scale
    return math.deg(math.atan2(my - cy, mx - cx))
end

------------------------------------------------------------
-- Dragging
------------------------------------------------------------
button:SetScript("OnDragStart", function(self)
    self:SetScript("OnUpdate", function()
        local angle = GetAngleFromCursor()
        UpdatePosition(angle)
        YippYappHelperDB.minimapAngle = angle
    end)
end)

button:SetScript("OnDragStop", function(self)
    self:SetScript("OnUpdate", nil)
end)

------------------------------------------------------------
-- Click handling
------------------------------------------------------------
button:SetScript("OnClick", function(self, btn)
    if btn == "LeftButton" then
        if ns.ToggleDashboard then
            ns:ToggleDashboard()
        end
    elseif btn == "RightButton" then
        if ns.MainFrame then
            if ns.MainFrame:IsShown() then
                ns.MainFrame:Hide()
            else
                ns:RefreshAllSlots()
                ns:RefreshCrests()
                ns:AnchorFrame()
                ns.MainFrame:Show()
            end
        end
    end
end)

------------------------------------------------------------
-- Tooltip
------------------------------------------------------------
button:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_LEFT")
    GameTooltip:SetText("|cff00ff00YippYapp Helper|r")
    GameTooltip:AddLine("Left-click: Dashboard", 1, 1, 1)
    GameTooltip:AddLine("Right-click: Gear Upgrades", 1, 1, 1)
    GameTooltip:AddLine("Drag to reposition", 0.5, 0.5, 0.5)
    GameTooltip:Show()
end)

button:SetScript("OnLeave", GameTooltip_Hide)

------------------------------------------------------------
-- Init: load saved position
------------------------------------------------------------
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
initFrame:SetScript("OnEvent", function(self)
    self:UnregisterAllEvents()
    YippYappHelperDB = YippYappHelperDB or {}
    local angle = YippYappHelperDB.minimapAngle or DEFAULT_ANGLE
    UpdatePosition(angle)

    if YippYappHelperDB.hideMinimapButton then
        button:Hide()
    end
end)
