local revision = tonumber(string.sub("$Revision: 22871 $", 12, -3))
if revision > Cartographer.revision then
	Cartographer.version = "r" .. revision
	Cartographer.revision = revision
	Cartographer.date = string.sub("$Date: 2006-12-31 13:39:11 -0500 (Sun, 31 Dec 2006) $", 8, 17)
end

local L = AceLibrary("AceLocale-2.2"):new("Cartographer-Battlegrounds")

L:RegisterTranslations("enUS", function() return {
	["Battlegrounds"] = true,
	["Module which provides maps of battlegrounds."] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Battlegrounds"] = "Schlachtfelder",
	["Module which provides maps of battlegrounds."] = "Modul, das Schlachtfeldkarten zur Verfügung stellt.",
} end)

L:RegisterTranslations("frFR", function() return {
	["Battlegrounds"] = "Champs de bataille",
	["Module which provides maps of battlegrounds."] = "Module fournissant des cartes pour les champs de bataille.",
} end)

L:RegisterTranslations("koKR", function() return {
	["Battlegrounds"] = "전장",
	["Module which provides maps of battlegrounds."] = "전장 지도 모듈을 제공합니다.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Battlegrounds"] = "戰場",
	["Module which provides maps of battlegrounds."] = "提供戰場地圖的模組。",
} end)

L:RegisterTranslations("zhCN", function() return {
--	["Battlegrounds"] = true,
--	["Module which provides maps of battlegrounds."] = true,
} end)

L:RegisterTranslations("esES", function() return {
	["Battlegrounds"] = "Campos de Batalla",
	["Module which provides maps of battlegrounds."] = "M\195\179dulo que provee mapas de los campos de batalla",
} end)

Cartographer_Battlegrounds = Cartographer:NewModule("Battlegrounds", "AceHook-2.1", "AceEvent-2.0")

local BZ = AceLibrary("Babble-Zone-2.2")
local Tourist = AceLibrary("Tourist-2.0")

function Cartographer_Battlegrounds:OnInitialize()
	self.name = L["Battlegrounds"]
	self.title = L["Battlegrounds"]
	Cartographer.options.args.Battlegrounds = {
		name = L["Battlegrounds"],
		desc = L["Module which provides maps of battlegrounds."],
		type = 'group',
		args = {
			toggle = {
				name = AceLibrary("AceLocale-2.2"):new("Cartographer")["Enabled"],
				desc = AceLibrary("AceLocale-2.2"):new("Cartographer")["Suspend/resume this module."],
				type = "toggle",
				order = -1,
				get = function() return Cartographer:IsModuleActive(self) end,
				set = function() Cartographer:ToggleModuleActive(self) end
			},
		},
		handler = self,
	}
	
	local validate = {
		["Warsong Gulch"] = BZ["Warsong Gulch"],
		["Arathi Basin"] = BZ["Arathi Basin"],
		["Alterac Valley"] = BZ["Alterac Valley"],
		["Eye of the Storm"] = BZ["Eye of the Storm"],
	}
	
	Cartographer.gotoOptions.args.Battlegrounds = {
		name = L["Battlegrounds"],
		desc = L["Battlegrounds"],
		type = 'text',
		get = function()
			return Cartographer:GetCurrentEnglishZoneName()
		end,
		set = "ShowBattleground",
		validate = validate,
		handler = self,
		hidden = function() return not Cartographer:IsModuleActive(self) end
	}
end

function Cartographer_Battlegrounds:OnEnable()
	self:Hook("SetMapZoom", true)
	self:Hook("SetMapToCurrentZone", true)
	self:Hook("GetMapInfo", true)
	self:RegisterEvent("Cartographer_ChangeZone")
	self:Hook("WorldMapZoomOutButton_OnClick", true)
end

local currentBG = nil
function Cartographer_Battlegrounds:ShowBattleground(name)
	if currentBG == name then
		return
	end
	local current = GetRealZoneText()
	current = BZ:HasReverseTranslation(current) and BZ:GetReverseTranslation(current)
	if current == name then
		currentBG = nil
		local map = self.hooks.GetMapInfo()
		if name == "Warsong Gulch" then
			if map == "WarsongGulch" then
				return
			end
		elseif name == "Arathi Basin" then
			if map == "ArathiBasin" then
				return
			end
		elseif name == "Alterac Valley" then
			if map == "AlteracValley" then
				return
			end
		elseif name == "Eye of the Storm" then
			if map == "NetherstormArena" then
				return
			end
		end
		self.hooks.SetMapToCurrentZone()
		return
	end
	local realName = name
	if name == "Warsong Gulch" then
		name = "WarsongGulch"
	elseif name == "Arathi Basin" then
		name = "ArathiBasin"
	elseif name == "Alterac Valley" then
		name = "AlteracValley"
	elseif name == "Eye of the Storm" then
		name = "NetherstormArena"
	else
		error(string.format("Cannot show unknown battleground %q", name), 2)
	end
	SetMapZoom(-1, nil)
	currentBG = name
	WorldMapFrame_Update()
	WorldMapZoomOutButton:Enable()
	self:TriggerEvent("Cartographer_ChangeZone", realName, BZ[realName])
	currentBG = name
end

function Cartographer_Battlegrounds:Cartographer_ChangeZone(zone)
	currentBG = nil
end

function Cartographer_Battlegrounds:SetMapZoom(a, b)
	currentBG = nil
	return self.hooks.SetMapZoom(a, b)
end

function Cartographer_Battlegrounds:SetMapToCurrentZone()
	currentBG = nil
	return self.hooks.SetMapToCurrentZone()
end

function Cartographer_Battlegrounds:GetMapInfo()
	if currentBG then
		return currentBG
	end
	return self.hooks.GetMapInfo()
end

function Cartographer_Battlegrounds:WorldMapZoomOutButton_OnClick()
	if not currentBG then
		return self.hooks.WorldMapZoomOutButton_OnClick()
	end
	currentBG = nil
	self.hooks.SetMapZoom(0)
end