-- maputils.lua
-- Map utilities for Retail UiMapIDs with match-level logic

local ADDON_NAME, WDIGI = ...
WDIGI.MapUtils = WDIGI.MapUtils or {}

------------------------------------------------------------
-- GetCurrentPlayerData
-- Returns: mapID, playerX, playerY
------------------------------------------------------------
function WDIGI.MapUtils.GetCurrentPlayerData()
    local mapID = C_Map.GetBestMapForUnit("player")
    if not mapID then
        return nil, nil, nil
    end

    local pos = C_Map.GetPlayerMapPosition(mapID, "player")
    if not pos then
        return mapID, nil, nil
    end

    return mapID, pos.x, pos.y
end

------------------------------------------------------------
-- IsSameZone (strict)
-- Only returns true for exact zone or direct parent/child
------------------------------------------------------------
function WDIGI.MapUtils.IsSameZone(a, b)
    if not a or not b or a <= 0 or b <= 0 then
        return false
    end

    if a == b then
        return true
    end

    local aInfo = C_Map.GetMapInfo(a)
    local bInfo = C_Map.GetMapInfo(b)
    if not aInfo or not bInfo then
        return false
    end

    -- Same direct parent
    if aInfo.parentMapID ~= 0 and aInfo.parentMapID == bInfo.parentMapID then
        return true
    end

    -- Same grandparent
    local aParent = aInfo.parentMapID and C_Map.GetMapInfo(aInfo.parentMapID)
    local bParent = bInfo.parentMapID and C_Map.GetMapInfo(bInfo.parentMapID)

    if aParent and bParent and aParent.parentMapID ~= 0 and aParent.parentMapID == bParent.parentMapID then
        return true
    end

    return false
end

------------------------------------------------------------
-- GetZoneMatchLevel
-- 0 = same zone
-- 1 = same parent zone
-- 2 = same continent
-- 3 = everything else
------------------------------------------------------------
function WDIGI.MapUtils.GetZoneMatchLevel(playerMapID, vendorMapID)
    if not playerMapID or not vendorMapID then
        return 3
    end

    if playerMapID == vendorMapID then
        return 0
    end

    local p = C_Map.GetMapInfo(playerMapID)
    local v = C_Map.GetMapInfo(vendorMapID)
    if not p or not v then
        return 3
    end

    -- Same parent zone
    if p.parentMapID ~= 0 and p.parentMapID == v.parentMapID then
        return 1
    end

    -- Same continent (grandparent)
    local pParent = p.parentMapID and C_Map.GetMapInfo(p.parentMapID)
    local vParent = v.parentMapID and C_Map.GetMapInfo(v.parentMapID)

    if pParent and vParent and pParent.parentMapID ~= 0 and pParent.parentMapID == vParent.parentMapID then
        return 2
    end

    return 3
end

------------------------------------------------------------
-- GetZoneMultiplier
-- Scaling factor for distance calculations
------------------------------------------------------------
function WDIGI.MapUtils.GetZoneMultiplier()
    -- Get current map to determine scale
    local mapID = C_Map.GetBestMapForUnit("player")
    if not mapID then return 1000 end

    local mapInfo = C_Map.GetMapInfo(mapID)
    local multiplier = 1000.0 -- Base Default

    -- 1. Scale by Map Type
    if mapInfo then
        -- Enum.UIMapType.City is usually 3
        if mapInfo.mapType == 3 then
            multiplier = 1625.0 -- Calibrated for Stormwind/Orgrimmar scaling
        elseif mapInfo.mapType == 4 then -- Dungeon
            multiplier = 500.0 -- Dungeons are much smaller grids
        elseif mapInfo.mapType == 2 then -- Zone (Outdoor)
            multiplier = 1000.0
        end
    end

    -- 2. Specific Map Overrides
    -- If we are in Stormwind City specifically (Map 84)
    if mapID == 84 then
        multiplier = 1650.0
    elseif mapID == 1453 then -- Orgrimmar
        multiplier = 1500
    end

    return multiplier
end
