local revision = tonumber(string.sub("$Revision: 23887 $", 12, -3))
if revision > Cartographer.revision then
	Cartographer.version = "r" .. revision
	Cartographer.revision = revision
	Cartographer.date = string.sub("$Date: 2007-01-07 09:37:49 -0500 (Sun, 07 Jan 2007) $", 8, 17)
end

local Tourist = AceLibrary("Tourist-2.0")

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

L:RegisterTranslations("enUS", function() return {
	["Coordinates"] = true,
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = true,
	
	["Cursor"] = true,
	["Player"] = true,
	
	["Background color"] = true,
	["The color of the background frame behind coordinates"] = true,
	
	["Text color"] = true,
	["The color of the coordinates"] = true,
	
	["Text size"] = true,
	["The size of the coordinates"] = true,
	
	["Show background"] = true,
	["Show background frame behind coordinates"] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Coordinates"] = "Koordinaten",
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = "Modul zur Anzeige der Spieler- und Mauszeiger-Koordinaten im unteren Bereich der Weltkarte.",
	
	["Cursor"] = "Mauszeiger",
	["Player"] = "Spieler",
	
	["Background color"] = "Hintergrundfarbe",
	["The color of the background frame behind coordinates"] = "Die Farbe des Hintergrundes hinter den Koordinaten.",
	
	["Text color"] = "Textfarbe",
	["The color of the coordinates"] = "Die Farbe der Koordinatenanzeige.",
	
	["Text size"] = "Schriftgröße",
	["The size of the coordinates"] = "Die Schriftgröße der Koordinatenanzeige.",
	
	["Show background"] = "Zeige Hintergrund",
	["Show background frame behind coordinates"] = "Zeige Hintergrundfenster hinter den Koordinaten.",
} end)


L:RegisterTranslations("frFR", function() return {
	["Coordinates"] = "Coordonnées",
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = "Modules indiquant les coordonnées du joueur et du curseur en bas de la carte du monde.",

	["Cursor"] = "Curseur ",
	["Player"] = "Joueur ",

	["Background color"] = "Couleur de l'arrière-plan",
	["The color of the background frame behind coordinates"] = "Détermine la couleur de l'arrière-plan des coordonnées.",

	["Text color"] = "Couleur du texte",
	["The color of the coordinates"] = "Détermine la couleur des coordonnées.",

	["Text size"] = "Taille du texte",
	["The size of the coordinates"] = "Détermine la taille des coordonnées.",

	["Show background"] = "Afficher l'arrière-plan",
	["Show background frame behind coordinates"] = "Affiche ou non un arrière-plan derrière les coordonnées.",
} end)

L:RegisterTranslations("koKR", function() return {
	["Coordinates"] = "좌표",
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = "플레이어와 커서의 좌표를 세계 지도 밑에 보여줍니다.",
	
	["Cursor"] = "커서",
	["Player"] = "플레이어",
	
	["Background color"] = "배경 색상",
	["The color of the background frame behind coordinates"] = "좌표 뒤의 배경 프레임의 색상을 변경합니다.",
	
	["Text color"] = "텍스트 색상",
	["The color of the coordinates"] = "좌표의 색상을 변경합니다.",
	
	["Text size"] = "텍스트 크기",
	["The size of the coordinates"] = "좌표의 크기를 변경합니다.",
	
	["Show background"] = "배경 표시",
	["Show background frame behind coordinates"] = "좌표 뒤의 배경 프레임을 표시합니다.",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Coordinates"] = "座標",
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = "在世界地圖底端增加游標和玩家目前座標的模組。",
	
	["Cursor"] = "游標",
	["Player"] = "玩家",
	
	["Background color"] = "背景顏色",
	["The color of the background frame behind coordinates"] = "座標背景視窗的顏色",
	
	["Text color"] = "文字顏色",
	["The color of the coordinates"] = "座標的顏色",
	
	["Text size"] = "文字大小",
	["The size of the coordinates"] = "座標的大小",
	
	["Show background"] = "顯示背景",
	["Show background frame behind coordinates"] = "顯示座標的背景視窗",
} end)

L:RegisterTranslations("esES", function() return {
	["Coordinates"] = "Coordenadas",
	["Module to add coordinates to the bottom of the world map of the player and the cursor."] = "M\195\179dulo para a\195\177adir coordenadas del jugador y cursor en la parte inferior del mapa del mundo",
	
	["Cursor"] = "Cursor",
	["Player"] = "Jugador",
	
	["Background color"] = "Color de fondo",
	["The color of the background frame behind coordinates"] = "El color del fondo del marco tras las coordenadas",
	
	["Text color"] = "Color del texto",
	["The color of the coordinates"] = "El color de las coordenadas",
	
	["Text size"] = "Tama\195\177o del texto",
	["The size of the coordinates"] = "El tama\195\177o de las coordenadas",
	
	["Show background"] = "Mostrar fondo",
	["Show background frame behind coordinates"] = "Muestra el marco de fondo tras las coordenadas",
} end)

