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

	extend: "Ext.panel.Panel",

	alias: [
		"widget.ip2DomainModelEntity"
	],

	width: 150,

	height: 100,

	minHeight: 40,

	resizable: true,

	maximized: false,

	collapsible: true,

	collapsed: false,

	isDragging: false,

	wasDragging: false,

	config: {
		designer: null,
		required: false,
		entity: null,
		grid: null,
		lastModified: null
	},

	layout: {
		type: "fit"
	},

	header: {
		listeners: {
			dblclick: {
				fn: function(i_header) {
					var i_panel = i_header.up("panel");
					i_panel.fireEvent("edit", i_panel);
				}
			}
		}
	},

	draggable: {
		insertProxy: false,
		onDrag: function(e) {

			var i_self = this;

			var i_element = i_self.el;
			var i_panel = i_self.panel;

			var o_entity = i_panel.getEntity();

			i_panel.isDragging = true;
			i_panel.wasDragging = false;

			var i_designer = i_panel.designer;

			var a_xy = i_element.getLocalXY();

			o_entity.designer.x = a_xy[0];
			o_entity.designer.y = a_xy[1];

			i_self.x = a_xy[0];
			i_self.y = a_xy[1];

			if (i_designer.svg) {

				var a_connections = i_designer.svg.connections;

				Ext.Array.each(a_connections, function(o_connection) {

					var a_slaves = o_connection.slaves;
					var b_found = false;

					Ext.Array.each(a_slaves, function(o_slave) {
						if (o_slave.path.id) {
							b_found = true;
							return false;
						}
					});

					if (b_found) {

						o_connection.hide();
						o_connection.show();
					}
				});
			}
		},
		endDrag: function(e) {

			var i_self = this;

			var el = i_self.el;
			var i_panel = i_self.panel;
			var i_designer = i_panel.designer;

			i_panel.isDragging = false;
			i_panel.wasDragging = true;
			i_self.panelProxy.hide();
			i_panel.saveState();

			if (i_designer.svg) {

				var a_connections = i_designer.svg.connections;

				Ext.Array.each(a_connections, function(o_connection) {

					var a_slaves = o_connection.slaves;
					var b_found = false;

					Ext.Array.each(a_slaves, function(o_slave) {
						if (o_slave.path.id) {
							b_found = true;
							return false;
						}
					});

					if (b_found) {

						o_connection.hide();
						o_connection.show();
					}
				});
			}
			Ext.Function.defer(function() {
				i_panel.wasDragging = false;
			}, 100);
		}
	},

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

				var i_self = i_panel;
				var o_entity = i_self.getEntity();
				var i_designer = i_self.getDesigner();

				var a_items = [];

				var i_edit = Ext.widget("menuitem", {
					iconCls: "pf-edit",
					text: ip2.i18n({
						da: "Redigér",
						en: "Edit"
					}),
					handler: function(i_item) {
						i_self.fireEvent("edit", i_self);
					}
				});

				var i_delete = Ext.widget("menuitem", {
					iconCls: "pf-delete",
					text: ip2.i18n({
						da: "Slet",
						en: "Delete"
					}),
					enable: !i_self.getRequired(),
					handler: function(i_item) {

						ip2.messageBox({
							title: ip2.i18n({
								da: "Bekræft slet",
								en: "Confirm Delete"
							}),
							message: ip2.i18n({
								da: "Er du sikker på du vil slette..?",
								en: "Are you sure you want to delete"
							}),
							yesText: ip2.i18n({
								da: "Ja",
								en: "Yes"
							}),
							noText: ip2.i18n({
								da: "Nej",
								en: "No"
							}),
							yesFn: function () {
								i_self.fireEvent("removeEntity", i_self, o_entity);
							}
						});
					}
				});

				var i_saveApplication = Ext.widget("menuitem", {
					iconCls: "pf-floppy_disk",
					text: ip2.i18n({
						da: "Gem applikation",
						en: "Save application"
					}),
					enable: !i_self.getRequired(),
					handler: function(i_item) {
						i_self.onSaveApplication();
					}
				});

				var i_buildAndSave = Ext.widget("menuitem", {
					iconCls: "pf-link",
					text: ip2.i18n({
						da: "Gem og byg",
						en: "Save and build"
					}),
					enable: !i_self.getRequired(),
					handler: function(i_item) {
						i_self.onSaveAndBuild();
					}
				});

				a_items.push(i_edit);
				a_items.push(i_buildAndSave);
				a_items.push(i_saveApplication);
				a_items.push(i_delete);

				var i_menu = Ext.widget("menu", {

					items: a_items
				});

				var i_header = i_self.getHeader();

				var i_button = Ext.widget("button", {
					iconCls: "pf-arrow_down",
					menu: i_menu,
					itemId: "menubutton",
					arrowVisible: false,
					listeners: {
						afterrender: {
							fn: function(i_button) {
								i_button.el.dom.style.setProperty("border", "none", "important");
							}
						}
					}
				});

				i_header.items.insert(0, i_button);

				Ext.QuickTips.init();

				var i_tpl = Ext.create("Ext.XTemplate",
					"<tpl for=\".\">",
						"<div><span>Entity: <strong>{entity}</strong></span></div>",
						"<div><span>" + ip2.i18n({ da: "Sidste ændring", en: "Last modified" }) + ": <strong>{lastmodified}</strong></span></div>",
						"<tpl if=\"this.hasValue(values.file)  == true\">",
							"<div><span>" + ip2.i18n({ da: "DB2 Filnavn", en: "DB2 Filename" }) + ": <strong style='color:red;'>{file}</strong></span></div>",
						"</tpl>",
					"</tpl>",
					{
						hasValue: function(value) {
							return value ? true : false;
						}
					}
				);

				i_tpl.compile();

				var i_tooltip = Ext.create("Ext.tip.ToolTip", {
					target: i_self.getHeader().getEl(),
					html: "",
					listeners: {
						// Change content dynamically depending on which element triggered the show.
						beforeshow: {
							fn: function (i_tip) {

								var o_entity = i_self.getEntity();
								var i_date;
								var s_lastmodified = "N/A";

								if (Ext.isNumber(i_self.getLastModified())) {
									i_date = new Date(i_self.getLastModified());
								}

								if (i_date) {
									s_lastmodified = Ext.Date.format(i_date, Ext.util.Format.dateFormat + " H:i:s");
								}

								var o_values = {
									entity: o_entity.name,
									lastmodified: s_lastmodified,
									file: o_entity.file
								};

								var s_value = i_tpl.apply(o_values);
								i_tip.update(s_value);
							}
						}
					}
				});

				var i_relationButton = Ext.widget("button", {
					iconCls: "pf-link",
					itemId: "btn-relation",
					ui: "function-action",
					tooltip: ip2.i18n({
						da: "Vis relationer",
						en: "Show relations"
					}),
					handler: function() {

					}
				});

				i_header.items.add(i_relationButton);
			},
			scope: "this"
		}
	},

	requires: [
		"Portfolio.designer.ui.domainModelEntityColumnGridSmall",
		"Portfolio.designer.ui.domainModelEntityManager"
	],

	initComponent: function() {

		var i_self = this;
		i_self.changed = false;

		var o_entity = i_self.getEntity();
		var i_designer = i_self.getDesigner();

		i_self.name = o_entity.name;
		i_self.title = ip2.i18n((o_entity.title) ? o_entity.title : o_entity.name);

		if (!o_entity.designer) {
			o_entity.designer = i_designer.createSettings();
		}

		i_self.x = o_entity.designer.x;
		i_self.y = o_entity.designer.y;
		i_self.width = (o_entity.designer.width) ? o_entity.designer.width : i_designer.settings.entities.size[0];
		i_self.height = (o_entity.designer.height) ? o_entity.designer.height : i_designer.settings.entities.size[1];

		i_self.orginalWidth = i_self.width;
		i_self.orginalHeight = i_self.height;

		i_self.callParent();

		if (o_entity.columns) {

			var i_grid = Ext.widget("ip2DomainModelEntityColumnGridSmall", {
				entity: o_entity,
				listeners: {
					itemdblclick: {
						fn: function(i_view, i_record) {
							i_self.onEdit(i_record.get("name"));
						}
					}
				}
			});

			i_self.setGrid(i_grid);
			i_self.add(i_grid);
		}

		i_self.enableBubble("removeEntity");
	},

	onSaveApplication: function() {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var o_entity = i_self.getEntity();
		i_designer.doSaveApplication(o_entity.name);
	},

	onSaveAndBuild: function() {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var o_entity = i_self.getEntity();
		i_designer.doSaveAndBuild(o_entity.name);
	},

	onEdit: function(s_colum) {

		var i_self = this;
		var i_designer = i_self.getDesigner();
		var o_model = i_designer.model;
		var s_modelId = i_designer.dmdFile.split("/").pop() || "";
		var o_entity = i_self.getEntity();
		var o_size = ip2.getWindowSize();

		var i_window = Ext.widget("ip2DomainModelEntityManager", {
			designer: i_self.getDesigner(),
			portalEntityManager: i_self,
			entity: o_entity,
			entityName: o_entity.name,
			model: o_model || "",
			modelId: s_modelId,
			required: i_self.getRequired() || false,
			width: o_size.width * 0.9,
			height: o_size.height * 0.9,
			columnToEdit: s_colum || ""
		});

		i_window.on({
			saveModel: {
				fn: function(i_window, o_entity) {

					o_entity.designer.lastmodified = Number(new Date());

					i_self.setLastModified(o_entity.designer.lastmodified);

					i_self.setEntity(o_entity);

					i_self.update(o_entity);

					var o_found = Ext.Array.findBy(o_model.entities, function(o_value) {
						return o_value.name == o_entity.name;
					});

					Ext.apply(o_found, o_entity);
				}
			}
		});

		i_window.show();
	},

	update: function(o_entity) {

		var i_self = this;
		i_self.setEntity(o_entity);
	},
	setChanged: function(o_value) {
		var s_value = ip2.i18n(o_value) || this.getTitle();
		this.setTitle(s_value);
		this.changed = true;
	}
});
