Ext.define("Portfolio.designer.ui.domainModelEntityHierarchyProperties", {

	extend: "Ext.grid.property.Grid",

	alias: [
		"widget.ip2DomainModelEntityHierarchyProperties"
	],

	sortableColumns: false,

	config: {
		entityManager: null,
		designer: null,
		entity: null,
		hierarchyManager: null
	},

	requires: [
		"Portfolio.designer.ui.DataModels",
		"Portfolio.designer.ui.domainModelEntityComboBox"
	],

	initComponent: function() {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var i_entityManager = i_self.getEntityManager();

		i_self.propertyNames = Ext.apply({}, {
			entity: "",
			baseTable: "",
			allowSync: false,
			allowUpdate: false,
			where: ""
		});

		i_self.source = Ext.apply({}, {
			entity: "",
			baseTable: "",
			allowSync: false,
			allowUpdate: false,
			where: ""
		});

		i_self.customEditors = {
			entity: Ext.widget("ip2DomainModelEntityComboBox", {
				entities: i_designer.model.required,
				queryMode: "local",
				hiddenEntities: [
					{
						name: i_self.entity.name
					}
				]
			}),
			baseTable: Ext.create("Portfolio.designer.ui.DataModels", {
				listeners: {
					focus: function() {
						this.setPlugin("DB2");
					}
				}
			})
		};

		i_self.listeners = {
			propertychange: function(source, recordId, value) {
				if (recordId == "baseTable") {
					i_self.fireEvent("updateTree", i_self, recordId, source);
				}
				else if (recordId == "entity") {
					i_self.fireEvent("updateTree", i_self, recordId, source);
				}
			}
		};

		i_self.callParent();

		i_self.enableBubble("updateTree");
	},

	updateValues: function(o_data) {

		var i_self = this;
		o_data = o_data || {};

		var o_properties = {};

		Ext.Object.each(i_self.propertyNames, function(s_key, value) {
			o_properties[s_key] = o_data[s_key] || value;
		});

		i_self.setSource(o_properties);
	},

	// updateColumnFromFields: function(entityName, baseTable) {

	// 	var i_self = this;

	// 	var i_designer = i_self.getDesigner();
	// 	var i_entityManager = i_self.getEntityManager();
	// 	var o_entity = i_self.getEntity();

	// 	if (!i_entityManager.entity.extend) {
	// 		i_entityManager.entity.extend = {};
	// 	}
	// 	i_designer.current.extend.entity = entityName;
	// 	i_designer.current.extend.baseTable = baseTable;

	// 	// remove all existing from fields
	// 	var store = i_entityManager.columnManager.columnGrid.store;

	// 	store.data.each(function(record) {
	// 		if (record.data.from) {
	// 			store.remove(record);
	// 		}
	// 	});

	// 	// add "from" fields from new base table or entity
	// 	var extend = ip2.determineEntityExtend(i_designer.current.extend.entity, i_designer.current.extend.baseTable);

	// 	if (extend.type == "baseTable") {
	// 		var model = {};
	// 		Ext.Ajax.request({
	// 			url: ip2.setServiceUrl(i_designer.serviceProxy,
	// 				{
	// 					action: "getImportEntityList",
	// 					lib: extend.library,
	// 					tbl: extend.table
	// 				}
	// 			),
	// 			success: function (response) {
	// 				eval("model =" + response.responseText);
	// 				for (var name in model.model.entities) {
	// 					i_self.addColumnFromFields(model.model.entities[name].columns);

	// 					// build standard grid layout with limit number of columns
	// 					ip2.saveEntityGridUI(o_entity, null, i_entityManager.columnManager.columnGrid.store);

	// 					// build standard form layout with limit number of portlet / fields
	// 					ip2.saveEntityFormUI(o_entity, model.model.entities[name].columns);
	// 				}
	// 			}
	// 		});
	// 	}
	// 	else if (extend.type == "entity") {
	// 		i_self.addColumnFromFields(i_designer.model.entities[entityName].columns);
	// 	}
	// },

	addColumnFromFields: function(a_columns) {

		var i_self = this;

		var i_designer = i_self.getDesigner();
		var i_entityManager = i_self.getEntityManager();
		var i_columnManager = i_entityManager.columnManager;
		var i_grid = i_columnManager.columnGrid;

		var i_store = i_grid.getStore();
		var i_model = i_store.getModel();
		var a_records = [];

		Ext.Array.each(a_columns, function(o_column) {

			var o_config = Ext.apply({}, {
				name: o_column.name,
				from: o_column.name,
				type: "extention",
				title: o_column.title || o_column.name
			});

			var i_record = i_model.create(o_config);
			a_records.push(i_record);
		});

		i_store.add(a_records);
	}

  });
