Ext.define("Portfolio.entity.controller.Form", {
	extend: "Ext.app.ViewController",

	alias: "controller.portfolio-entity-form",

	onLanguageChange: function(i_formpanel, s_language) {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_view.setLanguageChange(true);
		i_view.doChangeLanguage(s_language);
		i_view.setLanguageChange(false);
	},

	onValidityChange: function(i_formpanel, b_valid) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_actionBar = i_view.getActionbar();

		var i_saveclose = i_actionBar.items.get("saveclose");

		if (!b_valid) {
			i_saveclose.hide();
		}
		else {

			var b_dirty = i_view.isDirty();

			if (b_dirty) {
				i_view.fireEvent("dirtychange", i_view, b_dirty);
			}
		}
	},

	onDirtyChange: function(i_formpanel, b_dirty) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var b_valid = i_view.isValid();

		if (b_valid) {

			var i_actionBar = i_view.getActionbar();

			var i_saveclose = i_actionBar.items.get("saveclose");

			b_dirty = i_view.isDirty();

			if (b_dirty) {
				i_saveclose.show();
			}
			else {
				i_saveclose.hide();
			}
		}
	},

	onBeforeClose: function(i_formpanel) {

		var i_controller = this;
		var i_view = i_controller.getView();

		var b_force = i_view.getForceClose();
		var b_dirty = i_view.isDirty();

		if (!b_force && b_dirty) {

			Portfolio.util.Handler.messageBox({
				closable: true,
				message: Portfolio.util.Handler.i18n({
					da: "Du er ved at lukke uden at gemme!<br>Hvordan vil du forsætte?",
					en: "You are about to close without having saved!<br>How would you like to proceed?"
				}),
				yesText: Portfolio.util.Handler.i18n({
					da: "Gem",
					en: "Save"
				}),
				yesFn: function () {
					i_view.save(function (i_record) {

						i_view.setForceClose(true);
						var i_contextParent = i_view.getContextParent();
						i_contextParent.close();
					});
				},
				noText: Portfolio.util.Handler.i18n({
					da: "Luk",
					en: "Close"
				}),
				noFn: function () {

					i_view.setForceClose(true);

					var i_contextParent = i_view.getContextParent();
					i_contextParent.close();
				}

			});
		}
		else {
			b_force = true;
		}

		return b_force;
	},

	isDirty: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_tabpanel = i_view.getTabPanel();

		var o_modified = i_view.getModified();
		var a_widgets = i_view.getWidgets();
		var o_languageInfo = i_view.getLanguageInfoInformation();

		var b_dirty = false;

		if (i_tabpanel) {

			var a_tabs = i_tabpanel.items.getRange();

			var i_dirty = Ext.Array.findBy(a_tabs, function(i_tab) {

				var i_form = i_tab.getForm ? i_tab.getForm() : null;

				return i_form ? i_form.isDirty() : i_tab.isDirty ? i_tab.isDirty() : false;
			});

			if (i_dirty) {
				b_dirty = true;
			}
		}

		
		if (!b_dirty) {

			if (o_modified && o_languageInfo) {

				var a_languages = i_view.getLanguages();

				Ext.Array.each(a_languages, function(s_lang) {

					var a_modified = o_modified[s_lang] || [];

					if (a_modified.length > 0) {

						Ext.Array.each(a_modified, function(o_field) {
							if (o_field.dirty) {
								b_dirty = o_field.dirty;
								return false;
							}
						});
					}
					if (b_dirty) {
						return false;
					}
				});
			}

			if (!b_dirty) {

				Ext.Array.each(a_widgets, function(o_widget) {

					var i_widget = i_view.down("#" + o_widget.itemId);

					var i_widgetCmp = i_widget.getWidgetCmp();

					if (i_widgetCmp && i_widgetCmp.isDirty) {

						var b_widgetDirty = i_widgetCmp.isDirty();

						if (b_widgetDirty) {

							b_dirty = true;
						}
					}
				});
			}
		}

		return b_dirty;
	},

	isValid: function () {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_tabpanel = i_view.getTabPanel();

		var a_tabs = i_tabpanel.items.getRange();

		var b_valid = true;

		var i_valid = Ext.Array.findBy(a_tabs, function(i_tab) {

			var i_form = i_tab.getForm ? i_tab.getForm() : null;

			return i_form ? !i_form.isValid() : i_tab.isValid ? !i_tab.isValid() : true;
		});

		if (i_valid) {
			b_valid = true;
		}

	
		var o_languageInfo = i_view.getLanguageInfoInformation();
		var o_modified = i_view.getModified();
		var a_widgets = i_view.getWidgets();

		if (b_valid) {

			if (o_modified && o_languageInfo) {

				var a_languages = i_view.getLanguages();

				Ext.Array.each(a_languages, function(s_lang) {

					var a_modified = o_modified[s_lang] || [];

					if (a_modified.length > 0) {

						Ext.Array.each(a_modified, function(o_field) {
							if (o_field.hasOwnProperty("valid")) {
								b_valid = o_field.valid;
								return false;
							}
						});
					}

					if (!b_valid) {
						return false;
					}
				});
			}

			if (b_valid) {

				Ext.Array.each(a_widgets, function(o_widget) {

					var i_widget = i_view.down("#" + o_widget.itemId);

					var i_widgetCmp = i_widget.getWidgetCmp();

					if (i_widgetCmp && i_widgetCmp.isValid) {

						var b_widgetValid = i_widgetCmp.isValid();

						if (!b_widgetValid) {

							b_valid = false;
						}
					}
				});
			}
		}

		return b_valid;
	},

	onDelete: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
		var o_model = i_view.getDmdModel();

		var i_record = i_view.getRecord();
		var i_model = i_view.getModel();
		var i_contextParent = i_view.getContextParent();

		var o_dmdInfo = i_view.getDmdInfo();
		var o_proxy = o_model.proxy;
		var o_entity = o_dmdInfo.entity;

		var i_errorTemplate = new Ext.XTemplate(Portfolio.util.Handler.i18n({
			da: "Der skete en fejl</br>Fejl:</br><strong>{message}</strong>",
			en: "An error occured</br>Error:</br><strong>{message}</strong>"
		}));

		i_errorTemplate.compile();

		Portfolio.util.Handler.messageBox({
			icon: Ext.Msg.WARNING,
			message: Portfolio.util.Handler.i18n({
				da: "Vil du slette DENNE record",
				en: "Do you want to delete THIS record..?"
			}),
			yesText: Portfolio.util.Handler.i18n({
				da: "Ja",
				en: "Yes"
			}),
			yesFn: function () {

				if (o_proxy.portfolio) {

					var a_batch = [{
						type: "delete",
						dmd: i_view.getDmd(),
						entity: o_entity.name,
						key: Portfolio.util.Handler.extractKeys(i_model, i_record)
					}];

					var s_msg = Portfolio.util.Handler.i18n({
						da: "Sletter...",
						en: "Deleting..."
					});

					i_view.setLoading(s_msg, true);

					Ext.Ajax.request({
						url: "/ip2-Services/ip2dmdproxy.aspx",
						jsonData: {
							batch: a_batch
						},
						success: function (o_response) {

							var o_result = Ext.decode(o_response.responseText, true) || {
								success: false
							};

							if (o_result.success) {
								i_view.action = "delete";
								i_view.setForceClose(true);
								i_contextParent.close();
							}
							else {

								Portfolio.util.Handler.messageBox({
									icon: Ext.Msg.WARNING,
									message: i_errorTemplate.apply({
										message: o_result.message
									}),
									yesText: Portfolio.util.Handler.i18n({
										da: "OK",
										en: "OK"
									})
								});
							}
						},
						failure: function (i_error) {


							Portfolio.util.Handler.messageBox({
								icon: Ext.Msg.WARNING,
								message: i_errorTemplate.apply({
									message: i_error.message
								}),
								yesText: Portfolio.util.Handler.i18n({
									da: "OK",
									en: "OK"
								})
							});
						}
					});
				}
				else {
					i_record.erase({
						success: function(i_record, i_operation) {

							i_view.setLoading(false);
							i_view.action = "delete";
							i_view.setForceClose(true);
							i_contextParent.close();
						},
						failure: function(i_record, i_operation) {

							i_view.setLoading(false);

							var i_error = i_operation.getError();
							var i_response = i_error.response;

							var o_response = Ext.decode(i_response.responseText);

							Portfolio.util.Handler.messageBox({
								icon: Ext.Msg.WARNING,
								message: i_errorTemplate.apply({
									message: o_response.message
								}),
								yesText: Portfolio.util.Handler.i18n({
									da: "OK",
									en: "OK"
								})
							});
						}
					});
				}
			},
			noText: Portfolio.util.Handler.i18n({
				da: "Nej",
				en: "No"
			})
		});
	},

	onPreSave: function(i_formpanel) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_content = i_view.getContent();

		var b_dirty = i_view.isDirty();

		i_view.doSave(function(i_record) {
			i_view.updateTitle(i_record);
			i_view.setAction("update");
			i_view.fireEvent("contentSave", i_content, i_view);
			Portfolio.util.Handler.App.fireEvent("contentSave", i_content, i_view);
		});
	},

	onPostSave: function(i_formpanel) {

	},

	onSave: function(i_formpanel) {

	},

	onWidgetSave: function(i_formpanel) {

	}
});
