require('./style.scss');
var Component = require('vn/component');

module.exports = new Class({
	Extends: Component
	,Tag: 'htk-loader'
	,Properties: {
		form: {
			type: Db.Form
			,set(x) {
				this.link({_form: x}, {'status-changed': this.onFormChange});
				this.onFormChange();
			}
			,get() {
				return this._form;
			}
		}
	}

	,initialize(props) {
		Component.prototype.initialize.call(this, props);
		this.createRoot('div');

		var div = this.createElement('div');
		div.className = 'spinner';

		var spinner = new Htk.Spinner();
		div.appendChild(spinner.node);

		var childs = this.createElement('div');

		this.spinner = spinner;
		this.div = div;
		this.childs = childs;
		this.isLoading = true;
		this.stop();
	}

	,appendChild(child) {
		this.childs.appendChild(child);
	}

	,stop() {
		if (!this.isLoading)
			return;

		this.isLoading = false;
		this.spinner.stop();
		Vn.Node.removeChilds(this.node);
		this.node.appendChild(this.childs);
	}

	,start() {
		if (this.isLoading)
			return;

		this.isLoading = true;
		this.spinner.start();
		Vn.Node.removeChilds(this.node);
		this.node.appendChild(this.div);
	}

	,onFormChange() {
		if (this._form.ready)
			this.stop();
		else
			this.start();
	}
});

