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

	extend: "Ext.window.Window",

	alias: [
		"widget.ip2DomainModelEntityRelationRowProperties"
	],

	title: ip2.i18n("properties"),

	width: 600,

	height: 400,

	layout: "fit",

	resizable: true,

	modal: true,

	config: {
		entityManager: null,
		record: null,
		designer: null,
		relationGrid: null,
		generalForm: null
	},

	listeners: {
		beforedestroy: {
			fn: function(i_window) {

				var b_result = true;

				var i_formPanel = i_window.getGeneralForm();

				if (i_formPanel.isValid()) {
					b_result = i_formPanel.isDirty();
				}
				return !b_result;
			}
		}
	},

	initComponent: function() {

		var i_self = this;

		i_self.callParent();

		var i_record = i_self.getRecord();
		var o_properties = i_record.get("properties");
		var i_designer = i_self.getDesigner();
		var o_model = i_designer.model;
		var i_entityMananger = i_self.getEntityManager();

		var a_buttons = [
			"->"
		];

		var i_buttonOk = Ext.widget("button", {
			text: ip2.i18n("ok"),
			disabled: true,
			itemId: "button-ok",
			iconCls: "pf-floppy_disk",
			handler: function() {
				i_self.doSave();
				i_self.close();
			}
		});

		var i_buttonCancel = Ext.widget("button", {
			text: ip2.i18n("cancel"),
			itemId: "button-cancel",
			iconCls: "pf-navigate_cross",
			handler: function() {
				i_self.close();
			}
		});

		a_buttons.push(i_buttonOk);
		a_buttons.push(i_buttonCancel);

		var as = i_record.get("as");
		var a_entities = o_model.required;

		var s_title = "";

		if (Ext.isEmpty(as)) {

			if (i_entityMananger.entityName == i_record.get("betweenEntity")) {

				var o_entity = Ext.Array.findBy(a_entities, function(o_value) {
					return o_value.name == i_record.get("andEntity");
				});

				if (o_entity) {
					s_title = ip2.i18n(o_entity.title);
				}
			}
			else {

				var o_entity = Ext.Array.findBy(a_entities, function(o_value) {
					return o_value.name == i_record.get("betweenEntity");
				});

				if (o_entity) {
					s_title = ip2.i18n(o_entity.title);
				}
			}
		}
		else {
			s_title = as;
		}

		var i_generalForm = Ext.widget("form", {
			border: false,
			bodyPadding: 10,
			layout: "anchor",
			trackResetOnLoad: true,
			defaults: {
				labelAlign: "left",
				labelWidth: 100,
				anchor: "100%"
			},
			items: [
				{
					xtype: "textfield",
					fieldLabel: ip2.i18n("as"),
					name: "as"
				},
				{
					xtype: "i18n-textfield",
					fieldLabel: ip2.i18n("title"),
					name: "title"
				},
				{
					xtype: "checkbox",
					fieldLabel: Ext.String.format(
						"Show as Between",
						s_title
					),
					name: "shownAsChildBetween",
					checked: o_properties ? o_properties.shownAsChild.between : true
				},
				{
					xtype: "checkbox",
					fieldLabel: Ext.String.format(
						"Show as And",
						s_and
					),
					name: "shownAsChildAnd",
					checked: o_properties ? o_properties.shownAsChild.and : true
				},
				{
					fieldLabel: "Sql where",
					name: "sqlwhere",
					xtype: "textarea",
					allowBlank: true,
					value: o_properties ? o_properties.sql : ""
				}
			],
			listeners: {
				dirtychange: {
					fn: function(i_form, b_dirty) {

						i_buttonOk[b_dirty ? "enable" : "disable"]();
					}
				},
				validitychange: {
					fn: function(i_form, b_valid) {

						if (!b_valid) {
							i_buttonOk.disable();
						}
						else {
							var b_dirty = i_form.isDirty();
							i_generalForm.fireEvent("dirtychange", i_generalForm, b_dirty);
						}
					}
				}
			}
		});

		var i_form = i_generalForm.getForm();

		i_form.setValues(o_properties || {});

		var o_entity = Ext.Array.findBy(a_entities, function(o_value) {
			return o_value.name == i_record.get("betweenEntity");
		});

		var s_between = ip2.i18n(o_entity.title);

		o_entity = Ext.Array.findBy(a_entities, function(o_value) {
			return o_value.name == i_record.get("andEntity");
		});

		var s_and = ip2.i18n(o_entity.title);

		var i_toolbar = Ext.widget("toolbar", {
			dock: "bottom",
			items: a_buttons
		});

		i_self.addDocked(i_toolbar);

		i_self.add(i_generalForm);

		i_self.setGeneralForm(i_generalForm);
	},

	promptSaveChanges: function(f_calback) {

		var i_self = this;

		f_calback = f_calback || Ext.emptyFn;

		ip2.messageBox({
			title: ip2.i18n("saveChanges"),
			message: ip2.i18n("doYouWantToChange"),
			yesFn: function () {
				if (i_self.doSave()) {
					f_calback.call(i_self);
				}
			},
			noFn: function() {
				f_calback.call(i_self);
			},
			yesText: ip2.i18n({
				da: "Ok",
				en: "OK"
			}),
			cancelText: ip2.i18n({
				da: "Annuller",
				en: "Cancel"
			})
		});
	},

	doSave: function() {

		var i_self = this;

		var i_record = i_self.getRecord();

		var i_formPanel = i_self.getGeneralForm();
		var i_form = i_formPanel.getForm();
		var o_values = i_form.getValues();

		var o_properties = {
			as: o_values.as,
			title: o_values.title,
			shownAsChild: {
				between: o_values.shownAsChildBetween,
				and: o_values.shownAsChildAnd
			},
			sql: o_values.sqlwhere
		};

		i_record.set("properties", o_properties);
		i_record.commit();

		i_self.fireEvent("notifyRelationUpdate", i_record);
	}
});
