
Ext.define('Ext.ux.widget.folderorganizer.Pages', {

	extend: 'Ext.ux.folderorganizer.Organizer',

	alias: ['widget.widgetpages'],

	initComponent: function () {

		var i_cmp = this,
			o_config = i_cmp.initialConfig,
			o_settings = {};

		var a_proxyMap = [
			'folderTitle', 'folderUiName', 'folderOrderBy',
			'folderKey', 'propertyKey', 'contentToken',
			'folderToken', 'contentKey', 'propertyKey',
			'contentTitle', 'contentFilter',
			'folderDataLibrary', 'folderUiName'
		];

		// Start building the settings object from the initialConfig
		Ext.apply(o_settings, ip2.pick(i_cmp.initialConfig, [
			'proxy', 'dmd', 'entity', 'xpd', 'extraProps', 'environment',
			'menus', 'buttons',	'expandToLevel', 'levelSelectFirst'
		]));

		Ext.apply(o_settings, ip2.pick(o_config, a_proxyMap));

		var o_organizer = {};

		Ext.apply(o_organizer, {
			xtype: 'xpdlanguagespanel',
			keyMap: {
				relation: {
					child: o_config.relation.keys.foreign,
					parent: o_config.relation.keys.token
				},
				id: o_config.relation.keys.foreign
			},
			xpdId: '__cid__',
			name: o_config.field,
			panelPrefix: o_config.field + '_',
			languagePropertyName: o_config.relation.keys.on,
			propertiesPropertyName: o_config.relation.keys.properties,
			xpd: o_config.relation.xpd,
			dmd: {
				dmd: o_config.relation.dmd,
				entity: o_config.relation.entity,
				context: 'form'
			},
			trackerParent: i_dataTrackerParent.self,
			batchName: i_owner.initialConfig.batchName,
			updateTrackerRecord: function(data) {

				var self = this,
					keyMap = self.getKeyMap(),
					propertiesPropertyName = self.getPropertiesPropertyName(),
					id = data[keyMap.relation.child],
					dataTrack = self.getDataTrack(),
					properties = data[propertiesPropertyName],
					cid;

				properties = Ext.decode(properties);

				cid = String(properties.__cid__);

				dataTrack.each(function (parent) {

					// We do not wish to change the entityeditor as it will go dirty then!
					if (!parent.self.entityEditor) {

						var store = parent.getStore(),
							record = store.getById(id),
							s_pf = o_config.columnPrefix;

						if (!record) return false;

						ip2.each(properties, function (value, s_prop) {

							if (s_prop == '__cid__' && record.get(o_config.columnCid) != cid) {
								record.set(o_config.columnCid, cid);
							}
							else if (record.data[s_pf + s_prop] != null && record.data[s_pf + s_prop] != value) {
								record.set(s_pf + s_prop, value);
							}
						});
					}
				});
			},
			updateTrackerTitle: function (i_form, o_data) {

				// Get a title for the entity editor!
				var i_xpd = i_form.getXpdField(),
					i_editor = i_owner.up('[entityEditor=true]'),
					s_prop = o_data[s_properties],
					o_prop,
					s_title, s_type;

				try {
					o_prop = Ext.decode(s_prop)
				}
				catch (err) {}

				if (o_prop) {
					s_title = o_prop.title || o_prop.header;
					s_type = i_xpd.xpdData.collections[o_prop[i_xpd.xpd.idProperty]].title;

					s_type = ip2.xpd.translate(i_xpd.xpd, s_type);
					s_title = s_type + ': ' + s_title;
					i_editor.updateTitle(s_title);
				}
			},
			listeners: {
				'recordload': {
					fn: function (i_langPanel, i_form, i_record) {
						i_langPanel.updateTrackerTitle(i_form, i_record.data);
						ip2.waitMask.hide();
					}
				},
				'isready': {
					fn: function(self) {

						var master = self.getMaster(),
							xpdPanel = master.down('[name=' + s_properties + ']'),
							picker = xpdPanel.getCollectionPicker();

						// Run the picker change on init to make sure we dim
						// the needed other widgets in case we have first picker value
						// as our collection cid
						i_widget.onPickerChange(picker, picker.getValue());

						picker.on({
							'change': {
								fn: i_widget.onPickerChange,
								scope: i_widget
							}
						});
					},
					single: true
				},
				'xpdcreate': {
					fn: function(self, data) {
						self.updateTrackerRecord(data);
						self.updateTrackerTitle(self.getMaster(), data);
					}
				},
				'xpdupdate': {
					fn: function(self, data) {
						self.updateTrackerRecord(data);
						self.updateTrackerTitle(self.getMaster(), data);
					}
				}
			}
		});

		Ext.apply(i_cmp, o_organizer);

		// Clean up;
		ip2.each(a_proxyMap, function (s_property) {
			delete i_cmp[s_property];
		});

		i_cmp.callParent(arguments);

		return i_cmp;
	}
});
