/*

The MIT License

Copyright (c) 2009 Prolific Interactive

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

var Prolific = window.Prolific = function (P) {

	//Class constructor
	function Class (classSpecs) {
		//The returned class wraps around the init() method specified in the class specs
		var self = function (instanceSpecs) {
				var specs = augment(instanceSpecs || {}, classSpecs.defaults || {}),
					inherited = classSpecs.Parent? beget(classSpecs.Parent(specs)): {},
					obj = beget(inherited, classSpecs.init(specs, classSpecs)),
					m,
					ifcTest;
					
				//Mixins
				for (m in classSpecs.mixins) {
					augment(obj, classSpecs.mixins[m]);
				}
				
				if (instanceSpecs.__ifcTest !== true && classSpecs.onInit) {
					classSpecs.onInit.call(obj, instanceSpecs, classSpecs);
				}
				
				return obj;
			};
			
		//Inherit static members of the parent class.
		self.prototype = classSpecs.Parent;
		
		//Test for full interface implementation. For debugging purposes, it helps to provide a name property in the classSpecs.
		if (classSpecs.Interface && (ifcTest =  classSpecs.Interface.test(self)) !== true) {
			throw [ 'Class ', (classSpecs.name || 'Undetermined'), ' does not fully implement ', classSpecs.Interface.getName(), '. Missing members: ', ifcTest.join(', ') ].join('');
		}
		
		//Override parent's static members
		override(self, classSpecs.Static? classSpecs.Static(classSpecs): {});
		
		//Augment with some standard static methods, return.
		return augment(self, {
			getSpecs: function () {
				return classSpecs;
			}
		});
		
	}
	
	//Interface constructor
	function Interface (name, membs) {
		return {
			test: function (myClass) {
				var membArr = membs.split(','),
					myDummy = new myClass({ __ifcTest: true }),
					m,
					result = true,
					nonList = [];
				for (m in membArr) {
					if (!myDummy[membArr[m]]) {
						nonList.push(membArr[m]);
						result = false;
					}
				}
				return result || nonList;
			},
			getClasses: function () {
				return classArr;
			},
			getName: function () {
				return name;
			}
		};
	}
	
	//Module creates an instance of a singleton
	function Module (singleton) {
		return singleton.call(this);
	}
	
	//Privileged methods
	
	function addClass (name, specs) {
		this[name] = new Class( override(specs, { name: name }) );
		return this;
	}
	
	function addInterface (name, membs) {
		this[name] = new Interface(name, membs);
		return this;
	}
	
	function method (name, func) {
		this[name] = func;
		return this;
	}
		
	function beget (o, membs) {
		var F = function () {}, self;
		F.prototype = o;
		self = new F();
		return membs? Prolific.augment(self, membs): self;
	}
	
	function augment (obj, membs) {
		var m;
		for (m in membs) {
			if (!obj.hasOwnProperty(m)) {
				obj[m] = membs[m];
			}
		}
		return obj;
	}
	
	function augmentThis (membs) {
		return augment(this, membs);
	}
	
	function override (obj, membs) {
		var m;
		for (m in membs) {
			obj[m] = membs[m];
		}
		return obj;
	}
	
	function overrideThis (membs) {
		return override(this, membs);
	}

	//Returns the whole Prolific namespace object
	return {
		//An app provides a customized, closed environment to develop within
		app: function (name, closure) {
			var myApp = this[name] = {
				//A feature creates a subset of the app
				feature: function (name, builder) {
					var myFeature = this[name] = {
						//Feature tasks should be delegated to modules
						module: function (name, singleton) {							
							this[name] = new Module(singleton);
							return this;
						},
						addClass: addClass,
						addInterface: addInterface,
						method: method,
						augment: augmentThis,
						override: overrideThis
					};
					builder.call(this, myFeature);
					return this;
				},
				addClass: addClass,
				addInterface: addInterface,
				method: method,
				augment: augmentThis,
				override: overrideThis
			};
			closure.call(this, myApp);
			return this;
		},
		beget: beget,
		augment: augment,
		override: override,
		method: method
	};
}(Prolific);