Cartographer_Coordinates = Cartographer:NewModule("Coordinates")

function Cartographer_Coordinates:OnInitialize()
	self.db = Cartographer:AcquireDBNamespace("Coordinates")
	Cartographer:RegisterDefaults("Coordinates", "profile", {
		background = false,
		backgroundR = 0,
		backgroundG = 0,
		backgroundB = 0,
		backgroundA = 1,
		textR = 1,
		textG = 0.82,
		textB = 0,
		textA = 1,
		textSize = 1,
	})

	Cartographer.options.args.Coordinates = {
		name = L["Coordinates"],
		desc = L["Module to add coordinates to the bottom of the world map of the player and the cursor."],
		type = 'group',
		args = {
			bgColor = {
				name = L["Background color"],
				desc = L["The color of the background frame behind coordinates"],
				type = "color",
				get = "GetBackgroundColor",
				set = "SetBackgroundColor",
				hasAlpha = true,
				disabled = "~IsShowingBackground"
			},
			color = {
				name = L["Text color"],
				desc = L["The color of the coordinates"],
				type = "color",
				get = "GetTextColor",
				set = "SetTextColor",
				hasAlpha = true,
			},
			size = {
				name = L["Text size"],
				desc = L["The size of the coordinates"],
				type = "range",
				get = "GetTextSize",
				set = "SetTextSize",
				min = 0.5,
				max = 1.5,
				isPercent = true,
				step = 0.05
			},
			background = {
				name = L["Show background"],
				desc = L["Show background frame behind coordinates"],
				type = "toggle",
				get = "IsShowingBackground",
				set = "ToggleShowingBackground",
			},
			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,
	}
	
end

function Cartographer_Coordinates:OnEnable()
	if not self.frame then
		self.frame = CreateFrame("Frame", "CartographerCoordinates", WorldMapFrame)
		self.frame:SetScript("OnUpdate", self.OnUpdate)
		
		self.frame.cursorCoords = self.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		self.frame.cursorCoords:SetPoint("RIGHT", WorldMapFrame, "CENTER", -80, -367)
		self.frame.cursorCoords:SetTextColor(self:GetTextColor())
		local font,_,style = self.frame.cursorCoords:GetFont()
		self.frame.cursorCoords:SetFont(font,self:GetTextSize()*12,style)
		
		self.frame.playerCoords = self.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		self.frame.playerCoords:SetPoint("LEFT", WorldMapFrame, "CENTER", 80, -367)
		self.frame.playerCoords:SetTextColor(self:GetTextColor())
		local font,_,style = self.frame.playerCoords:GetFont()
		self.frame.playerCoords:SetFont(font,self:GetTextSize()*12,style)
		
		self.frame.yardsAway = self.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		self.frame.yardsAway:SetPoint("CENTER", WorldMapFrame, "CENTER", 0, -367)
		self.frame.yardsAway:SetTextColor(self:GetTextColor())
		local font,_,style = self.frame.yardsAway:GetFont()
		self.frame.yardsAway:SetFont(font,self:GetTextSize()*12,style)
		
		self.background1 = CreateFrame("Frame", "CartographerCoordinatesBackground1", WorldMapFrame)
		self.background1:SetPoint("TOPLEFT", self.frame.cursorCoords, "TOPLEFT", -3, 3)
		self.background1:SetPoint("BOTTOMRIGHT", self.frame.cursorCoords, "BOTTOMRIGHT", 3, -3)
		local tex = self.background1:CreateTexture()
		self.background1.texture = tex
		tex:SetAllPoints(self.background1)
		tex:SetTexture(self:GetBackgroundColor())
		
		self.background2 = CreateFrame("Frame", "CartographerCoordinatesBackground2", WorldMapFrame)
		self.background2:SetPoint("TOPLEFT", self.frame.playerCoords, "TOPLEFT", -3, 3)
		self.background2:SetPoint("BOTTOMRIGHT", self.frame.playerCoords, "BOTTOMRIGHT", 3, -3)
		local tex = self.background2:CreateTexture()
		self.background2.texture = tex
		tex:SetAllPoints(self.background2)
		tex:SetTexture(self:GetBackgroundColor())
		
		self.background3 = CreateFrame("Frame", "CartographerCoordinatesBackground3", WorldMapFrame)
		self.background3:SetPoint("TOPLEFT", self.frame.yardsAway, "TOPLEFT", -3, 3)
		self.background3:SetPoint("BOTTOMRIGHT", self.frame.yardsAway, "BOTTOMRIGHT", 3, -3)
		local tex = self.background3:CreateTexture()
		self.background3.texture = tex
		tex:SetAllPoints(self.background3)
		tex:SetTexture(self:GetBackgroundColor())
	end
	if not self.db.profile.background then
		self.background1:Hide()
		self.background2:Hide()
		self.background3:Hide()
	else
		self.background1:Show()
		self.background2:Show()
		self.background3:Show()
	end
	self.frame:Show()
	self.frame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
	self.frame:RegisterEvent("MINIMAP_ZONE_CHANGED")
	self.frame:SetScript("OnEvent", function()
		self:OnEvent()
	end)
	self:OnEvent()
end

function Cartographer_Coordinates:OnDisable()
	self.frame:Hide()
	self.background1:Hide()
	self.background2:Hide()
end

local currentYardWidth, currentYardHeight
local function yardify(x,y)
	if x then
		return x * currentYardWidth, y * currentYardHeight
	end
end

local metric = not LOCALE_enUS -- get Brits too
function Cartographer_Coordinates.OnUpdate()
	local self = Cartographer_Coordinates
	local x, y = GetCursorPosition()
	
	local px, py = GetPlayerMapPosition("player")
	local left, top = WorldMapDetailFrame:GetLeft(), WorldMapDetailFrame:GetTop()
	local width = WorldMapDetailFrame:GetWidth()
	local height = WorldMapDetailFrame:GetHeight()
	local scale = WorldMapDetailFrame:GetEffectiveScale()
	local cx = (x/scale - left) / width
	local cy = (top - y/scale) / height
	
	self.frame.cursorCoords:SetText(string.format("%s: %.1f, %.1f", L["Cursor"], 100 * cx, 100 * cy))
	if px == 0 or py == 0 or IsInInstance() or Cartographer:GetCurrentInstance() then
		self.frame.playerCoords:SetText("")
		self.frame.yardsAway:SetText("")
	else
		self.frame.playerCoords:SetText(string.format("%s: %.1f, %.1f", L["Player"], 100 * px, 100 * py))
		if not currentYardWidth or GetRealZoneText() ~= Cartographer:GetCurrentLocalizedZoneName() then
			self.frame.yardsAway:SetText("")
		else
			local px_y, py_y = yardify(px, py)
			local cx_y, cy_y = yardify(cx, cy)
			local dx_y, dy_y = px_y - cx_y, py_y - cy_y
			local d_y = (dx_y*dx_y + dy_y*dy_y)^0.5
			if metric then
				self.frame.yardsAway:SetText(string.format("%.0f m", d_y*0.9144))
			else
				self.frame.yardsAway:SetText(string.format("%.0f yd", d_y))
			end
		end
	end
end

function Cartographer_Coordinates:IsShowingBackground()
	return self.db.profile.background
end

function Cartographer_Coordinates:ToggleShowingBackground(value)
	if value == nil then
		value = not self.db.profile.background
	end
	self.db.profile.background = value
	
	if not value then
		self.background1:Hide()
		self.background2:Hide()
	else
		self.background1:Show()
		self.background2:Show()
	end
end

function Cartographer_Coordinates:GetBackgroundColor()
	return self.db.profile.backgroundR, self.db.profile.backgroundG, self.db.profile.backgroundB, self.db.profile.backgroundA
end

function Cartographer_Coordinates:SetBackgroundColor(r, g, b, a)
	self.db.profile.backgroundR, self.db.profile.backgroundG, self.db.profile.backgroundB, self.db.profile.backgroundA = r, g, b, a
	
	self.background1.texture:SetTexture(r, g, b, a)
	self.background2.texture:SetTexture(r, g, b, a)
end

function Cartographer_Coordinates:GetTextColor()
	return self.db.profile.textR, self.db.profile.textG, self.db.profile.textB, self.db.profile.textA
end

function Cartographer_Coordinates:SetTextColor(r, g, b, a)
	self.db.profile.textR, self.db.profile.textG, self.db.profile.textB, self.db.profile.textA = r, g, b, a
	
	self.frame.cursorCoords:SetTextColor(r, g, b, a)
	self.frame.playerCoords:SetTextColor(r, g, b, a)
end

function Cartographer_Coordinates:GetTextSize()
	return self.db.profile.textSize
end

function Cartographer_Coordinates:SetTextSize(value)
	self.db.profile.textSize = value
	
	local font,_,style = self.frame.cursorCoords:GetFont()
	self.frame.cursorCoords:SetFont(font,value*12,style)
	local font,_,style = self.frame.playerCoords:GetFont()
	self.frame.playerCoords:
	SetFont(font,value*12,style)
end

function Cartographer_Coordinates:OnEvent()
	currentYardWidth, currentYardHeight = Tourist:GetZoneYardSize(GetRealZoneText())
end