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

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

	alias: [
		"widget.ip2DomainModelEntityFormDesignerPropertyGrid"
	],

	border: false,

	sortableColumns: false,

	config: {
		record: null
	},

	requires: [
		"Portfolio.designer.model.domainModelEntityFormDesignerPropertyGridModel"
	],

	initComponent: function() {

		var i_self = this;

		var i_store = Ext.create("Ext.data.Store", {
			model: "Portfolio.designer.model.domainModelEntityFormDesignerPropertyGridModel"
		});

		i_self.store = i_self.propStore = i_store;

		i_self.callParent();

		var i_grouper = Ext.create("Ext.util.Grouper", {
			property: "category",
			groupFn: function (i_record) {
				return i_record.get("category");
			}
		});

		i_store.setGrouper(i_grouper);
	},

	getCellEditor: function(i_record, column) {

		var i_self = this;
		var propName = i_record.get(i_self.nameField);
		var val = i_record.get(i_self.valueField);
		var i_editor = i_self.getConfigProp(propName, "editor");
		var type = i_self.getConfigProp(propName, "type");
		var o_editors = i_self.editors;
		var i_field;

		if (i_editor) {

			if (!(i_editor instanceof Ext.grid.CellEditor)) {

				if (!(i_editor instanceof Ext.form.field.Base)) {

					Ext.apply(i_editor, {
						value: val
					});

					if (i_editor.xtype) {

						i_field = Ext.widget(i_editor.xtype, i_editor);
					}
					else {
						i_field = Ext.ComponentManager.create("textfield", i_editor);
					}
				}

				i_editor = i_self.setConfigProp(propName, "editor", new Ext.grid.CellEditor({
					field: i_field
				}));

				i_field.isChanging = true;
				i_editor.alignment = "tl";

				i_editor.onFocusLeave = (function(f_old) {
					return function(e) {
						var i_self = this;
						var i_field = this.field;
						if (!i_field.isChanging) {
							f_old.apply(this, arguments);
						}
						else {
							i_field.isChanging = false;
						}
					};
				})(i_editor.onFocusLeave);
			}
		}
		else {
			if (type) {
				switch (type) {
					case "date":
						i_editor = o_editors.date;
					break;
					case "number":
						i_editor = o_editors.number;
					break;
					case "boolean":
						i_editor = o_editors.boolean;
					break;
					default:
						i_editor = o_editors.string;
				}
			}
			else {
				if (Ext.isString(val)) {
					i_editor = o_editors.string;
				}
				else if (Ext.isDate(val)) {
					i_editor = o_editors.date;
				}
				else if (Ext.isNumber(val)) {
					i_editor = o_editors.number;
				}
				else if (Ext.isBoolean(val)) {
					i_editor = o_editors.boolean;
				}
				else if (Ext.isArray(val)) {

					var a_data = [];

					Ext.Array.each(val, function(value) {
						var o_value = {
							key: value,
							value: value
						};
						a_data.push(o_value);
					});

					var i_store = Ext.create("Ext.data.Store", {
						fields: [
							{
								name: "key", type: "auto"
							},
							{
								name: "value", type: "auto"
							}
						],
						data: a_data
					});

					i_field = Ext.widget("combobox", {
						store: i_store,
						valueField: "value",
						displayField: "key",
						editable: false,
						queryMode: "local",
						value: val,
						multiSelect: true
					});

					i_editor = i_self.setConfigProp(propName, "editor", new Ext.grid.CellEditor({
						field: i_field
					}));
				}
				else if (Ext.isObject(val)) {
					if (i_editor) {
						if (!i_editor.field) {
							i_editor = i_self.setConfigProp(propName, "editor", new Ext.grid.CellEditor({
								field: Ext.widget("i18n-textfield", {
									value: val
								})
							}));
						}
					}
					else {
						i_editor = i_self.setConfigProp(propName, "editor", new Ext.grid.CellEditor({
							field: Ext.widget("i18n-textfield", {
								value: val
							})
						}));
					}
				}
				else {
					if (!i_editor) {
						i_editor = o_editors.string;
					}
					else {
						if (!type && !i_editor.field) {
							i_editor = o_editors.string;
						}
					}
				}
			}
		}

		i_editor.editorId = propName;
		i_editor.field.column = i_self.valueColumn;

		if (i_editor.field.xtype == "i18ntextfield") {

			i_editor.alignment = "tl";

			if (!i_editor.isChanged) {

				i_editor.isChanged = true;

				i_editor.onFocusLeave = (function(f_old) {
					return function(e) {
						var i_self = this;
						var i_field = this.field;
						if (!i_field.isChanging) {
							f_old.apply(this, arguments);
						}
					};
				})(i_editor.onFocusLeave);
			}
		}

		if (i_editor.field.xtype == "i18n-textfield") {
			i_editor.onFocusLeave = (function(f_old) {

				return function() {

					var i_field = this.field;
					var i_selection = i_field.getSelections();
					if (i_selection.isHidden()) {
						f_old.apply(this, arguments);
					}
				};
			})(i_editor.onFocusLeave);
		}

		return i_editor;
	},

	setSource: function(source, o_config) {

		var i_self = this;

		var o_result = {};

		if (o_config !== undefined) {

			i_self.sourceConfig = Ext.apply({}, o_config);
			i_self.configure(i_self.sourceConfig);
		}

		var i_store = i_self.getStore();

		var i_model = i_store.getModel();

		var a_records = [];

		if (Ext.isArray(source)) {
			a_records = source;

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

				o_result[o_data.name] = o_data.value;
			});
		}
		else {

			o_result = source;

			Ext.Object.each(source, function(s_key, value) {

				var i_record = i_model.create({
					name: s_key,
					value: value,
					category: "system"
				});

				a_records.push(i_record);
			});
		}

		i_self.source = o_result;

		i_store.loadData(a_records);
	}
});
