var modules = {
	"render": {
		"source": "./modules/render.js",
		"dependsOn": []
	},
	"input": {
		"source": "./modules/input.js",
		"dependsOn": ["render"]
	},
	"resources": {
		"source": "./modules/resources.js",
		"dependsOn": []
	},
	"world": {
		"source": "./modules/world.js",
		"dependsOn": []
	},
	"network": {
		"source": "./modules/network.js",
		"dependsOn": []
	}
};

var plugins = {
	"splash": {
		"type": "plugin",
		"source": "./plugins/splash.js",
		"loadAs": "splash",
		"dependsOn": ["render"]
	}
};

var loadings = {};

class GameCore extends global.utils.EventListener {
	constructor () {
		super();

		this.modules = {};
		this.plugins = {};
	}
	init (meta) {
		return new Promise((function (resolve, reject) {
			for (var data in meta) {
				if (meta.hasOwnProperty(data)) {
					var metadata = meta[data];

					if (loadings[data]) continue;

					loadings[data] = this.loadModule(data);
				}
			}

			Promise.all(Object.values(loadings)).then((function () {
				this.emit("pre-init", {});

				resolve(this);
			}).bind(this)).catch(reject);
		}).bind(this));
	}
	loadModule (moduleName) {
		return load("./modules/" + moduleName + ".js").then((function (mod) {
			this.modules[moduleName] = mod;
		}).bind(this)).catch(throwError);
	}
	loadPlugin (pluginName) {
		return load("./plugins/" + pluginName + ".js").then((function (plugin) {
			this.plugins[pluginName] = plugin;
		}).bind(this)).catch(throwError);
	}
}

module.exports = {
	"GameCore": GameCore,
	"loadData": {
		"modules": modules,
		"plugins": plugins
	}
};
