/**  @author Gilles Coomans <gilles.coomans@gmail.com> */

var utils = require('./utils'),
	PureNode = require('./pure-node'),
	Emitter = require('./emitter');

/**
 * Container Container
 */
function Container(parent) {
	PureNode.call(this);
	this.__yContainer__ = true;
	this.childNodes = [];
	if (parent)
		this.parent = parent;
};

Container.prototype  = {
	/**
	 * mount container in selector
	 * @param  {[type]} selector [description]
	 * @param  {[type]} mode     could be : null, appendTo, insertBefore
	 * @param  {[type]} querier  [description]
	 * @return {[type]}          [description]
	 */
	mount: function(selector, mode, querier) {
		if (this.destroyed)
			throw new Error('yamvish container has been destroyed. could not mount anymore.');
		if (selector && (selector === this.mountPoint || selector === this.mountSelector))
			return this;
		var node = selector;
		if (typeof node === 'string') {
			this.mountSelector = selector;
			node = (querier || utils.domQuery)(selector);
		}
		if (!node)
			throw new Error('yamvish : mount point not found : ' + selector);

		if (mode === 'insertBefore') {
			this.mountPoint = node.parentNode;
			if (!this.mountPoint)
				throw new Error('container mount fail : no parent found for insertBefore');
			this.mountSelector = null;
			utils.mountChildren(this, this.mountPoint, node);
		} else {
			this.mountPoint = node;
			// console.log('Container.mount : ', this, selector);
			if (!mode && node.childNodes && node.childNodes.length) // mount as innerHTML : empty node before appending
				utils.emptyNode(node);

			utils.mountChildren(this, node);
		}

		return this.emit('mounted', this);
	},
	mountBefore: function(nextSiblingSelector, querier) {
		return this.mount(nextSiblingSelector, 'insertBefore', querier);
	},
	appendTo: function(selector, querier) {
		return this.mount(selector, 'append', querier);
	},
	unmount: function() {
		if (!this.mountPoint)
			return this;
		for (var i = 0; i < this.childNodes.length; i++)
			this.mountPoint.removeChild(this.childNodes[i]);
		this.mountPoint = null;
		this.mountSelector = null;
		return this.emit('unmounted', this);
	},
	destroy: function() {
		// console.log('Container destroy :', this);
		if (this.destroyed)
			return this;
		this.emit('destroy', this);
		this.destroyed = true;
		if (this.childNodes)
			for (var i = 0; i < this.childNodes.length; i++)
				utils.destroyElement(this.childNodes[i], true);
		this.childNodes = null;
		this.context = null;
		this.mountPoint = null;
		this.mountSelector = null;
		if (this._route) {
			if (this._route.unbind)
				this._route.unbind();
			this._route = null;
		}
	},
	hide: function() {
		if (this.destroyed)
			return this;
		this.childNodes.forEach(function(child) {
			if (!child.style)
				child.style = {};
			child.style.display = 'none';
		});
	},
	show: function() {
		if (this.destroyed)
			return this;
		this.childNodes.forEach(function(child) {
			if (child.style)
				child.style.display = '';
		});
	}
};

utils.shallowMerge(PureNode.prototype, Container.prototype);
utils.shallowMerge(Emitter.prototype, Container.prototype);

Container.prototype.appendChild = function(child, nextSibling) {
	PureNode.prototype.appendChild.call(this, child);
	if (this.mountPoint) {
		nextSibling = nextSibling || utils.findNextSibling(this);
		if (child.__yPureNode__ && !child.__yVirtual__)
			utils.mountChildren(child, this.mountPoint, nextSibling);
		else if (nextSibling)
			this.mountPoint.insertBefore(child, nextSibling);
		else
			this.mountPoint.appendChild(child);
	}
	return child;
};
Container.prototype.removeChild = function(child) {
	if (!this.childNodes)
		return false;
	PureNode.prototype.removeChild.call(this, child);
	if (this.mountPoint)
		utils.removeChild(this.mountPoint, child);
	return child;
};
Container.prototype.insertBefore = function(child, ref) {
	if (!this.childNodes)
		return false;
	PureNode.prototype.insertBefore.call(this, child, ref);
	if (this.mountPoint)
		utils.insertBefore(this.mountPoint, child, ref);
	return child;
};

module.exports = Container;

@@ -509,52 +509,8 @@
 ore%0A
-%09 * @param  %7B%5Btype%5D%7D querier  %5Bdescription%5D%0A
 %09 * 
@@ -585,25 +585,16 @@
 or, mode
-, querier
 ) %7B%0A%09%09if
@@ -902,20 +902,8 @@
 de =
- (querier %7C%7C
  uti
@@ -913,17 +913,16 @@
 domQuery
-)
 (selecto
@@ -1625,33 +1625,24 @@
 lingSelector
-, querier
 ) %7B%0A%09%09return
@@ -1688,25 +1688,16 @@
 tBefore'
-, querier
 );%0A%09%7D,%0A%09
@@ -1723,25 +1723,16 @@
 selector
-, querier
 ) %7B%0A%09%09re
@@ -1769,17 +1769,8 @@
 end'
-, querier
 );%0A%09

remove querier in container.mount