var VnObject = require('./object');
var LotIface = require('./lot-iface');
var Value = require('./value');

module.exports = new Class({
	Extends: VnObject
	,Implements: LotIface
	,Tag: 'vn-lot'
	,Properties: {
		$: {
			type: Object
			,set(x) {
				this.setAll(x);
			}
			,get() {
				return this._params;
			}
		}
		,params: {
			type: Object
			,set(x) {
				this.$ = x;
			}
			,get() {
				return this.$;
			}
		}
	}

	,initialize(props) {
		this._params = new Proxy({}, {
			set(obj, prop, value) {
				return Reflect.set(obj, prop, value);
			}
		});
		VnObject.prototype.initialize.call(this, props);
	}
	
	,keys() {
		return Object.keys(this._params);
	}

	,assign(params) {
		var diff = Value.partialDiff(this._params, params);
		this._assign(diff);
	}

	,setAll(params) {
		var diff = Value.diff(this._params, params);
		this._assign(diff);
	}

	,_assign(diff) {
		if (diff) {
			Object.assign(this._params, diff);
			this._paramsChanged(diff);
			this.changed(diff);
		}
	}

	/**
	 * Called when lot params changes, can be implemented by child classes to be
	 * notified about changes.
	 * 
	 * @param {Object} diff Changed parameters and its new values
	 */
	,_paramsChanged() {}
});