var parser = require('xml2json');
var fs = require('fs');
var pd = require('pretty-data').pd;
var log = require('./logger');

object2xml = function(data) {
	return pd.xml(parser.toXml(data));
}
exports.object2xml = object2xml;


exports.readObject = function(path, file, callback) {
	fs.readFile(path + file, function(err, data){
		if(err) {
			log.error("Could not open file: " + err);
			callback(err, undefined);
		}
		// Send a method response with a value
		var json = parser.toJson(data.toString(), {reversible: true});
		callback(err, JSON.parse(json));
	});
}

exports.writeObject = function(path, file, data, callback) {

	fs.writeFile(path + file, object2xml(data), function(err){
		callback(err);
	});
}

exports.applyChanges = function (manifest, request) {
	var projects = manifest.manifest.project;

	// Modify
	for (i in request.changes) {
		change = request.changes[i];
		switch (change.action) {
			case "Add":
				// TODO: error checking, does it already exist?
				project = {};
				project.name = change.name;
				project.path = change.path;
				project.groups = change.groups;
				project.revision = change.revision;
				project.remote = change.remote;
				projects.push(project);
			break;

			case "Remove":
				// TODO: Error checking, is it really removing anything?
				projects = projects.filter(function(v) { return v.name == change.name ? false : true;});
				manifest.manifest.project = projects;
			break;

			case "Modify":
				for (j in projects) {
					project = projects[j];
					if (change.name == project.name) {
						project.path = change.path;
						project.groups = change.groups;
						project.revision = change.revision;
						project.remote = change.remote;
					}
				}
			break;

			default:
				log.error("Unknown change action: " + change.action);
				return false;
			break;
		}

	}

	return true;

}

@@ -145,15 +145,660 @@
 ion(
-data) %7B
+manifestObject) %7B%0A%09var project;%0A%09var external = %7B%7D;%0A%09external.project = %5B%5D;%0A%0A%09var length = manifestObject.manifest.project.length;%0A%09for (i = 0; i %3C length; i++) %7B%0A%09%09project = manifestObject.manifest.project%5Bi%5D;%0A%09%09if (project.external) %7B%0A%09%09%09delete project.external;%0A%09%09%09delete project.path;%0A%09%09%09external.project.push(project);%0A%09%09%09manifestObject.manifest.project%5Bi%5D = undefined;%0A%09%09%7D%0A%09%7D%0A%0A%09if (external.project.length %3E 0) %7B%0A%09%09manifestObject.manifest.external = external;%0A%0A%09%09// Cleanup main project array%0A%09%09for (i = length; i %3E 0; i--) %7B%0A%09%09%09if (manifestObject.manifest.project%5Bi%5D == undefined) %7B%0A%09%09%09%09manifestObject.manifest.project.splice(i, 1);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A
 %0A%09re
@@ -822,20 +822,30 @@
 r.toXml(
-data
+manifestObject
 ));%0A%7D%0Aex
@@ -1180,38 +1180,369 @@
 ;%0A%09%09
-callback(err, JSON.parse(json)
+var manifestObject = JSON.parse(json);%0A%0A%09%09if (manifestObject.manifest.external) %7B%0A%09%09%09while (manifestObject.manifest.external.project.length %3E 0) %7B%0A%09%09%09%09var project = manifestObject.manifest.external.project.shift();%0A%09%09%09%09project.external = true;%0A%09%09%09%09project.path = 'N/A';%0A%09%09%09%09manifestObject.manifest.project.push(project);%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09callback(err, manifestObject
 );%0A%09

Add support for external projects