--[[
============================================================================
Enhanced Mount Tracker  -  UI/MinimapButton.lua
============================================================================
Minimap button. Left-click opens the browser; drag to reposition. Tooltip
shows current collection progress. Position persists in EMT_Settings.
============================================================================
]]--

local ADDON_NAME, NS = ...
local EMT = NS.EMT
if not EMT then return end

EMT.Minimap = EMT.Minimap or {}
local MMB = EMT.Minimap

local RADIUS = 80

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

    local f = CreateFrame("Button", "EMTMinimapButton", 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\\Ability_Mount_RidingHorse")
    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 Mount Tracker", 1, 1, 1)
        local owned, total = EMT.MountAPI:GetProgress()
        local pct = total > 0 and (owned / total * 100) or 0
        GameTooltip:AddLine(string.format("Tracked: %d / %d  (%.0f%%)",
            owned, total, pct), 0, 1, 0)
        GameTooltip:AddLine(string.format("Total mounts owned: %d",
            EMT.MountAPI:GetOwnedCount()), 0.8, 0.8, 0.8)
        GameTooltip:AddLine(" ")
        GameTooltip:AddLine("Left-click: open Mount 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() EMT.Browser:Toggle() end)

    -- Drag-to-reposition around the minimap edge.
    local function onUpdateDrag()
        local mx, my = Minimap:GetCenter()
        local px, py = GetCursorPosition()
        local scale  = Minimap:GetEffectiveScale()
        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)
    EMT_Settings = EMT_Settings or {}
    EMT_Settings.minimapAngle = angle
end

function MMB:Show()
    if not self.frame then self:Create() end
    self.frame:Show()
    EMT_Settings = EMT_Settings or {}
    EMT_Settings.minimapShown = true
end

function MMB:Hide()
    if self.frame then self.frame:Hide() end
    EMT_Settings = EMT_Settings or {}
    EMT_Settings.minimapShown = 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 Mounts minimap button is
-- no longer created at login. Create/Show/Toggle remain above so the optional
-- "/emt minimap" toggle can still summon it on demand.
