RegisterNPC = {};
local RegisterNPC_mt = Class(RegisterNPC, AbstractManager);

function RegisterNPC.new()	
	local self = AbstractManager.new(RegisterNPC_mt);
	self.drawDebug = false;
	
	return self;
end;

function RegisterNPC:initDataStructures()
	self.loadedNpcs = false;
	self.npcs = {};
	self.nameToNPC = {};
	self.nameToIndex = {};
	self.npcsSubData = {};
	self.npcsSubDataToIndex = {};
end;

function RegisterNPC:loadMapData(xmlFile, missionInfo)
	RegisterNPC:superClass().loadMapData(self);	
	self:load();
	return true;
end;

--function RegisterNPC:unloadMapData()	
--	RegisterNPC:superClass().unloadMapData(self);
--end;

function RegisterNPC:load()	
	if self.loadedNpcs then return;end;
	local xmlFilename = Utils.getFilename("config/npcs.xml", AdditionalContracts.modDir);
	
	if xmlFilename == nil then
		Logging.xmlWarning(AdditionalContracts.modDir.. "config/npcs.xml", "File could not be opened");		
		return;
	end;
	local npcXMLFile = XMLFile.load("npcsXML", xmlFilename, NPCManager.xmlSchema);
		
	if not npcXMLFile then
		Logging.xmlWarning(xmlFilename, "File could not be opened");		
		return;
	end;
	
	for _, key in npcXMLFile:iterator("map.npcs.npc") do		
		self:loadNPC(npcXMLFile, key, _, AdditionalContracts.modDir);		
	end;
	self:loadSubData(xmlFilename);
	if npcXMLFile ~= nil then npcXMLFile:delete();npcXMLFile = nil;end;
	self.loadedNpcs = true;
	self:setOwnValues();
end;

function RegisterNPC:loadNPC(xmlFile, key, _, modDirectory)
	local xmlFilename = Utils.getFilename(xmlFile:getValue(key), modDirectory);
	if xmlFilename == nil then
		Logging.xmlWarning(xmlFile, "Missing xmlFilename for npc \'%s\'", key);
		return;
	else
		local name = xmlFile:getValue(key .. "#name");
		if name == nil then
			Logging.xmlWarning(xmlFile, "Missing name for npc \'%s\'", key);
			return;
		else			
			local upperName = string.upper(name);
			if self.nameToNPC[upperName] == nil then
				local npc, isDLCMissing = NPCUtil.createFromXML(xmlFilename);
				if not isDLCMissing then
					if npc ~= nil then
						table.insert(self.npcs, npc);
						self.nameToNPC[upperName] = npc;
						self.nameToIndex[upperName] = #self.npcs;
						npc.name = upperName;						
						npc.index = #self.npcs;
						--npc:register(true); --not relevant					
						--g_currentMission.onCreateObjectSystem:add(npc); --conversation - only mapLoaded function
						return true;
					end;
					Logging.xmlWarning(xmlFile, "Could not create NPC from file \'%s\' for \'%s\'!", xmlFilename, key);
				end;
			else
				Logging.xmlWarning(xmlFile, "NPC with name \'%s\' already exists for \'%s\'!", name, key);
				return;
			end;
		end;
	end;
end;

function RegisterNPC:loadSubData(xmlFilename)
	local xmlFile = loadXMLFile("npcsXMLXml", xmlFilename);

	if not xmlFile then
		Logging.xmlWarning(xmlFilename, "File could not be opened");		
		return nil;
	end;

	local i = 0;

	while true do
		local key = string.format("map.npcs.npcData(%d)", i);

		if not hasXMLProperty(xmlFile, key) then
			break;
		end;
		
		local npcSubData = {
			title = getXMLString(xmlFile, key.. "#title");
			typ = Utils.getNoNil(getXMLString(xmlFile, key.. "#typ"), "ALL"):upper();
			imageFilename = getXMLString(xmlFile, key.. "#imageFilename");			
			id = i + 1;
		};		
		table.insert(self.npcsSubData, npcSubData);
		self.npcsSubDataToIndex[npcSubData.typ] = #self.npcsSubData;
		i = i + 1;
	end;
	delete(xmlFile);
end;

function RegisterNPC:setOwnValues()
		
end;

function RegisterNPC:getNPCSubData(name)
	if name == nil then return nil;end;
	name = name:upper();
	return self.npcsSubData[self.npcsSubDataToIndex[name]];
end;

function RegisterNPC:getNPCByName(name, subDataTyp)
	if name == nil then	return nil;end;
	name = name:upper();	
	return self.nameToNPC[name], self:getNPCSubData(subDataTyp);
end;
g_additionalContractNpcManager = RegisterNPC.new();