Ext.define("Portfolio.dynapp.ui.column.Properties", {

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

	alias: [
		"widget.dynapp.ui.column.properties"
	],

	border: false,

	sortableColumns: false,

	config: {
		clones: {}
	},

	alternateClassName: [
		"Ext.dynapp.ui.column.Properties"
	],

	requires: [
		"Portfolio.dynapp.model.Column",
		"Portfolio.designer.ui.domainModelColumnTypeComboBox"
	],

	initComponent: function() {

		var i_self = this;
		var i_record;
		if (!i_self._record) {

			var i_model = Ext.ClassManager.get("Portfolio.dynapp.model.Column");

			i_record = i_model.create({
				type: "varchar"
			});
		}
		else {
			i_self.record = i_self._record;
			delete i_self._record;
		}

		i_self.source = {};
		i_self.customRenderers = {};
		i_self.error = {};
		i_self.error.rowIdx = -1;
		i_self.error.text = "";
		i_self.record = i_record;

		// setup all editors from normal.dmd

		var fields = {};

		var o_types = Ext.clone(i_self.types);
		var o_generics = Ext.clone(i_self.generic);
		var o_clones = {};

		Ext.Object.each(o_types, function(s_key, o_type) {
			var o_clone = Ext.clone(o_type);
			Ext.Object.each(o_generics, function(s_key, o_generic) {
				var b_found = false;
				Ext.Array.each(o_type.properties, function(o_prop) {
					if (o_prop.name == s_key) {
						b_found = true;
					}
				});
				if (!b_found) {
					o_clone.properties.push(Ext.clone(o_generic));
				}
			});
			o_clones[s_key] = o_clone;
		});

		for (var type in o_types) {
			for (var prop in o_types[type].properties) {
				if (o_types[type].properties[prop].editor) {
					var field = o_types[type].properties[prop].editor;
					field.name = o_types[type].properties[prop].name;
					field.listeners = (field.listeners) ? field.listeners : {};
					if (field.xtype == "ip2DomainModelColumnTypeComboBox") {

						Ext.apply(field, {
							designer: {
								normal: {
									types: Ext.clone(o_clones)
								}
							},
							listeners: {
								select: function(box, i_record) {
									var type = i_record.get("key");
									i_self.record.set("type", type);

									var template = i_self.getClones()[type];
									if (template && template.defaults) {
										for (var name in template.defaults) {
											var value = i_self.record.get(name);
											if (!value && value != undefined) {
												i_self.record.set(name, template.defaults[name]);
											}
										}
									}
									i_self.updateValues(i_self.record);
								}
							}
						});
					}
					else if (field.xtype == "ip2DomainModelExtendFieldComboBox") {

						Ext.apply(field.listeners, {
							select: function(box, i_record) {
								i_self.record.set("from", i_record.get("name"));
								i_self.record.set("name", i_record.get("name"));
								i_self.record.set("title", i_record.get("title"));

								i_self.updateValues(i_self.record);
							}
						});
					}
					else if (field.xtype == "i18ntextfield") {
						i_self.customRenderers[field.name] = function(v) {
							return ip2.i18n(v);
						};
					}
					else if (field.xtype == "i18n-textfield") {
						i_self.customRenderers[field.name] = function(v) {
							return ip2.i18n(v);
						};
					}
					else if (field.xtype == "comboinline") {
						var i_field = Ext.widget(field.xtype, field);
						i_self.customRenderers[field.name] = function(v) {
							return i_field.renderer(v);
						};
					}
					else if (field.xtype == "combosql") {
						field.options = i_self.options;
					}
					else if (field.xtype == "combotable") {
						field.schema = i_self.options.library;
						field.table = i_self.options.file;
					}
					fields[o_types[type].properties[prop].name] = field;
				}
			}
		}

		i_self.customEditors = {};
		for (var field in fields) {
			i_self.customEditors[field] = fields[field];
		}

		if (!i_self.listeners) {
			i_self.listeners = {};
		}

		i_self.callParent();

		i_self.on({
			afterrender: {
				fn: function(item) {
					if (item.meta) {
						var o_meta = item.meta;
						if (o_meta.properties) {
							var o_complete = {};
							var a_metakeys = Ext.Object.getKeys(o_meta);
							var a_propkeys = Ext.Object.getKeys(o_meta.properties);
							Ext.Array.each(a_metakeys, function(s_key) {
								if (s_key != "properties" && s_key != "plugin") {
									o_complete[s_key] = o_meta[s_key];
								}
							});
							Ext.Array.each(a_propkeys, function(s_key) {
								if (!o_complete[s_key]) {
									o_complete[s_key] = o_meta.properties[s_key];
								}
							});
							o_complete.type = o_complete.dataType || o_complete.datatype || o_complete.type || "varchar";
							o_complete.title = o_complete.label || o_complete.title || o_complete.text || o_complete.header || "";
							o_complete.nulls = o_complete.nulls || 1;

							i_record.set(o_complete);
						}
						else {
							i_record.set({
								name: o_meta.dataIndex,
								len: o_meta.len,
								title: o_meta.text,
								type: o_meta.datatype,
								dec: o_meta.dec,
								nulls: 1
							});

							var s_type = i_record.get("type");

							var o_template = item.types[s_type];
							if (o_template && o_template.defaults) {
								for (var name in o_template.defaults) {
									i_record.set(name, o_template.defaults[name]);
								}
							}
						}
					}

					item.updateValues(i_record);
				}
			}
		});

		i_self.setClones(o_clones);
	},
	getRecord: function() {

		var i_self = this;
		return i_self.record;
	},
	getColumnType: function(o_date) {
		return (o_date.from) ? "extention" : (o_date.like) ? "like" : (o_date.type) ? o_date.type : "new";
	},
	updateValues: function(record) {

		var me = this;
		var disabledProperties = [];
		var type = me.getColumnType(record.data);
		var o_template = me.getClones()[type];

		me.propertyNames = {};
		me.properties = {};

		if (o_template) {
			switch (type) {
				case "like":
					var baseDatatype = me.types[record.data.like];
					for (var name in baseDatatype) {
						var field = {};
						field.name = name;
						o_template.properties.push(field);
						record.data[name] = baseDatatype[name];
						disabledProperties.push(name);
					}
					break;
			}
			for (var key in o_template.properties) {
				var name = o_template.properties[key].name;
				me.propertyNames[name] = (ip2.i18n(name)) ? ip2.i18n(name) : name;
				var boolean = false;
				switch (name) {
					case "syncBoth" :
					case "isRowId" :
					case "isTitle" :
					case "isMandatory" :
					case "protected":
					case "inheritance":
						boolean = true;
						break;
					default:
						break;
				}
				var value;

				if (name == "title") {
					// value = ip2.i18n(record.data[name]);
					// value = Ext.JSON.encode(record.data[name]) ; // The property grid dont work with objects
					value = record.data[name];
				}
				else {
					value = (name == "type" && type == "extention") ? type : (record.data[name]) ? record.data[name] : (boolean) ? false : "";
				}

				me.properties[name] = value;

				var editor = me.customEditors[name];
				if (editor) {
					editor.disabled = false;
				}
			}
		}

		me.suspendEvents();
		me.setSource(me.properties);
		me.resumeEvents();

		me.closeProperties(disabledProperties);
		me.fireEvent("propertyupdate", me);
	},
	closeProperties: function(properties) {
		var me = this;

		for (var i = 0; i < properties.length; i++) {
			var editor = me.customEditors[properties[i]];
			if (editor) {
				editor.disabled = true;
			}
		}
	},

	getCellEditor: function(i_record, i_column) {

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

		if (i_editor) {
			if (!(i_editor instanceof Ext.grid.CellEditor)) {
				if (!(i_editor instanceof Ext.form.field.Base)) {
					i_editor = Ext.ComponentManager.create(i_editor, "textfield");
				}
				i_editor = i_self.setConfigProp(s_property, "editor", new Ext.grid.CellEditor({
					field: i_editor
				}));
			}
		}
		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.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 {
			i_editor = o_editors.string;
		}

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

			var s_table = i_self.record.get("table");
			var s_oldtable;
			var s_split;

			if (s_table) {

				s_split = s_table.split("/");
				s_table = s_split.pop();
				s_oldtable = i_editor.field.options.file;
				i_editor.field.options.file = s_table;

				if (i_editor.field.isLoaded && s_oldtable != s_table) {

					i_record.set({
						value: ""
					});

					var i_store = i_editor.field.getStore();
					i_store.load();
				}
			}
		}

		i_editor.editorId = s_property;
		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() {

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

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

			if (!i_editor.isChanged) {

				i_editor.isChanged = true;

				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;
	}
  });
