Ext.define("Portfolio.registry.controller.view.Main", {

	extend: "Ext.app.ViewController",

	alias: "controller.registry.view.main",

	alternateClassName: [
		"Ext.registry.controller.view.Main"
	],

	uses: [
		"Portfolio.registry.view.Detail"
	],

	onEdit: function(i_view, i_selected, item, index, e) {

		var i_controller = this;
		var i_tree = i_controller.getView();
		var i_store = i_tree.getStore();
		var i_proxy = i_store.getProxy();
		var o_store = i_proxy.dataStore;
		var i_node = i_selected;
		var o_options = i_tree.options;
		var a_coords = e.getXY();
		e.preventDefault();

		if ((ip2.isAccessible("app.sysreg.data.update")) ||
			ip2.isAccessible("app.sysreg.data.read") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".update") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".read") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".update") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".read")) {

			Ext.registry.util.Service.getChildNodes(i_tree.options, i_node.data, o_store, function(a_records) {
				i_node.set({
					children: a_records
				});
				var i_window = Ext.widget("registry.view.detail", {
					title: "Edit:",
					width: 800,
					height: 600,
					record: i_node,
					options: o_options,
					tree: i_tree
				});
				i_window.show();
			});
		}
	},

	onItemMenu: function(i_view, i_selected, item, index, e) {

		var i_controller = this;
		var i_tree = i_controller.getView();
		var i_store = i_tree.getStore();
		var i_proxy = i_store.getProxy();
		var o_store = i_proxy.dataStore;
		var i_node = i_selected;
		var o_options = i_tree.options;
		var a_coords = e.getXY();
		e.preventDefault();

		var a_items = [];

		if ((ip2.isAccessible("app.sysreg.data.update")) ||
			ip2.isAccessible("app.sysreg.data.read") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".update") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".read") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".update") ||
			ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".read")) {

			a_items.push({
				xtype: "menuitem",
				iconCls: "pf-edit",
				text: ip2.i18n({
					da: "Redigér",
					en: "Edit"
				}),
				disabled: !o_options.library || !o_options.file,
				handler: function() {
					Ext.registry.util.Service.getChildNodes(i_tree.options, i_node.data, o_store, function(a_records) {
						i_node.set({
							children: a_records
						});
						var i_window = Ext.widget("registry.view.detail", {
							title: ip2.i18n({
								da: "Redigér",
								en: "Edit"
							}),
							width: 800,
							height: 600,
							record: i_node,
							options: o_options,
							tree: i_tree
						});
						i_window.show();
					});
				}
			});
		}

		if (!i_selected.isRoot()) {

			if ((ip2.isAccessible("app.sysreg.data.copy")) ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".copy") ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".copy")) {

				a_items.push({
					xtype: "menuitem",
					iconCls: "pf-edit",
					text: ip2.i18n({
						da: "Kopier",
						en: "Copy"
					}),
					disabled: !o_options.library || !o_options.file,
					handler: function() {
						var i_window = Ext.widget("registry.view.copy", {
							title: ip2.i18n({
								da: "Kopier",
								en: "Copy"
							}),
							width: 600,
							record: i_node,
							options: o_options,
							tree: i_tree
						});
						i_window.show();
					}
				});
			}

			if ((ip2.isAccessible("app.sysreg.data.move")) ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".move") ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".move")) {

				a_items.push({
					xtype: "menuitem",
					iconCls: "pf-edit",
					text: ip2.i18n({
						da: "Flyt",
						en: "Move"
					}),
					disabled: !o_options.library || !o_options.file,
					handler: function() {
						var i_window = Ext.widget("registry.view.move", {
							title: ip2.i18n({
								da: "Flyt",
								en: "Move"
							}),
							width: 600,
							record: i_node,
							options: o_options,
							tree: i_tree
						});
						i_window.show();
					}
				});
			}

			if ((ip2.isAccessible("app.sysreg.data.delete")) ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".delete") ||
				ip2.isAccessible("app.sysreg.data.schema." + o_options.library + ".table." + o_options.file + ".delete")) {

				a_items.push({
					xtype: "menuitem",
					iconCls: "pf-delete",
					text: ip2.i18n({
						da: "Slet",
						en: "Delete"
					}),
					disabled: (!o_options.library || !o_options.file) || i_node.isRoot(),
					handler: function() {
						ip2.messageBox({
							width: 300,
							title: ip2.i18n({
								da: "Slet..?",
								en: "Delete..?"
							}),
							message: ip2.i18n({
								da: "Vil du slette<strong> " + i_node.get("text") + "</strong>",
								en: "Do you want to delete<strong> " + i_node.get("text") + "</strong>"
							}),
							yesFn: function () {
								Ext.registry.util.Service.deleteEntry(o_options, i_node.data, o_store, function() {
									var i_store = i_tree.getStore();
									if (!i_node.isRoot()) {
										i_node = i_node.parentNode;
									}
									i_node.removeAll(false);
									i_store.load({
										node: i_node
									});
								});
							},
							yesText: ip2.i18n({
								da: "Ja",
								en: "Yes"
							}),
							noText: ip2.i18n({
								da: "Nej",
								en: "No"
							})
						});
					}
				});
			}
		}

		a_items.push({
			xtype: "menuitem",
			iconCls: "pf-refresh",
			text: ip2.i18n({
				da: "Opfrisk",
				en: "Refresh"
			}),
			disabled: !o_options.library || !o_options.file,
			handler: function() {
				var i_store = i_tree.getStore();
				i_node.removeAll(false);
				i_store.load({
					node: i_node
				});
			}
		});

		var i_menu = Ext.widget("menu", {
			items: a_items
		});

		i_menu.showAt(a_coords[0], a_coords[1]);
	}
});
