Ext.define("Ext.ux.data.dmd.Store", {

	extend: "Ext.data.Store",

	/**
	 * @cfg {String} dmd
	 * Name of the DMD file
	 */
	dmd: "",

	/**
	 * @cfg {String} entity
	 * Name of the entity in the DMD
	 */
	entity: "",

	/**
	 * @cfg {String} [view="portfolio"]
	 * Name of the view to use
	 */
	view: "portfolio",

	/**
	 * @cfg {String} [context="grid"]
	 * View context (grid or form)
	 */
	context: "grid",

	/**
	 * @cfg {Object} [key=null]
	 */
	key: null,

	/**
	 * @cfg {Array} [union=null]
	 */
	union: null,

	/**
	 * @cfg {Boolean} [meta=false]
	 */
	meta: null,

	extraParams: null,

	/**
	 * @property {Object} dmdObject
	 * The DMD object from path
	 */
	dmdObject: null,

	/**
	 * @property {String} batchUpdateMode
	 * Sets the updating behavior based on batch synchronization. 'operation' (the default) will update the Store's
	 * internal representation of the data after each operation of the batch has completed, 'complete' will wait until
	 * the entire batch has been completed before updating the Store's data. 'complete' is a good choice for local
	 * storage proxies, 'operation' is better for remote proxies, where there is a comparatively high latency.
	 */
	batchUpdateMode: "complete",

	constructor: function(o_config) {

		var i_store = this;

		// Get the DMD object from the dmd property,
		// set it - and overload config name to path
		var o_dmd = ip2.resource.get(o_config.dmd);
		var o_model = o_dmd.model;

		i_store.dmdObject = Ext.clone(o_dmd);
		o_config.dmd = o_dmd._path;

		var o_entity = Ext.Array.findBy(o_model.entities, function(o_value) {
			return o_value.name == o_config.entity;
		});

		var o_ui = o_entity.ui || {
			grids: {}
		};

		var o_view = o_ui.grids[o_config.view] || o_ui.grids.portfolio;

		Ext.applyIf(o_config, {
			sort: o_view.sort
		});

		var i_model = ip2.model(o_config);

		var o_params = ip2.dmd.createExtraParams(o_config);

		var o_proxy = Ext.apply({
			type: "dmd",
			extraParams: o_params
		}, o_dmd.proxy);

		Ext.applyIf(o_config, {
			model: i_model,
			proxy: o_proxy
		});

		var o_settings = Ext.apply({}, o_config);
		i_store.callParent([o_settings]);

		return i_store;
	},

	getExtraParams: function () {

		var i_store = this;
		var i_proxy = i_store.getProxy();

		return i_proxy.getExtraParams();
	},
	setExtraParams: function (o_params) {

		var i_store = this;
		var i_proxy = i_store.getProxy();

		i_proxy.setExtraParams(o_params);
	},

	getKey: function () {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		return o_params.key;
	},
	setKey: function (value) {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		if (value && Ext.Object.getSize(value)) {
			i_store.key = value;
			o_params.key = value;
		}
		else {
			i_store.key = null;
			delete o_params.key;
		}

		return i_store;
	},
	getMeta: function () {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		return o_params.meta;
	},
	setMeta: function (value) {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		value = value ? true : false;
		i_store.meta = value;
		o_params.meta = value;

		return i_store;
	},
	getUnion: function () {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		return o_params.union;
	},
	setUnion: function (a_union) {

		var i_store = this;
		var o_params = i_store.getExtraParams();

		if (!a_union || !a_union.length || a_union == null) {
			i_store.union = null;
			delete o_params.union;
		}
		else {
			i_store.union = a_union;
			o_params.union = a_union;
		}

		return i_store;
	}
});
