
var Lot = require ('./lot');
var IteratorIface = require ('./iterator-iface');
var ModelIface = require ('./model-iface');

/**
 * A light iterator for models. It assumes that its row and model properties
 * are always valid.
 */
module.exports = new Class
({
	Extends: Lot
	,Implements: IteratorIface
	,Properties:
	{
		model:
		{
			type: ModelIface
			,set: function (x)
			{
				this._model = x;
			}
			,get: function ()
			{
				return this._model;
			}
		},
		row:
		{
			type: Number
			,set: function (x)
			{
				this._row = x;
				this.rowChanged ();
			}
			,get: function ()
			{
				return this._row;
			}
		},
		numRows:
		{
			type: Number
			,get: function ()
			{
				return this._model ?
					this._model.numRows : 0;
			}
		},
		ready:
		{
			type: Boolean
			,get: function ()
			{
				return this._model ?
					this._model.ready : false;
			}
		}
	}

	,initialize: function (props)
	{
		Object.assign (this, {
			 _model: null
			,_row: -1
			,_rowLock: false
		});
		this.parent (props);
	}

	,_paramsChanged: function (diff)
	{
		if (!this._rowLock)
		for (var key in diff)
			this._model.set (this._row, key, diff[key]);
	}

	,rowChanged: function ()
	{
		this._rowLock = true;

		var row = this._model.getObject (this._row);
		this.params = row != null ? row : {};

		this._rowLock = false;
	}
});
