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

	extend: "Ext.panel.Panel",

	alias: [
		"widget.ip2DomainModelEntityColumnManager"
	],

	layout: {
		type: "border"
	},

	requires: [
		"Portfolio.designer.ui.domainModelEntityColumnGrid",
		"Portfolio.designer.ui.domainModelEntityColumnCommonProperties",
		"Portfolio.designer.ui.domainModelColumnTypeComboBox",
		"Portfolio.designer.ui.uiTypeComboBox"
	],

	config: {
		entity: null,
		columnGrid: null,
		commonProperties: null,
		designer: null,
		columnToEdit: -1,
		required: false,
		entityManager: null
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent();

		i_self.add(i_self.createItems());
	},

	createItems: function() {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var i_entityManager = i_self.getEntityManager();
		var n_column = i_self.getColumnToEdit();
		var o_entity = i_self.getEntity();
		var a_items = [];

		var i_columnGrid = Ext.widget("ip2DomainModelEntityColumnGrid", {
			designer: i_designer,
			entityManager: i_entityManager,
			columnManager: i_self,
			columnToEdit: n_column,
			entity: o_entity,
			region: "center",
			border: true,
			split: true,
			flex: 2,
			required: i_self.getRequired()
		});

		var i_commonProperties = Ext.widget("ip2DomainModelEntityColumnCommonProperties", {
			designer: i_designer,
			columnManager: i_self,
			entityManager: i_entityManager,
			entity: o_entity,
			region: "east",
			split: true,
			flex: 1,
			listeners: {
				afterrender: {
					fn: function() {
						if (n_column) {
							var i_columnGrid = i_self.getColumnGrid();
							i_columnGrid.fireEvent("commonPropertyRendered", i_columnGrid, i_self);
						}
					}
				}
			}
		});

		var i_selectionModel = i_columnGrid.getSelectionModel();
		var i_store = i_columnGrid.getStore();
		var a_records = i_store.getRange();

		i_selectionModel.on("selectionchange", i_self.onRowSelect, i_self);
		i_entityManager.entityPropertyManager.fireEvent("datachanged", a_records, null);

		i_commonProperties.editingPlugin.on({
			beforeedit: {
				fn: function() {
					return !i_self.getRequired();
				}
			}
		});

		i_self.setColumnGrid(i_columnGrid);
		i_self.setCommonProperties(i_commonProperties);

		a_items.push(i_columnGrid);
		a_items.push(i_commonProperties);

		return a_items;
	},

	onRowSelect: function(sm, a_records) {

		var i_self = this;
		var i_commonProperties = i_self.getCommonProperties();

		if (a_records.length) {
			i_commonProperties.setRecord(a_records[0]);
			i_commonProperties.updateValues(a_records[0]);

			i_commonProperties.editingPlugin.startEditByPosition(
				{
					row: 0,
					column: 0
				}
			);
		}
	},

	doSave: function(o_entity) {

		var i_self = this;
		var i_columnGrid = i_self.getColumnGrid();
		var i_designer = i_self.getDesigner();
		var a_columns = [];
		var i_store = i_columnGrid.getStore();
		var a_records = i_store.getRange();
		var b_valid = true;

		Ext.Array.each(a_records, function(i_record) {

			var o_column = {};
			var o_recordData = i_record.getData();
			var type = o_recordData.from ? "extention" : o_recordData.like ? "like" : o_recordData.type ? o_recordData.type : "new";
			var o_template = i_designer.normal.types[type];

			if (!o_template) {
				o_template = {
					properties: {}
				};
			}

			var a_properties = o_template.properties || [];

			Ext.Object.each(i_record.data, function(s_key) {
				Ext.Array.each(a_properties, function(o_property) {

					if (o_property.name == s_key) {

						var value = i_record.get(s_key);
						var n_length = 0;
						var n_decimal = 0;
						var a_values = [];

						switch (s_key) {
							case "type":
								if (value == "property") {
									value = "varchar";
								}
								if (value.toLowerCase() == "varchar" ||
									value.toLowerCase() == "char") {
									n_length = i_record.get("len") || 0;
									if (n_length) {
										value = value + "(" + n_length + ")";
									}
								}
								if (value.toLowerCase() == "decimal") {
									n_length = i_record.get("len") || 0;
									n_decimal = i_record.get("dec") || 0;
									a_values = [n_length, n_decimal];
									if (n_length) {
										value = value + "(" + a_values.join(",") + ")";
									}
								}
								break;
						}
						if (Ext.isString(value)) {
							if (Ext.String.startsWith("{") && Ext.String.endsWith("}")) {
								var parsed = Ext.decode(value, true) || "";
								if (parsed || parsed === false) {
									if (!Ext.isFunction(parsed)) {
										value = parsed;
									}
								}
							}
						}
						if (value) {
							o_column[s_key] = value;
						}

						return false;
					}
				});
			});
			if (Ext.Object.getKeys(o_column).length > 0) {
				a_columns.push(o_column);
			}
		});

		if (a_columns.length === 0) {

			ip2.messageBox({
				title: ip2.i18n("error"),
				message: ip2.i18n("addAtLeaseOneColumn"),
			});

			b_valid = false;
		}

		// replace columns for entity
		if (!Ext.isEmpty(a_columns)) {

			Ext.apply(o_entity,  {
				columns: a_columns
			});
		}
		return b_valid;
	}
  });
