-- Copyright (C) 2010, 2011 Tyson Brown
-- 
-- This file is part QuestHelper2, an AddOn for World of Warcraft.
-- 
-- QuestHelper2 is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- 
-- QuestHelper2 is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with QuestHelper2. If not, see <http://www.gnu.org/licenses/>.

if LibDebug then LibDebug() end
local shared = select(2, ...)

-- This file provides functions for determining the names of Quests, NPCs, Items, and Areas from their IDs.

local area_name_cache = {}

function shared.areaName(area_id, level)
  local level_names = area_name_cache[area_id]
  
  if not level_names then
    level_names = {}
    area_name_cache[area_id] = level_names
  end
  
  level = level or 0
  
  local name = level_names[level]
  
  if name then
    return name
  end
  
  shared.pushMap()
  SetMapByID(area_id)
  
  if GetCurrentMapAreaID() == area_id then
    local zone = GetCurrentMapZone()
    
    name = _G["DUNGEON_FLOOR_"..(GetMapInfo() or ""):upper()..(level-(DungeonUsesTerrainMap() and 1 or 0))]
    
    if not name and zone > 0 then
      name = select(GetCurrentMapZone(), GetMapZones(GetCurrentMapContinent()))
      
      if name and level > 0 then
        name = name .. " " .. shared.L.AREA_LEVEL_INDEX(level)
      end
    end
    
    if not name then
      -- This isn't localized, but it's probably better than a number.
      name = GetMapInfo()
    end
  end
  
  if not name then
    name = "Area #"..area_id..(level ~= 0 and " Level #"..level or "")
  end
  
  shared.popMap()
  
  level_names[level] = name
  
  return name
end
