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

local bs = AceLibrary("Babble-Spell-2.2")
local L = AceLibrary("AceLocale-2.2"):new("Cartographer_Professions")
L:RegisterTranslations("enUS", function() return {
	["Professions"] = true,
	["Always"] = true,
	["With Profession"] = true,
	["Never"] = true,
	["Stub for loading Cartographer module addons based on your professions."] = true,
	["Unable to load addon `%s': %s"] = true,
	["Load all professions"] = true,
	["This option will load all the profession modules including those you currently dont know."] = true,
} end)

L:RegisterTranslations("koKR", function() return {
	["Professions"] = "전문 기술",
 	["Always"] = "항상",
	["With Profession"] = "해당 기술",
	["Never"] = "안함",
	["Stub for loading Cartographer module addons based on your professions."] = "당신의 직업에 기초를 둔 Cartographer 모듈 애드온을 불려옵니다.",
	["Unable to load addon `%s': %s"] = "%s 애드온 로드 실패: %s",
	["Load all professions"] = "모든 직업",
	["This option will load all the profession modules including those you currently dont know."] = "이 옵션은 전문 기술을 배우지 않은 상태에도 모든 직업이 사용할 수 있는 모듈입니다..",
} end)

L:RegisterTranslations("deDE", function() return {
	["Professions"] = "Berufe",
	["Always"] = "Immer",
	["With Profession"] = "Mit Berufen",
	["Never"] = "Nie",
	["Stub for loading Cartographer module addons based on your professions."] = "Platzhalter für das Laden von Cartographer-Modulen basierend auf Deinen Berufen.",
	["Unable to load addon `%s': %s"] = "Addon konnte nicht geladen werden: `%s': %s",
	["Load all professions"] = "Lade alle Berufe",
	["This option will load all the profession modules including those you currently dont know."] = "Diese Option wird alle Berufe Module laden inklusive dehrer die du gegenwärtig nicht hast.",
} end)

L:RegisterTranslations("frFR", function() return {
	["Professions"] = "Métiers",
	["Always"] = "Toujours",
	["With Profession"] = "Avec Métier",
	["Never"] = "Jamais",
	["Stub for loading Cartographer module addons based on your professions."] = "Charge des modules pour Cartographer basés sur vos métiers.",
	["Unable to load addon `%s': %s"] = "Impossible de charger l'addon '%s' : %s",
	["Load all professions"] = "Charger tous les métiers",
	["This option will load all the profession modules including those you currently dont know."] = "Cette option chargera tous les modules de métiers, même ceux que vous ne pratiquez pas.",
	} end)

L:RegisterTranslations("esES", function() return {
	["Professions"] = "Profesiones",
	["Always"] = "Siempre",
	["With Profession"] = "Con Profesi\195\179n",
	["Never"] = "Nunca",
	["Stub for loading Cartographer module addons based on your professions."] = "Carga los m\195\179dulos del accesorio Cartographer basados en tus profesiones",
	["Unable to load addon `%s': %s"] = "No se ha podido cargar el accesorio `%s': %s",
	["Load all professions"] = "Cargar todas las profesiones",
	["This option will load all the profession modules including those you currently dont know."] = "Esta opci\195\179n cargar todos los m\195\179dulos de profesi\195\179n incluyendo aquellos que no conoces actualmente",
} end)

L:RegisterTranslations("zhTW", function() return {
	["Professions"] = "專業技能",

	["Stub for loading Cartographer module addons based on your professions."] = "依據你的專業技能載入Cartographer模組。",
	["Unable to load addon `%s': %s"] = "無法載入插件`%s': %s",
} end)

local mod = Cartographer:NewModule('Professions', 'AceEvent-2.0', 'AceConsole-2.0')

function mod:OnInitialize()
	self.name    = L["Professions"]
	self.title   = L["Professions"]
	self.author  = 'Chris "kergoth" Larson'
	self.notes   = L["Stub for loading Cartographer module addons based on your professions."]
	self.email   = 'clarson@kergoth.com'
	self.website = nil
	self.version = nil
	-- adjustment for load all option
    self.db = Cartographer:AcquireDBNamespace("Professions")
	Cartographer:RegisterDefaults("Professions", "profile", {
		profs = {},
	})

	local opts = {
		name = L["Professions"],
		desc = L["Stub for loading Cartographer module addons based on your professions."],
		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,
	}
	self:RegisterChatCommand({'/cartprof'}, opts)
	for n=1,GetNumAddOns() do
		local pipometa = GetAddOnMetadata(n, 'X-Cartographer-Prof')
		if pipometa and bs:HasTranslation(pipometa) then
			local profLocale = bs:GetTranslation(pipometa)
			if not self.db.profile.profs[profLocale] then
				self.db.profile.profs[profLocale] = "With Profession"
			end
			opts.args[profLocale] = {
				name = profLocale,
				desc = profLocale,
				type = 'text',
				get = function() 
					return self:LoadProfPref(profLocale) 
				end,
				set = function(v) 
					self:SetProfLoad(profLocale,v) 
				end,
				disabled = function() return not Cartographer:IsModuleActive(self) end,
				validate = { ["Always"] = L["Always"], ["With Profession"] = L["With Profession"], ["Never"] = L["Never"] },
			}
		end
	end
	Cartographer.options.args.Professions = opts
	self.addons = {}
end

function mod:OnEnable()
	self:RegisterEvent('SKILL_LINES_CHANGED')
	self:SKILL_LINES_CHANGED()
end

function mod:OnDisable()
	self:UnregisterAllEvents()
	for k,v in pairs(self.addons) do
		Cartographer:ToggleModuleActive(v,false)
	end
end

function mod:SetProfLoad(prof,v)
	self.db.profile.profs[prof] = v
	self:SKILL_LINES_CHANGED()
end

function mod:LoadProfPref(v)
	if not self.db.profile.profs[v] then
		self.db.profile.profs[v] = "With Profession"
	end
	return self.db.profile.profs[v]
end

function mod:BootProfession(n,name)
	local name, title, notes, enabled, loadable, reason, security = GetAddOnInfo(n)
	if not IsAddOnLoaded(n) then
		if enabled and loadable then
			local loaded, reason = LoadAddOn(n)
			if not loaded then
				self:Print(L["Unable to load addon `%s': %s"], name, reason)
			end
		end
	end
end
function mod:SKILL_LINES_CHANGED()
	local skills = {}
	for i=1,GetNumSkillLines() do
		local skillname, isHeader = GetSkillLineInfo(i)
		if not isHeader then
			skills[skillname] = true
		end
	end

	for n=1,GetNumAddOns() do
		local pipometa = GetAddOnMetadata(n, 'X-Cartographer-Prof')
		if pipometa then
			if bs:HasTranslation(pipometa) then
				local localProf = bs:GetTranslation(pipometa)
				if self.db.profile.profs[localProf] == "Always" then
					self:BootProfession(n)
				elseif self.db.profile.profs[localProf] == "With Profession" then
					for skillname in pairs(skills) do
						--if bs:HasReverseTranslation(skillname) and pipometa == bs:GetReverseTranslation(skillname) then
						if localProf == skillname then
							self:BootProfession(n)
						end
					end
				end
			end
		end
	end

	for k,v in pairs(self.addons) do
		if self.db.profile.profs[k] == "Always" then
			Cartographer:ToggleModuleActive(v,true)
		elseif self.db.profile.profs[k] == "With Profession" and skills[k] then
			Cartographer:ToggleModuleActive(v,true)
		else
			Cartographer:ToggleModuleActive(v,false)
		end
	end
end