ip2.declare("Portfolio.userRolePreview", {
	xtype: "container",
	require: [
		"Ext.ux.navigation.controller.UserRole",
		"Ext.ux.navigation.view.UserRolePanel",
		"Ext.ux.navigation.model.RolePermissionTree",
		"Ext.ux.navigation.viewmodel.RolePermissionTree",
		"Ext.ux.navigation.controller.RolePermissionTree",
		"Ext.ux.navigation.view.RolePermissionTree",
		"Ext.ux.navigation.controller.RolePermissionMenuTree",
		"Ext.ux.navigation.view.RolePermissionMenuTree",
		"Ext.ux.navigation.controller.RolePermissionNavigationTree",
		"Ext.ux.navigation.view.RolePermissionNavigationTree"
	],
	layout: "fit",
	onInitialize: function(widget, owner) {

		var i_self = this;

		var i_panel = Ext.widget("panel", {
			layout: {
				type: "hbox",
				align: "stretch"
			}
		});

		var i_record = owner.dataParent.getRecord();

		var i_userrole = Ext.widget("userrolepanel", {
			title: ip2.i18n({
				da: "Roller",
				en: "Roles"
			}),
			record: owner.dataParent.getRecord(),
			oldID: owner.dataParent.oldID,
			entity: widget.entity,
			flex: 1,
			split: true,
			autoScroll: true,
			collapsible: true,
			collapseDirection: "left"
		});

		i_panel.add(i_userrole);

		i_userrole.on({
			rolechange: {
				fn: function(item) {

					var s_msg = ip2.i18n({
						da: "Opdater....",
						en: "Updating..."
					});

					i_self.setLoading(s_msg);

					var a_roles = item.getController().getRoles();

					i_self.updatePermissions(a_roles, function(a_permission) {

						i_self.updateNavigationPermissions(a_roles, function(a_navigation) {

							if (i_self.treeMenu) {
								i_self.treeMenu.getController().doReload(a_permission, a_roles);
							}
							if (i_self.treeNavigation) {
								i_self.treeNavigation.getController().doReload(a_navigation);
							}
							if (i_self.treePermission) {
								i_self.treePermission.getController().doReload(a_roles);
							}
							i_self.setLoading(false);
						});
					});
				},
				buffer: 400
			},
			rolesLoaded: {
				fn: function(item) {
					var a_roles = item.getController().getRoles();
					i_self.addPermissions(i_panel, a_roles);
				}
			}
		});

		widget.userRolePanel = i_userrole;

		widget.add(i_panel);
	},

	doSave: function(f_callback) {

		var i_widget = this;
		var i_userRolePanel = i_widget.userRolePanel;
		i_userRolePanel.record = i_widget.ownerCt.dataParent.getRecord();
		i_userRolePanel.getController().doSave();

		f_callback && f_callback();
	},

	doDelete: function(f_callback) {

		var i_widget = this;
		var i_userRolePanel = i_widget.userRolePanel;
		i_userRolePanel.getController().doDelete();

		f_callback && f_callback();
	},

	isDirty: function() {
		return this.ownerCt.isDirty();
	},

	addPermissions: function(i_panel, a_roles) {

		var i_self = this;
		var i_userRolePanel = i_self.userRolePanel;
		var i_model = Ext.ClassManager.get("Ext.ux.navigation.model.RolePermissionTree");
		var i_record = i_model.create({});

		i_record.set(i_record.idProperty, a_roles[0]);

		i_self.updatePermissions(a_roles, function(a_permission) {

			i_self.updateNavigationPermissions(a_roles, function(a_navigation) {

				var b_data = false;
				var b_menu = false;
				var b_navigation = false;

				var a_items = [];

				if (ip2.isAccessible("app.portfolio.functions.navusret00.widgets.userRolePreview.data")) {

					var i_treePermission = Ext.widget("rolepermissiontree", {
						title: ip2.i18n({
							da: "Data & Funktioner",
							en: "Data & Functions"
						}),
						flex: 2,
						split: true,
						registry: "navrolprm",
						rolerecord: i_record,
						roles: a_roles
					});

					var i_treeViewPermission = i_treePermission.getView();

					i_treeViewPermission.onCheckChange = function(i_node) {
						return false;
					};

					i_self.treePermission = i_treePermission;

					a_items.push(i_treePermission);
					b_data = true;
				}

				if (ip2.isAccessible("app.portfolio.functions.navusret00.widgets.userRolePreview.menu")) {
					var i_treeMenu = Ext.widget("rolepermissionmenutree", {
						title: ip2.i18n({
							da: "Menu",
							en: "Menu"
						}),
						record: i_record,
						permissions: a_permission,
						split: true,
						flex: 2
					});

					var i_treeMenuView = i_treeMenu.getView();

					i_treeMenuView.onCheckChange = function(i_node) {
						return false;
					};

					i_self.treeMenu = i_treeMenu;

					a_items.push(i_treeMenu);
					b_menu = true;
				}

				if (ip2.isAccessible("app.portfolio.functions.navusret00.widgets.userRolePreview.navigation")) {

					var i_treeNavigation = Ext.widget("rolepermissionnavigationtree", {
						title: ip2.i18n({
							da: "Navigation",
							en: "Navigation"
						}),
						record: i_record,
						permissions: a_navigation,
						split: true,
						flex: 2
					});

					var i_treeNavigationView = i_treeNavigation.getView();

					i_treeNavigationView.onCheckChange = function(i_node) {
						return false;
					};

					i_self.treeNavigation = i_treeNavigation;

					a_items.push(i_treeNavigation);
					b_navigation = true;
				}

				Ext.suspendLayouts();

				i_panel.add(a_items);

				Ext.resumeLayouts(true);
			});
		});
	},

	updatePermissions: function(a_roles, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: "POST",
			jsonData: {
				action: "ip2NavProxy.getRoleMenuPermission",
				roletoken: a_roles
			},
			faliure: function() {
				var a_result = [];
				f_callback(a_result);
			},
			success: function(response, request) {
				var a_result;
				try {
					a_result = JSON.parse(response.responseText);
				}
				catch (err) {
					a_result = [];
				}
				f_callback(a_result);
			}
		});
	},
	updateNavigationPermissions: function(a_roles, f_callback) {

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: "POST",
			jsonData: {
				action: "ip2NavProxy.getRoleNavigationPermission",
				roletoken: a_roles
			},
			faliure: function() {
				var a_result = [];
				f_callback(a_result);
			},
			success: function(response, request) {
				var a_result;
				try {
					a_result = JSON.parse(response.responseText);
				}
				catch (err) {
					a_result = [];
				}

				f_callback(a_result);
			}
		});
	}
});
