var Step = require ('./step');

module.exports = new Class
({
	Extends: Vn.Component
	,Tag: 'htk-assistant'
	,Properties:
	{
		stepCount:
		{
			type: Number
			,set: function (x)
			{
				this._stepCount = x;
				
				if (x > 0)
					this.setStep (0);
				else
					this.setStep (-1);
			}
			,get: function ()
			{
				return this._stepCount;
			}
		},
		step:
		{
			type: Number
			,set: function (x)
			{
				this.setStep (x);
			}
			,get: function ()
			{
				return this._stepIndex;
			}
		},
		stepFunc:
		{
			type: Function
			,set: function (x)
			{
				this._stepFunc = x;
				this.setStep (this._stepIndex);
			}
			,get: function ()
			{
				return this._stepFunc;
			}
		}
	}

	,_steps: []
	,_stepNode: null
	,_stepIndex: -1
	,_stepName: null
	,_stepCount: 0
	,_stepFunc: null

	,appendChild: function (step)
	{
		if (!(step instanceof Step))
			return;

		this._node.appendChild (step.node);
		this._steps[step.name] = step;

		if (step.name === this._stepName)
			this._showStepNode ();

	}
	,render: function ()
	{
		var node = this.createRoot ('div');
	}
	,setStep: function (stepIndex)
	{
		if (!(stepIndex >= -1 && stepIndex < this.stepCount))
			return;
		
		if (this._stepFunc && stepIndex != -1)
		{
			var stepName = this._stepFunc (stepIndex);
		
			if (stepName)
			{
				if (this._step)
					this._step.node.style.display = 'none';

				this._stepName = stepName;
				this._showStepNode ()
				this._setStepIndex (stepIndex);
			}
			else if (this._stepIndex < stepIndex)
				this.setStep (stepIndex + 1);
			else
				this.setStep (stepIndex - 1);
		}
		else
		{
			if (this._step)
				this._step.node.style.display = 'none';

			this._stepName = null;
			this._setStepIndex (stepIndex);
		}
	}
	
	,_showStepNode: function ()
	{
		var step = this._steps[this._stepName];

		if (step)
		{
			this._step = step;
			step.node.style.display = 'block';
		}
	}

	,_setStepIndex: function (stepIndex)
	{
		this._stepIndex = stepIndex;
		this.emit ('step-change', stepIndex);
	}
	
	,movePrevious: function ()
	{
		this.setStep (this._stepIndex - 1);
	}
	
	,moveNext: function ()
	{
		this.setStep (this._stepIndex + 1);
	}
});