--[[
============================================================================
Enhanced Pet Tracker  -  Core/Waypoint.lua
============================================================================
TomTom integration. TomTom is an optional dependency — declared under
## OptionalDeps in the TOC — so this module always checks for it at runtime
and degrades gracefully (prints the coordinates to chat) when it is absent.

TomTom's public API:
  TomTom:AddWaypoint(uiMapID, x, y, opts)
where x and y are 0-1 fractions. Our data stores them as 0-100 percentages
(that is the form ATT and Rarity use), so we divide by 100 on the way in.
TomTom then owns the map pin, the minimap pin, and the on-screen arrow.

Because TomTom takes the uiMapID directly, no zone-name table is needed —
TomTom resolves the map itself. (That is why coordinates are cheap to ship
for waypoints even though a human-readable zone label would not be.)
============================================================================
]]--

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

EPT.Waypoint = EPT.Waypoint or {}
local WP = EPT.Waypoint

WP._last = nil    -- last waypoint UID we created (so we can clear it)

-- Is TomTom installed and loaded?
function WP:HasTomTom()
    return _G.TomTom ~= nil and type(_G.TomTom.AddWaypoint) == "function"
end

-- rec is a PetData record; needs cm (mapID), cx, cy (0-100 percentages).
function WP:HasLocation(rec)
    return rec and rec.cm and rec.cx and rec.cy
end

-- Drop a waypoint for a pet. Returns true if a TomTom waypoint was created,
-- false if it only printed coordinates (no TomTom).
function WP:SetForPet(speciesID, rec, label)
    if not self:HasLocation(rec) then
        EPT:Print("No map location is recorded for this pet.")
        return false
    end
    label = label or ("Pet #" .. tostring(speciesID))

    if self:HasTomTom() then
        -- clear our previous pin so waypoints don't pile up
        if self._last and TomTom.RemoveWaypoint then
            pcall(TomTom.RemoveWaypoint, TomTom, self._last)
        end
        local uid = TomTom:AddWaypoint(rec.cm, rec.cx / 100, rec.cy / 100, {
            title       = label,
            from        = "Enhanced Pet Tracker",
            persistent  = false,
            minimap     = true,
            world       = true,
            crazy       = true,   -- the on-screen "crazy taxi" arrow
        })
        self._last = uid
        EPT:Printf("Waypoint set for |cffffff00%s|r (%.0f, %.0f).",
            label, rec.cx, rec.cy)
        return true
    end

    -- Graceful fallback: no TomTom — just report the coordinates.
    EPT:Printf("|cffffff00%s|r is at map %d (%.0f, %.0f). "
        .. "Install TomTom for an on-screen arrow.",
        label, rec.cm, rec.cx, rec.cy)
    return false
end

function WP:ClearLast()
    if self._last and self:HasTomTom() and TomTom.RemoveWaypoint then
        pcall(TomTom.RemoveWaypoint, TomTom, self._last)
        self._last = nil
    end
end
