Ext.define("Portfolio.designer.ui.views.form.view.Manager", {

	extend: "Ext.panel.Panel",

	layout: "fit",

	alias: "widget.designer.ui.views.form.view.manager",

	config: {
		designer: null,
		entityManager: null,
		entity: null,
		forms: null,
		tabPanel: null,
		actionbar: null,
		required: false
	},

	requires: [
		"Portfolio.designer.ui.domainModelEntityFormDesignerManager"
	],

	listeners: {
		deleteView: {
			fn: function(i_panel) {

				var i_self = this;
				var s_view = i_panel.getView();
				var a_forms = i_self.getForms();
				var a_remove = [];

				Ext.Array.each(a_forms, function(i_panel, index) {
					if (i_panel.getView() == s_view) {
						a_remove.push(i_panel);
						return false;
					}
				});

				Ext.Array.each(a_remove, function(i_remove) {
					Ext.Array.remove(a_forms, i_remove);
				});

				i_self.setForms(a_forms);
			}
		},
		newColumns: {
			fn: function(i_manager, n_columns) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("newColumns", i_panel, n_columns);
				});
			}
		},
		newRelations: {
			fn: function(i_manager, n_relations) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("newRelations", i_panel, n_relations);
				});
			}
		},
		newRelations: {
			fn: function(i_manager, n_relations) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("newAffiliations", i_panel, n_relations);
				});
			}
		},
		entityChange: {
			fn: function(i_manager, o_values) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("entityChange", i_panel, o_values);
				});
			}
		},
		updateRelation: {
			fn: function(i_manager, i_record) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("updateRelation", i_panel, i_record);
				});
			}
		},
		updateColumn: {
			fn: function(s_old, s_new) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function (i_form) {
					i_form.fireEvent("updateColumn", s_old, s_new);
				});
			}
		},
		removeColumn: {
			fn: function(s_column) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function (i_form) {
					i_form.fireEvent("removeColumn", s_column);
				});
			}
		},
		removeAffiliation: {
			fn: function(s_name) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function (i_form) {
					i_form.fireEvent("removeAffiliation", s_name);
				});
			}
		},
		removeRelation: {
			fn: function(i_manager, s_entity) {

				var i_self = this;
				var a_forms = i_self.getForms();

				Ext.Array.each(a_forms, function(i_panel) {
					i_panel.fireEvent("removeRelation", i_panel, s_entity);
				});
			}
		}
	},

	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);
		i_self.addDocked(i_self.createToolbar());
		i_self.add(i_self.createLayout());

		return i_self;
	},

	createLayout: function() {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var i_entityManager = i_self.getEntityManager();
		var o_entity = i_self.getEntity();

		if (!o_entity.ui) {
			o_entity.ui = {};
		}
		if (!o_entity.ui.forms) {
			o_entity.ui.forms = {};
		}
		if (!o_entity.ui.forms.portfolio) {
			o_entity.ui.forms.portfolio = {
				columns: []
			};
		}

		var a_items = [];

		var i_tabpanel = Ext.widget("tabpanel", {
			layout: "fit",
			plugins: [
				"tabreorderer"
			]
		});

		Ext.Object.each(o_entity.ui.forms, function(s_key) {

			var i_panel = Ext.widget("ip2DomainModelEntityFormDesignerManager", {
				title: s_key,
				designer: i_designer,
				manager: i_self,
				entityManager: i_entityManager,
				entity: o_entity,
				view: s_key,
				required: i_self.getRequired(),
				closable: s_key != "portfolio",
				listeners: {
					beforeclose: {
						fn: function(i_panel) {

							ip2.messageBox({
								icon: Ext.MessageBox.INFO,
								title: ip2.i18n({
									da: "Advarrelse",
									en: "Warning"
								}),
								message: ip2.i18n({
									da: "<strong>Vil du slette dette view...<strong>",
									en: "<strong>Do you want to delete this view...<strong>"
								}),
								yesFn: function() {
									i_self.fireEvent("deleteView", i_panel);
									i_panel.destroy();
								},
								yesText: ip2.i18n({
									da: "Ok",
									en: "OK"
								}),
								cancelText: ip2.i18n({
									da: "Annuller",
									en: "Cancel"
								})
							});

							return false;
						},
						scope: this
					}
				}
			});

			i_tabpanel.add(i_panel);
			a_items.push(i_panel);
		});

		i_self.setForms(a_items);
		i_self.setTabPanel(i_tabpanel);

		i_tabpanel.setActiveTab(0);

		return i_tabpanel;
	},

	createView: function(s_name) {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var i_entityManager = i_self.getEntityManager();
		var o_entity = i_self.getEntity();
		var i_tabpanel = i_self.getTabPanel();
		var a_forms = i_self.getForms();
		var s_key = s_name.toLowerCase();

		var i_panel = Ext.widget("ip2DomainModelEntityFormDesignerManager", {
			title: s_key,
			designer: i_designer,
			manager: i_self,
			entityManager: i_entityManager,
			entity: o_entity,
			view: s_key,
			required: i_self.getRequired(),
			closable: true,
			listeners: {
				beforeclose: {
					fn: function(i_panel) {

						ip2.messageBox({
							icon: Ext.MessageBox.INFO,
							title: ip2.i18n({
								da: "Advarrelse",
								en: "Warning"
							}),
							message: ip2.i18n({
								da: "<strong>Vil du slette dette view...<strong>",
								en: "<strong>Do you want to delete this view...<strong>"
							}),
							yesFn: function() {
								i_self.fireEvent("deleteView", i_panel);
								i_panel.destroy();
							},
							yesText: ip2.i18n({
								da: "Ok",
								en: "OK"
							}),
							cancelText: ip2.i18n({
								da: "Annuller",
								en: "Cancel"
							})
						});

						return false;
					},
					scope: this
				}
			}
		});

		a_forms.push(i_panel);
		var i_active = i_tabpanel.add(i_panel);
		i_tabpanel.setActiveTab(i_active);
	},

	createToolbar: function() {

		var i_self = this;

		var i_toolbar = Ext.widget("toolbar", {
			dock: "top",
			items: [{
				xtype: "button",
				text: ip2.i18n({
					da: "Tilføj view",
					en: "Add view"
				}),
				iconCls: "pf-add",
				disabled: i_self.getRequired(),
				handler: function(i_button) {

					var i_window = Ext.widget("window", {
						width: 400,
						title: ip2.i18n({
							da: "Nyt view",
							en: "New view"
						}),
						layout: "fit",
						modal: true,
						items: [
							{
								xtype: "form",
								layout: "anchor",
								bodyPadding: 10,
								defaults: {
									anchor: "100%"
								},
								items: [
									{
										xtype: "textfield",
										fieldLabel: "Name",
										name: "view"
									}
								]
							}
						],
						buttons: [
							{
								xtype: "button",
								text: "Ok",
								handler: function(i_button) {

									var i_window = i_button.up("window");
									var i_panel = i_window.down("form");
									var i_form = i_panel.getForm();
									var i_field = i_form.findField("view");
									i_window.close();

									i_self.createView(i_field.getValue());
								}
							},
							{
								xtype: "button",
								text: "Cancel",
								handler: function(i_button) {

									var i_window = i_button.up("window");
									i_window.close();
								}
							}
						]
					});

					i_window.show();
				}
			}]
		});

		i_self.setActionbar(i_toolbar);
		return i_toolbar;
	},

	doSave: function(o_entity) {

		var i_self = this;
		var a_forms = i_self.getForms();

		if (!o_entity.ui) {
			o_entity.ui = {};
		}

		o_entity.ui.forms = {};

		Ext.Array.each(a_forms, function(i_panel) {

			o_entity.ui.forms[i_panel.getView()] = i_panel.doSave(null, true);
		});

		return o_entity;
	}
});
