define(["dojo","dojox/json/ref","dojox/json/schema","dojox/application/kernel","text!dojox/application/schema/application.json"],function(dojo,ref,schema, kernel,appSchema){
	return function(config){
		if (!config){
			throw Error("App Config Missing");
		}

		console.log("before resolve: ", appSchema);

		if (appSchema){
			var as = dojox.json.ref.resolveJson(appSchema);
		}

		console.log("as: ", as);	
		//TODO validate schema here
		//if (schema.validate(config,appSchema)){
			console.log("config.modules: ", config.modules);
			return define(config.modules, function(){
				var modules=[kernel];
				for(var i=0;i<arguments.length;i++){
					modules.push(arguments[i]);
				}
	
				return new (dojo.declare(modules))(dojox.json.ref.resolveJson(config));
			});
		//}else{
		//	console.error("Failed to process application config");
		//}
	};
});

