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

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

L:RegisterTranslations("enUS", function() return {
	["Group Colors"] = true,
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = true,
} end)

L:RegisterTranslations("deDE", function() return {
	["Group Colors"] = "Gruppenfarben",
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = "Modul, das Spieler Deiner Gruppe oder Deines Raids als farbigen Punkt in ihrer jeweiligen Klassenfarbe und mit Raid-Gruppennummer auf der Weltkarte anzeigt.",
} end)

L:RegisterTranslations("frFR", function() return {
	["Group Colors"] = "Couleurs de groupe",
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = "Module transformant les points de la carte de votre groupe/raid en des cercles colorés selon la classe et indiquant le numéro de groupe.",
} end)

L:RegisterTranslations("koKR", function() return {
	["Group Colors"] = "그룹 색상",
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = "모든 당신의 파티 및 공격대원들의 위치를 직업에 따른 서클과 숫자를 통해 보여줍니다.",
} end)

L:RegisterTranslations("zhCN", function() return {
--	["Group Colors"] = true,
--	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = true,
} end)

L:RegisterTranslations("zhTW", function() return {
	["Group Colors"] = "小隊顏色",
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = "在地圖上將小隊和團隊隊友圓點依職業著色，並顯示號碼表示他們所在隊伍的模組。",
} end)

L:RegisterTranslations("esES", function() return {
	["Group Colors"] = "Colores de Grupo",
	["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."] = "M\195\179dulo que convierte todos los PdIs de tu grupo y banda en c\195\173rculos coloreados basados en clase, y muestra un n\195\186mero sobre ellos basado en su grupo de banda",
} end)

Cartographer_GroupColors = Cartographer:NewModule("GroupColors", "AceEvent-2.0")

local _G = getfenv(0)

function Cartographer_GroupColors:OnInitialize()
	self.name = L["Group Colors"]
	self.title = L["Group Colors"]
	Cartographer.options.args.GroupColors = {
		name = L["Group Colors"],
		desc = L["Module which turns all your party's and your raid's POIs into circles colored based on class, and shows a number on them based on their raid group."],
		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,
	}
	
end

function Cartographer_GroupColors:OnEnable()
	self:RegisterEvent("PARTY_MEMBERS_UPDATE", "Update")
	self:RegisterEvent("RAID_ROSTER_UPDATE", "Update")
	self:RegisterEvent("WORLD_MAP_UPDATE", function()
		if GetNumRaidMembers() > 0 then
			self:ScheduleRepeatingEvent("Cartographer_GroupColors", self.Update, 0.5, self)
		else
			self:Update()
		end
	end)
	self:RegisterEvent("ADDON_LOADED", function(addon)
		if addon == "Blizzard_BattlefieldMinimap" then
			self:Update()
		end
	end)
	self:Update()
	for i = 1, 4 do
		_G["WorldMapParty" .. i .. "Icon"]:SetTexture("Interface\\AddOns\\Cartographer\\GroupColorsArtwork\\Normal")
	end
end

function Cartographer_GroupColors:OnDisable()
	for i = 1, 4 do
		_G["WorldMapParty" .. i .. "Icon"]:SetVertexColor(1, 1, 1)
		_G["WorldMapParty" .. i .. "Icon"]:SetTexture("Interface\\WorldMap\\WorldMapPartyIcon")
	end
	for i = 1, 40 do
		_G["WorldMapRaid" .. i .. "Icon"]:SetVertexColor(1, 1, 1)
		_G["WorldMapRaid" .. i .. "Icon"]:SetTexture("Interface\\WorldMap\\WorldMapPartyIcon")
	end
end

function Cartographer_GroupColors:Update()
	local worldMapShown = WorldMapFrame:IsShown()
	local battlefieldMinimapShown = BattlefieldMinimap and BattlefieldMinimap:IsVisible()
	if not worldMapShown and not battlefieldMinimapShown then
		self:CancelScheduledEvent("Cartographer_GroupColors")
		return
	end
	
	if GetNumRaidMembers() > 0 then
		local flash = GetTime() % 1 < 0.5
		
		for i = 1, GetNumRaidMembers() do
			local wmUnit = _G["WorldMapRaid" .. i].unit
			local bmUnit 
			if battlefieldMinimapShown then
				bmUnit = _G["BattlefieldMinimapRaid" .. i].unit
			end
			if not wmUnit and not bmUnit then
				break
			end
			if worldMapShown and wmUnit then
				local _, _, subgroup, _, _, fileName = GetRaidRosterInfo(string.sub(wmUnit, 5)+0)
				if subgroup and fileName then
					local tex = _G["WorldMapRaid" .. i .. "Icon"]
					tex:SetTexture("Interface\\AddOns\\Cartographer\\GroupColorsArtwork\\Group" .. subgroup)
					local t = RAID_CLASS_COLORS[fileName]
					if flash and UnitAffectingCombat("raid"..i) then
						tex:SetVertexColor(0.8, 0, 0) -- flash red
					elseif flash and UnitIsDeadOrGhost("raid"..i) then
						tex:SetVertexColor(0.2, 0.2, 0.2) -- flash gray
					elseif t then
						tex:SetVertexColor(t.r, t.g, t.b)
					else
						tex:SetVertexColor(0.8, 0.8, 0.8)
					end
				end
			end
			if battlefieldMinimapShown and bmUnit then
				local _, _, subgroup, _, _, fileName = GetRaidRosterInfo(string.sub(bmUnit, 5)+0)
				if subgroup and fileName then
					local tex = _G["BattlefieldMinimapRaid" .. i .. "Icon"]
					tex:SetTexture("Interface\\AddOns\\Cartographer\\GroupColorsArtwork\\Group" .. subgroup)
					local t = RAID_CLASS_COLORS[fileName]
					if flash and UnitAffectingCombat("raid"..i) then
						tex:SetVertexColor(0.8, 0, 0) -- flash red
					elseif flash and UnitIsDeadOrGhost("raid"..i) then
						tex:SetVertexColor(0.2, 0.2, 0.2) -- flash gray
					elseif t then
						tex:SetVertexColor(t.r, t.g, t.b)
					else
						tex:SetVertexColor(0.8, 0.8, 0.8)
					end
				end
			end
		end
	else
		for i = 1, GetNumPartyMembers() do
			local _,fileName = UnitClass("party" .. i)
			local tex = _G["WorldMapParty" .. i .. "Icon"]
			local t = RAID_CLASS_COLORS[fileName]
			tex:SetVertexColor(t.r, t.g, t.b)
		end
	end
end