
/**
 * The main base class. Manages the signal system. Objects based on this class
 * can be instantiated declaratively using XML.
 */
module.exports = new Class
({
	/**
	 * Tag to be used when the class instance is defined via XML.
	 */
	Tag: 'vn-object'

	/**
	 * Class public properties.
	 */
	,Properties: {}

	/*
	 * Reference count.
	 */
	,_refCount: 1

	/*
	 * Signal handlers data.
	 */
	,_signalData: null

	/**
	 * Initializes the object and sets all properties passed to the class
	 * constructor.
	 * 
	 * @param {Object} props The properties passed to the contructor
	 */
	,initialize: function (props)
	{
		this.setProperties (props);
	}
	
	/**
	 * Sets a group of object properties.
	 * 
	 * @param {Object} props Properties
	 */
	,setProperties: function (props)
	{
		for (var prop in props)
			this[prop] = props[prop];
	}
	
	/**
	 * Increases the object reference count.
	 */
	,ref: function ()
	{
		this._refCount++;
		return this;
	}
	
	/**
	 * Decreases the object reference count.
	 */
	,unref: function ()
	{
		this._refCount--;
		
		if (this._refCount === 0)
			this._destroy ();
	}

	/**
	 * Called from @Vn.Builder when it finds a custom tag as a child of the
	 * element.
	 * 
	 * @param {Vn.Scope} scope The scope instance
	 * @param {Node} node The custom tag child nodes
	 */
	,loadXml: function () {}

	/**
	 * Called from @Vn.Builder when it finds a a child tag that isn't
	 * associated to any property.
	 * 
	 * @param {Object} child The child object instance
	 */
	,appendChild: function () {}

	/**
	 * Conects a signal with a function.
	 *
	 * @param {string} id The signal identifier
	 * @param {function} callback The callback
	 * @param {Object} instance The instance
	 */
	,on: function (id, callback, instance)
	{
		if (!(callback instanceof Function))
		{
			console.warn ('Vn.Object: Invalid callback for signal \'%s\'', id);
			return;
		}
		
		this._signalInit ();
		var callbacks = this._signalData.signals[id];

		if (!callbacks)
			callbacks = this._signalData.signals[id] = [];
			
		callbacks.push ({
			 blocked: false
			,callback: callback
			,instance: instance
		});
	}

	/**
	 * Locks/Unlocks a signal emission to the specified object.
	 *
	 * @param {string} id The signal identifier
	 * @param {function} callback The callback
	 * @param {boolean} block %true for lock the signal, %false for unlock
	 */
	,blockSignal: function (id, callback, block, instance)
	{
		if (!this._signalData)
			return;

		var callbacks = this._signalData.signals[id];
		
		if (!callbacks)
			return;
			
		for (var i = 0; i < callbacks.length; i++)
		if (callbacks[i].callback == callback
		&& callbacks[i].instance == instance)
			callbacks[i].blocked = block;
	}
	
	/**
	 * Emits a signal in the object.
	 *
	 * @param {string} id The signal identifier
	 */
	,emit: function (id)
	{
		if (!this._signalData)
			return;

		var callbacks = this._signalData.signals[id];
		
		if (!callbacks)
			return;

		var callbackArgs = [];
		callbackArgs.push (this);
	
		for (var i = 1; i < arguments.length; i++)
			callbackArgs.push (arguments[i]);

		for (var i = 0; i < callbacks.length; i++)
		if (!callbacks[i].blocked)
			callbacks[i].callback.apply (callbacks[i].instance, callbackArgs);
	}
	
	/**
	 * Disconnects a signal from current object.
	 *
	 * @param {string} id The signal identifier
	 * @param {function} callback The connected callback
	 * @param {Object} instance The instance
	 */
	,disconnect: function (id, callback, instance)
	{
		if (!this._signalData)
			return;

		var callbacks = this._signalData.signals[id];
		
		if (callbacks)
		for (var i = callbacks.length; i--;)
		if (callbacks[i].callback === callback
		&& callbacks[i].instance === instance)
			callbacks.splice (i, 1);
	}
	
	/**
	 * Disconnects all signals for the given instance.
	 *
	 * @param {Object} instance The instance
	 */
	,disconnectByInstance: function (instance)
	{
		if (!this._signalData)
			return;
	
		var signals = this._signalData.signals;

		for (var signalId in signals)
		{
			var callbacks = signals[signalId];

			if (callbacks)
			for (var i = callbacks.length; i--;)
			if (callbacks[i].instance === instance)
				callbacks.splice (i, 1);
		}
	}

	/**
	 * Destroys the object, this method should only be called before losing
	 * the last reference to the object. It can be overwritten by child classes
	 * but should always call the parent method.
	 */
	,_destroy: function ()
	{
		if (!this._signalData)
			return;
	
		var links = this._signalData.links;
	
		for (var key in links)
			this._unlink (links[key]);
		
		this._signalData = null;
	}
	
	/**
	 * Links the object with another object.
	 * 
	 * @param {Object} prop The linked property
	 * @param {Object} handlers The object events to listen with
	 */
	,link: function (prop, handlers)
	{
		this._signalInit ();
		var links = this._signalData.links;
	
		for (var key in prop)
		{
			var newObject = prop[key];
			var oldObject = this[key];
		
			if (oldObject)
				this._unlink (oldObject);

			this[key] = newObject;

			if (newObject)
			{
				links[key] = newObject.ref ();

				for (var signal in handlers)
					newObject.on (signal, handlers[signal], this);
			}
			else if (oldObject)
				links[key] = undefined;
		}
	}

	,_unlink: function (object)
	{
		object.disconnectByInstance (this);
		object.unref ();
	}
	
	,_signalInit: function ()
	{
		if (!this._signalData)
			this._signalData = {
				signals: {},
				links: {}
			};
	}
});

