Ext.define("Portfolio.entity.store.Tree", {

	xtype: "portfolio-entity-store-tree",

	extend: "Ext.data.TreeStore",

	alias: [
		"store.ux.entity.treestore",
		"store.portfolio.entity.store.tree"
	],

	requires: [
		"Portfolio.entity.store.Reader"
	],

	alternateClassName: [
		"Ext.ux.entity.TreeStore",
	],

	constructor: function (config) {

		var o_config = Ext.Object.merge(config);
		o_config.proxy = config.proxy || {};

		var i_store = this;
		var o_proxy = {
			type: "ajax",
			url: "/ip2-services/ip2dmdProxy.aspx?" + Ext.urlEncode({
				model: o_config.entity,
				action: "getRows",
				context: o_config.context || "grid",
				uiName: o_config.view || "portfolio",
				dmdFile: o_config.dmd
			}),
			reader: {
				type: "tree.dmd",
				$configStrict: false,
				rootProperty: "rows",
				totalProperty: "totalRows",
				model: config.model
			}
		};

		var o_info = ip2.dmd.getInfo({
			dmd: o_config.dmd,
			entity: o_config.entity,
			view: o_config.view || "portfolio",
			context: o_config.context || "grid"
		});

		o_proxy.entityInfo = o_info;

		Ext.Object.merge(o_config.proxy, o_proxy);

		i_store.callParent([o_config]);
	},

	read: function () {
		this.keep = true;
		return this.load.apply(this, arguments);
	},

	config: {
		sequence: ""
	},

	listeners: {
		beforeload: {
			fn: function (i_store, i_operation) {

				var i_node = i_operation.node;
				var s_where = "";
				var i_tree = i_store.ownerTree;
				var s_keep = "";
				var b_filter = false;
				var i_actionbar;
				var i_filterbar;
				var i_searchField;
				var i_languagePicker;

				var s_language = ip2.getUser().get("USRLNG");

				if (i_tree) {
					i_actionbar = i_tree.getActionBar();
					i_filterbar = i_tree.getFilterBar();
					i_searchField = i_actionbar.down("#search");
					i_languagePicker = i_actionbar.down("#button-language");
				}

				var i_proxy = i_store.getProxy();
				var o_entity = i_proxy.entityInfo.entity;

				var o_extraParams = i_proxy.getExtraParams();

				delete o_extraParams.search;
				delete o_extraParams.where;

				var a_sort = [];
				var s_sort = "";

				//s_where = s_where + i_node.get(i_store.childID);

				if (i_store.keep && !i_node.isRoot()) {
					s_keep = s_where;
				}

				var o_sorters = i_store.getSorters();

				Ext.Array.each(o_sorters.items, function (o_sort) {
					var s_sorter = o_sort.getProperty() + " " + o_sort.getDirection();
					a_sort.push(s_sorter);
				});

				s_sort = a_sort.join(", ");
				o_extraParams.sort = s_sort;

				if (i_filterbar) {
					if (i_filterbar.getWhere()) {
						b_filter = true;
						s_where = i_filterbar.getWhere();
					}

					var a_filters = i_filterbar.getFilters();

					o_sorters.loading = isNaN(o_sorters.updating) ? 1 : o_sorters.updating++;

					Ext.Array.each(a_filters, function(i_filter) {
						var o_sort = i_filter.getSortInfo();
						var b_found = false;
						ip2.each(o_sorters.items, function (i_sorter) {
							if (i_sorter.getProperty() == o_sort) {
								b_found = true;
							}
						});
						if (!b_found) {
							o_sorters.add(o_sort);
						}
					});

					o_sorters.loading = isNaN(o_sorters.updating) ? 0 : o_sorters.updating--;
				}

				if (i_searchField) {
					o_extraParams.search = i_searchField.getRawValue();
					if (!b_filter && o_extraParams.search) {
						s_where = "";
					}
					if (i_searchField.getWhere()) {
						if (!ip2.isEqual(s_where, i_searchField.getWhere())) {
							if (s_where) {
								s_where = s_where + " AND " + i_searchField.getWhere();
							}
							else {
								s_where = i_searchField.getWhere();
							}
						}
					}
				}

				if (s_keep) {
					var a_temp = [s_keep];

					if (s_keep.toLowerCase() != s_where.toLowerCase()) {
						a_temp.push(s_where);
						a_temp = a_temp.filter(function (value) {
							return value;
						});
					}
					s_where = a_temp.join(" AND ");
				}

				if (!i_node.isRoot()) {
					if (i_languagePicker && o_entity.language) {
						o_extraParams.language = i_languagePicker.key;
						o_extraParams.languageColumn = o_entity.language.column;
					}
					else {
						o_extraParams.language = s_language;
					}
				}
				else {
					delete o_extraParams.language;
					delete o_extraParams.languageColumn;

				}

				var a_where = [
					s_where
				];

				if (i_node.get(i_node.idProperty) > 0) {
					a_where.push(
						i_store.parentID + " = " + i_node.get(i_node.idProperty)
					);
				}

				a_where = Ext.Array.filter(a_where, function (s_value) {
					return s_value;
				});

				s_where = a_where.join(" AND ");

				if (i_store.getSequence()) {
					a_sort.push(i_store.getSequence() + " ASC");
					s_sort = a_sort.join(", ");
					o_extraParams.sort = s_sort;
				}

				o_extraParams.where = s_where;

				i_store.keep = false;
			}
		}
	}
});
