Ext.define('Ext.ux.stackorganizer.view.ContentAccordion', {
	extend: 'Ext.tab.Panel',
	alias: 'widget.stackcontentaccordion',

	config: {
		/**
		 * @cfg xpd object The not flattened XPD object
		 */
		xpd: null,
		/**
		 * @cfg stack object The stack as a node of data to use for filtering
		 */
		stack: null,
		/**
		 * @cfg tokenKey string
		 */
		tokenKey: null,
		/**
		 * @cfg collectionId number
		 */
		collectionId: null,
		/**
		 * @cfg dragConfig object Configuration for the DD
		 */
		dragConfig: null,
		fields: null
	},

	// The flattened XPD
	flattened: null,

	initComponent: function () {

		var i_cmp = this;

		i_cmp.callParent();

		var o_xpd = i_cmp.getXpd();
		var i_xpd = ip2.xpd.flatten(o_xpd);
		var s_tokenKey = i_cmp.getTokenKey();
		var o_stack = i_cmp.getStack();
		var a_collections = [];

		i_cmp.flattened = i_xpd;

		// Create the collection array and sort it for sequences
		Ext.Object.each(i_xpd.collections, function (s_key, o_collection) {

			if (s_key != 'all') {

				n_pos = o_collection.sequence || 0;

				while (a_collections[n_pos]) {
					n_pos++;
				}

				a_collections[n_pos] = o_collection;
			}
		});

		// Iterate over each collection and build a grid/widget for each
		// to add to the accordion, if allowed per XPD
		Ext.Array.each(a_collections, function (o_collection) {

			// We need a collection, and one with a dmd and entity
			// if not we can not provide a entitygrid
			if (o_collection && o_collection.dmd && o_collection.entity) {

				var i_grid;
				var i_store;
				var b_allow;

				// Determine if the configured collectionId is an allowed parent
				// for this collection
				b_allow = (function () {

					var b_allow = false;
					var parents = o_collection.allowedparents;
					var n_id = i_cmp.getCollectionId();

					// Parents is an array
					// If array is empty or collectionId is in the allowedparent array
					// it is allowed
					if (Ext.isArray(parents)) {

						if (!parents.length) {
							b_allow = true;
						}
						else {
							b_allow = (Ext.Array.indexOf(parents, n_id) >= 0);
						}
					}

					return b_allow;
				})();

				if (!b_allow) {
					return false;
				}

				i_store = Ext.create('Ext.ux.stackorganizer.store.Grid', {
					fields: i_cmp.getFields()
				});

				i_grid = i_cmp.add({
					xtype: 'stackcontentgrid',
					title: ip2.xpd.translate(i_xpd, o_collection.title, ip2.session.getLanguage()),
					xpd: i_cmp.getXpd(),
					supportXpd: o_collection.xpd,
					tokenKey: i_cmp.getTokenKey(),
					stack: o_stack,
					stackId: o_collection.id,
					collection: o_collection,
					multiSelect: false,
					store: i_store,
					onLeftKey: function (i_self) {
						i_cmp.fireEvent('onLeftKey', i_cmp);
						i_cmp.onLeftKey && i_cmp.onLeftKey();
					}
				});

				i_cmp.relayEvents(i_grid, ['stackadd']);
			}

			i_cmp.setActiveTab(i_cmp.items.items[0]);
		});

		return i_cmp;
	},

	setActiveFocus: function () {

		var i_cmp = this;
		var i_active = i_cmp.getActiveTab();
		var a_selection;

		if (!i_active) {
			i_cmp.setActiveTab(0);
		}

		// Get currently selected items
		a_selection = i_active.getSelectionModel().getSelection();

		// If none selected, select the first one
		if (!a_selection.length) {
			var i_selected = i_active.getStore().getAt(0);
			a_selection = i_selected ? [i_selected] : [];
		}

		i_active.getView().focus();
		i_active.getSelectionModel().select(a_selection);

		return i_cmp;
	},

	getStackById: function(id) {

		var i_self = this;
		var a_items = i_self.query("stackcontentgrid[stackId=" + id + "]");
		if (a_items.length > 0) {
			return a_items[0];
		}
		return null;

	},

	/**
	 * @method refreshStackData
	 * Will go through all child grids and refresh their data based on the passed stack data argument
	 *
	 * @param o_stack object The stack data object needed to build new filters
	 */
	refreshStackData: function (o_stack) {

		o_stack = o_stack || this.getStack();

		var i_cmp = this;

		i_cmp.items.each(function (i_grid) {
			i_grid.setStack(o_stack);
		});

		return i_cmp;
	}
});
