Ext.define("Ext.ux.folderorganizer.Util", {

	singleton: true,

	addCollectionColumns: function(i_self, a_columns) {

		a_columns = a_columns || [];

		var collection;
		var cid;
		var a_collectionColumns = [];

		i_self.collectionColumns = i_self.collectionColumns || {};

		// find cid for all children
		var findAllChildrenCid = function(cid) {
			var o_collection = Ext.ux.folderorganizer.Util.getXpdCollection(i_self, cid);
			if (!o_collection || !o_collection.allowedchilds) {
				return;
			}
			Ext.Array.each(o_collection.allowedchilds, function(n_child) {
				i_self.collectionColumns[o_collection.allowedchilds[n_child]] = {};
				if (o_collection.allowedchilds[n_child] != cid) {
					findAllChildrenCid(o_collection.allowedchilds[n_child]);
				}
			});
		};

		if (Ext.isEmpty(i_self.cid)) {
			// all collections
			Ext.Object.each(i_self.xpd.collections, function(s_key, o_value) {
				i_self.collectionColumns[o_value.id] = {};
			});
		}
		else {
			if (Ext.isObject(i_self.cid)) {
				if (i_self.cid.child) {
					findAllChildrenCid(i_self.cid.root);
				}
				else {
					findAllChildrenCid(i_self.cid);
				}
			}
			else {
				findAllChildrenCid(i_self.cid);
			}
		}

		var addColumn = function(o_column, o_collection, s_name, s_cid) {

			if (o_column && o_column.index) {

				if (!s_name || o_column.cidColumn) {
					s_cid = o_column.index;
					s_name = "__cid__";
				}

				var o_result = Ext.Object.merge({}, o_column, {
					dataIndex: "PROPERTIES",
					renderer: function(value, meta, record, rowIndex, colIndex) {
						var o_values = Ext.decode(value, true) || {};
						if (i_self.collectionColumns[o_values.__cid__] && i_self.collectionColumns[o_values.__cid__][colIndex]) {
							var s_property = i_self.collectionColumns[o_values.__cid__][colIndex].property;
							var value = o_values[s_property];

							if (s_property == "__cid__") {
								value = ip2.xpd.translate(i_self.xpd, i_self.xpd.flatten.collections[value].title);
							}
							if (Ext.isObject(value)) {
								return ip2.i18n(value);
							}
							else {
								return value;
							}
						}
						else {
							return "";
						}
					}
				});

				a_collectionColumns.push(o_result);

				i_self.collectionColumns[s_cid][o_column.index] = Ext.apply({}, {
					title: (s_name == "__cid__") ? "Type" : ip2.xpd.translate(i_self.xpd, o_collection.title),
					property: s_name
				});
			}
		};

		Ext.Object.each(i_self.collectionColumns, function(s_key, o_value) {
			var o_organizer;
			var o_collection = Ext.ux.folderorganizer.Util.getXpdCollection(i_self, s_key);
			if (o_collection && o_collection.organizer) {
				o_organizer = o_collection.organizer;
				addColumn(o_organizer.column, o_collection, s_key);
			}
			if (o_collection && o_collection.properties) {
				Ext.Object.each(o_collection.properties, function(s_property, o_property) {
					if (o_property.organizer) {
						o_organizer = o_property.organizer;
						addColumn(o_organizer.column, o_property, s_property, s_key);
					}
				});
			}
		});

		a_columns = a_columns.concat(a_collectionColumns);
		return a_columns;
	},

	setCollectionColumnHeader: function(me, node) {

		if (me.extraColumns && me.extraColumns.length > 0) {
			return;
		}

		if (!node.data.PROPERTIES) {
			return;
		}

		var props = Ext.decode(node.data.PROPERTIES, true) || {};
		if (props.__cid__ == me.previousNodeCid) {
			return;
		}
		me.previousNodeCid = props.__cid__;

		var s_title;
		var a_columns = (Ext.isFunction(me.getView)) ? me.getView().getGridColumns() : me.treegrid.getView().getGridColumns();

		Ext.Array.each(a_columns, function(i_column, n_index) {
			if (!i_column.isTreeColumn) {
				var o_collection = me.collectionColumns[props.__cid__];
				if (o_collection && o_collection[n_index]) {
					s_title = ip2.i18n(o_collection[n_index].header) || ip2.i18n(o_collection[n_index].title);
				}
				else {
					s_title = "";
				}
				i_column.setText(s_title);
			}
		});
	},

	getXpdCollectionByNode: function(me, node) {

		var cid = Ext.ux.folderorganizer.Util.getXpdCollectionId(me, node);

		if (cid == 0) {
			cid = parseInt((Ext.isArray(me.cid)) ? me.cid[0] : me.cid);
		}

		return Ext.ux.folderorganizer.Util.getXpdCollection(me, cid);
	},

	getXpdCollectionId: function(me, node) {

		if (node.data.cid) {
			return parseInt(node.data.cid);
		}
		else if (node.data.PROPERTIES) {
			var properties = {};
			if (ip2.isStringedObject(node.data.PROPERTIES)) {
				properties = Ext.decode(node.data.PROPERTIES, true) || {};
			}
			else {
				var o_props = {};
				var a_split = node.data.PROPERTIES.split(",");
				ip2.each(a_split, function(s_props) {
					var a_propsplit = s_props.split(":");
					if (a_propsplit.length == 2) {
						o_props[a_propsplit[0].trim()] = a_propsplit[1].trim();
					}
				});
				properties = o_props;
				node.data.PROPERTIES = JSON.stringify(o_props);
			}
			return parseInt(properties.__cid__);
		}
		else if (node.data[me.propertyNameMapping.PROPERTIES]) {
			var properties = Ext.decode(node.data[me.propertyNameMapping.PROPERTIES], true) || {};
			return parseInt(properties.__cid__);
		}
		else {
			return 0;
		}
	},

	getXpdCollection: function(me, cid) {

		if (me.xpd && me.xpd.flatten.collections[cid]) {
			return me.xpd.flatten.collections[cid];
		}
		else {
			return null;
		}
	}
});
