//Tracks actions assigned to planets being colonized
#include "/include/cqueue_saveload.as"

dictionary setGovernorMap;
dictionary loadQueueMap;

void onPlanetTaken(const Object@ obj) {
	Planet@ pl = getObjectByID(obj.uid).toPlanet();
	string@ key = i_to_s(obj.uid);
	string value;

	//Check setting governor
	if(setGovernorMap.get(key, value)) {
		pl.setUseGovernor(true);
		pl.setGovernorType(value);
		setGovernorMap.delete(key);
		return;
	}

	//Check loading queue
	if(loadQueueMap.get(key, value)) {
		loadQueue(pl, value);
		loadQueueMap.delete(key);
		return;
	}
}

string@ getAction(Planet@ pl) {
	string@ key = i_to_s(pl.toObject().uid);
	string value;

	//Check setting governor
	if(setGovernorMap.get(key, value)) {
		return localize("#RM_SetGovernor")+": "+localize("#PG_"+value);
	}

	//Check loading queue
	if(loadQueueMap.get(key, value)) {
		return localize("#RM_LoadQueue")+": "+value;
	}

	return null;
}

void setPlannedGovernor(Planet@ pl, string gov) {
	string@ key = i_to_s(pl.toObject().uid);
	loadQueueMap.delete(key);
	setGovernorMap.set(key, gov);
}

string@ getPlannedGovernor(Planet@ pl) {
	string@ key = i_to_s(pl.toObject().uid);
	string value;

	//Check setting governor
	if(setGovernorMap.get(key, value))
		return value;
	return null;
}

void setPlannedQueue(Planet@ pl, string queue) {
	string@ key = i_to_s(pl.toObject().uid);
	setGovernorMap.delete(key);
	loadQueueMap.set(key, queue);
}

string@ getPlannedQueue(Planet@ pl) {
	string@ key = i_to_s(pl.toObject().uid);
	string value;

	//Check setting governor
	if(loadQueueMap.get(key, value))
		return value;
	return null;
}

void onSaveFileWrite(XMLWriter@ xml) {
	xml.createHeader();
	string name, value;

	setGovernorMap.resetIter();
	do {
		setGovernorMap.getCurrentName(name);
		setGovernorMap.getCurrent(value);
		xml.addElement("g", true, "i", name, "v", value);
	} while(setGovernorMap.advance());

	loadQueueMap.resetIter();
	do {
		loadQueueMap.getCurrentName(name);
		loadQueueMap.getCurrent(value);
		xml.addElement("q", true, "i", name, "v", value);
	} while(loadQueueMap.advance());
}

void onSaveFileLoad(XMLReader@ xml) {
	string name, value;

	while(xml.advance()) {
		if(xml.getNodeType() != XN_Element)
			continue;

		name = xml.getAttributeValue("i");
		value = xml.getAttributeValue("v");

		if(xml.getNodeName() == "g") {
			setGovernorMap.set(name, value);
		}
		else if(xml.getNodeName() == "q") {
			loadQueueMap.set(name, value);
		}
	}
}
