
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(x) {
				this._model = x;
			}
			,get() {
				return this._model;
			}
		},
		row: {
			type: Number
			,set(x) {
				this._row = x;
				this.rowChanged();
			}
			,get() {
				return this._row;
			}
		},
		numRows: {
			type: Number
			,get() {
				return this._model ?
					this._model.numRows : 0;
			}
		},
		ready: {
			type: Boolean
			,get() {
				return this._model ?
					this._model.ready : false;
			}
		}
	}

	,_model: null

	,initialize(props) {
		Object.assign(this, {
			 _row: -1
			,_rowLock: false
		});
		Lot.prototype.initialize.call(this, props);
	}

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

	,rowChanged() {
		var row;
		
		if (this._model)
			row = this._model.getObject(this._row);

		this._rowLock = true;
		this.params = row != null ? row : {};
		this._rowLock = false;
	}
});
