--[[
============================================================================
Enhanced Rare Tracker  -  UI/MinimapButton.lua
============================================================================
Minimap button. Left-click opens the rare tracker; drag to reposition around
the minimap edge. Tooltip shows roster size and how many rares are currently
being tracked. Position + shown-state persist in ETB_RareDB.minimap. Mirrors
the EMT/EPT minimap-button pattern so it feels native to the suite.
============================================================================
]]--

local ADDON_NAME, NS = ...
local ETB = NS.ETB
local ERT = ETB and ETB.ERT
if not ERT then return end

ERT.Minimap = ERT.Minimap or {}
local MMB = ERT.Minimap
local RADIUS = 80

local function settings()
    ETB_RareDB = ETB_RareDB or {}
    ETB_RareDB.minimap = ETB_RareDB.minimap or { angle = 215, shown = true }
    return ETB_RareDB.minimap
end

function MMB:Create()
    if self.frame then return self.frame end
    if not Minimap then return nil end

    local f = CreateFrame("Button", "ERTMinimapButton", Minimap)
    f:SetSize(31, 31)
    f:SetFrameStrata("MEDIUM")
    f:SetFrameLevel(8)
    f:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")

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

    local icon = f:CreateTexture(nil, "BACKGROUND")
    icon:SetSize(20, 20)
    icon:SetPoint("CENTER", 0, 0)
    icon:SetTexture("Interface\\Icons\\INV_Misc_SpyGlass_03")   -- spyglass = scouting/tracking
    icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
    f.icon = icon

    f:SetScript("OnEnter", function(self)
        GameTooltip:SetOwner(self, "ANCHOR_LEFT")
        GameTooltip:SetText("Enhanced Rare Tracker", 1, 1, 1)
        local total = 0
        if ERT.RareRoster then for _ in pairs(ERT.RareRoster) do total = total + 1 end end
        local tracked = (ERT.GetTrackedNPCs and #ERT:GetTrackedNPCs()) or 0
        GameTooltip:AddLine(string.format("Rares in roster: %d", total), 0.8, 0.8, 0.8)
        GameTooltip:AddLine(string.format("Currently tracking: %d", tracked), 0, 1, 0)
        GameTooltip:AddLine(" ")
        GameTooltip:AddLine("Left-click: open Rare Tracker", 0.7, 0.7, 0.7)
        GameTooltip:AddLine("Drag: move button", 0.5, 0.5, 0.5)
        GameTooltip:Show()
    end)
    f:SetScript("OnLeave", function() GameTooltip:Hide() end)
    f:SetScript("OnClick", function() if ERT.ToggleTracker then ERT:ToggleTracker() end end)

    -- Drag-to-reposition around the minimap edge.
    local function onUpdateDrag()
        local mx, my = Minimap:GetCenter()
        local px, py = GetCursorPosition()
        local scale  = Minimap:GetEffectiveScale()
        if not (mx and px and scale and scale > 0) then return end
        px, py = px / scale, py / scale
        MMB:SetPosition(math.deg(math.atan2(py - my, px - mx)))
    end
    f:RegisterForDrag("LeftButton")
    f:SetScript("OnDragStart", function(self) self:SetScript("OnUpdate", onUpdateDrag) end)
    f:SetScript("OnDragStop",  function(self) self:SetScript("OnUpdate", nil) end)
    f:RegisterForClicks("LeftButtonUp")

    self.frame = f
    return f
end

function MMB:SetPosition(angle)
    if not self.frame then return end
    local rads = math.rad(angle)
    self.frame:ClearAllPoints()
    self.frame:SetPoint("CENTER", Minimap, "CENTER",
        math.cos(rads) * RADIUS, math.sin(rads) * RADIUS)
    settings().angle = angle
end

function MMB:Show()
    if not self.frame then self:Create() end
    if self.frame then self.frame:Show() end
    settings().shown = true
end

function MMB:Hide()
    if self.frame then self.frame:Hide() end
    settings().shown = false
end

function MMB:Toggle()
    if self.frame and self.frame:IsShown() then self:Hide() else self:Show() end
end

-- v15.111.0: The Central Plaza (the main ETB minimap button) is now the single
-- entry point for the whole suite, so this standalone Rare Tracker minimap
-- button is no longer created at login. Create/Show/Toggle remain above so the
-- optional "/etbrare minimap" toggle can still summon it on demand.
