Ext.define("Portfolio.menu.view.Main", {

	xtype: "portfolio-menu",

	extend: "Ext.panel.Panel",

	layout: {
		type: "vbox",
		align: "stretch"
	},

	border: false,
	bodyBorder: false,

	config: {
		systemArea: null,
		favoriteArea: null
	},

	requires: [
		"Portfolio.menu.view.System",
		"Portfolio.menu.view.Favorite"
	],

	listeners: {
		systemActivate: {
			fn: function(s_itemId) {

				var i_self = this;
				var i_system = i_self.down("#" + s_itemId);
				var i_systemArea = i_self.getSystemArea();

				if(i_system) {
					i_systemArea.setActiveItem(i_system);
				}
			}
		},
		aftershortcut: {
			fn: function(s_itemId) {

				var i_self = this;
				var i_system = i_self.down("#" + s_itemId);
				var i_systemArea = i_self.getSystemArea();

				if (i_system) {
					i_systemArea.setActiveItem(i_system);
					i_system.fireEvent("shortcut");
				}
			}
		}
	},

	ui: "menu-navigation",

	initComponent: function() {

		var i_self = this;
		i_self.callParent(arguments);

		i_self.add([
			i_self.createFavoriteArea(),
			i_self.createSystemArea()
		]);

		i_self.loadSystems();

		i_self.enableBubble([
			"startapplication",
			"systemAdded",
			"systemShortcut"
		]);

		return i_self;
	},

	createSystemArea: function() {

		var i_self = this;

		var i_panel = Ext.widget("panel", {
			layout: "card",
			flex: 3,
			border: false
		});

		i_self.setSystemArea(i_panel);

		return i_panel;
	},

	createFavoriteArea: function() {

		var i_self = this;

		var i_favorites = Ext.widget("portfolio-menu-favorite", {
			flex: 1,
			collapsible: true,
			collapsed: false,
			border: false,
			bodyBorder: false,
			split: true
		});

		i_self.setFavoriteArea(i_favorites);

		return i_favorites;
	},

	loadSystems: function() {

		var i_self = this;
		var i_user = Portfolio.util.Handler.getUser();
		var a_items = [];

		var i_systemArea = i_self.getSystemArea();

		var s_token = i_user.get(i_user.idProperty);
		var s_url = "/ip2-services/ip2transaction.aspx";

		Ext.Ajax.request({
			url: s_url,
			jsonData: {
				language: i_user.get("USRLNG") || "en",
				usertoken: s_token,
				action: "ip2NavProxy.listMenuNodes",
				node: "root"
			},
			actionMethods: {
				read: "POST"
			},
			success: function (o_result) {

				var a_systems = Ext.decode(o_result.responseText, true) || [];

				var a_bindings = [];
				var a_tabs = [];

				Ext.Array.each(a_systems, function(o_config, n_index) {

					var i_system;

					var s_title;

					var title = o_config.text || o_config.title;

					var o_properties = o_config.properties || {};

					var o_binding = o_properties.binding;

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

					try {
						if (!/\.(js|widget)/.test(o_config.url)) {

							i_system = Ext.widget("portfolio-menu-system", {
								title: s_title,
								layout: "fit",
								system: o_config,
								border: false,
								bodyBorder: false,
								itemId: "system-" + n_index,
							});
						}
						else {

							i_system = Portfolio.util.Handler.callWidget(o_config);
						}

						if (i_system) {

							i_system.setTitle(s_title);

							i_system.enableBubble("startapplication");

							a_tabs.push({
								title: s_title,
								itemId: "system-" + n_index,
								first: n_index === 0,
								binding: o_binding
							});

							a_items.push(i_system);
						}

						if (o_binding && o_binding.keyCode) {

							o_binding.panel = i_system;

							a_bindings.push(o_binding);
						}
					}
					catch (o_error) {

						// TEST FOR LEGACY
						if (!/(ip2Base\/)/.test(o_config.url)) {

							s_title = ip2.i18n({
								da: s_title + " kunne ikke indlæses",
								en: s_title + " could not be loaded"
							});

							Portfolio.util.Message.Add(s_title, {
								message: o_error.message,
								params: o_config
							}, "js");
						}
						else {

							if (!/\.(js|widget)/.test(o_config.url)) {

								s_title = ip2.i18n({
									da: s_title + " kunne ikke indlæses",
									en: s_title + " could not be loaded"
								});

								Portfolio.util.Message.Add(s_title, {
									message: o_error.message,
									params: o_config
								}, "js");
							}
						}
					}
				});

				i_systemArea.add(a_items);

				i_self.initKeys(a_bindings);

				i_self.fireEvent("systemAdded", a_tabs);
			}
		});
	},

	initKeys: function(a_bindings) {

		var i_self = this;

		var a_result = [];

		Ext.Array.each(a_bindings, function(o_config) {

			var i_panel = o_config.panel;

			var o_binding = {
				alt: o_config.alt,
				ctrl: o_config.ctrl,
				key: o_config.keyCode,
				handler: function(n_key, i_event) {

					i_event.stopEvent();
					i_self.fireEvent("systemShortcut", i_panel.itemId);
				}
			};

			a_result.push(o_binding);
		});

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: Ext.getDoc().el,
			binding: a_result
		});
	}
});
