Ext.define("Portfolio.registry.ui.property.Add", {

	extend: "Ext.window.Window",

	alias: "widget.registry.ui.property.add",

	layout: "fit",

	okFn: Ext.emptyFn,
	parentID: "",
	children: null,

	alternateClassName: [
		"Ext.registry.ui.property.Add"
	],

	initComponent: function() {

		var i_self = this;
		i_self.callParent();

		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;

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

		var i_cmbType = Ext.widget("combobox", {
			fieldLabel: ip2.i18n({
				da: "Type",
				en: "Type"
			}),
			name: "type",
			displayField: "key",
			valueField: "value",
			allowBlank: false,
			store: i_typeStore,
			queryMode: "local",
			minChars: 1,
			triggerAction: "all",
			listeners: {
				afterrender: {
					fn: function(item) {
						setTimeout(function() {
							item.focus(false, 100);
						}, 10);
					},
					single: true
				},
				select: {
					fn: function(i_item, i_record) {

						var i_form = i_self._form;
						var i_keyField = i_form.items.getAt(1);

						if (i_keyField) {
							i_keyField.enable();
						}

						var i_valueField = i_form.items.getAt(2);
						if (i_valueField) {
							i_form.remove(i_valueField, true);
							var i_newField = i_self.createField(i_record.get("value"));
							i_form.add(i_newField);
						}
					}
				}
			}
		});

		var i_form = Ext.widget("form", {
			layout: "anchor",
			trackResetOnLoad: true,
			autoScroll: true,
			bodyPadding: 5,
			defaults: {
				anchor: "100%"
			},
			items: [
				i_cmbType,
				{
					xtype: "textfield",
					name: "key",
					fieldLabel: ip2.i18n({
						da: "Nøgle",
						en: "Key"
					}),
					allowBlank: false,
					disabled: true,
					validator: function(value) {

						var s_error = true;
						var s_key = i_self.parentID + "." + value.toLowerCase();

						Ext.Array.each(i_self.children, function(o_child) {
							var s_childKey = o_child.key.toLowerCase();
							if (s_childKey == s_key) {
								s_error = ip2.i18n({
									da: "Nøgle skal vare unik",
									en: "Key must be uniq"
								});
							}
						});
						return s_error;
					}
				},
				{
					xtype: "displayfield",
					name: "value",
					fieldLabel: ip2.i18n({
						da: "Værdi",
						en: "Value"
					}),
					disabled: true
				}

			],
			listeners: {
				validitychange: {
					fn: function(i_form, b_valid) {
						var i_open = i_self.down("#btn-save");
						if (i_open) {
							if (b_valid) {
								i_open.enable();
							}
							else {
								i_open.disable();
							}
						}
					}
				}
			},
			buttons: i_self.createButtons()
		});

		i_self._form = i_form;

		return i_form;
	},

	getValue: function() {

		var i_self = this;
		var i_form = i_self._form.getForm();
		var o_values = i_form.getValues();
		if (i_self.parentID) {
			o_values.key = i_self.parentID + "." + o_values.key;
		}
		var o_meta = {
			properties: {
				type: o_values.type
			},
			plugin: {
				src: ""
			}
		};
		o_values.meta = o_meta;
		delete o_values.type;
		return o_values;
	},

	getData: function() {

		var a_data = [
			{
				key: "String",
				value: "varchar"
			},
			{
				key: "Integer",
				value: "integer"
			},
			{
				key: "Decimal",
				value: "decimal"
			},
			{
				key: "Bool",
				value: "bool"
			},
			{
				key: "Date",
				value: "date"
			},
			{
				key: "timestamp",
				value: "timestamp"
			}
		];
		return a_data;
	},

	createField: function(s_type) {

		var i_field;
		var s_fieldType = "";
		var o_config = {
			name: "value",
			fieldLabel: ip2.i18n({
				da: "Værdi",
				en: "Value"
			})
		};

		switch (s_type){
			case "string":
			case "char":
			case "varchar":
				s_fieldType = "textfield";
				break;
			case "integer":
			case "int":
			case "decimal":
			case "dec":
			case "float":
			case "numeric":
			case "smallint":
			case "bigint":
				s_fieldType = "numberfield";
				var n_precision = 0;
				if (s_type != "int" || s_type != "integer") {
					n_precision = 2;
				}
				o_config = Ext.Object.merge(o_config, {
					decimalPrecision: n_precision,
					decimalSeparator: Ext.util.Format.decimalSeparator
				});
				break;
			case "timestamp":
			case "time":
				s_fieldType = "isotimestampfield";
				break;
			case "date":
				s_fieldType = "datefield";
				o_config = Ext.Object.merge(o_config, {
					format: "d-m-Y",
					submitFormat: "Y-m-d"
				});
				break;
			case "bool":
				s_fieldType = "checkbox";
				break;
			default :
				s_fieldType = "textfield";
				break;
		}
		i_field = Ext.widget(s_fieldType, o_config);
		return i_field;
	},

	createButtons: function() {

		var i_self = this;
		var a_buttons = [
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Ok",
					en: "OK"
				}),
				itemId: "btn-save",
				disabled: true,
				handler: function() {
					if (i_self.okFn) {
						i_self.okFn(i_self.getValue(), function() {
							i_self.close();
						});
					}
					else {
						i_self.close();
					}
				}
			},
			{
				xtype: "button",
				text: ip2.i18n({
					da: "Luk",
					en: "Close"
				}),
				itemId: "btn-close",
				handler: function() {
					i_self.close();
				}
			}
		];

		return a_buttons;
	}
});
