Ext.define("Ext.ux.navigation.controller.RolePermissionNavigationTree", {
	extend: "Ext.app.ViewController",

	alias: "controller.navigation.rolepermissionnavigationtree",

	doSave: function(f_callback) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_record = i_viewModel.get("record");
		var i_store = i_view.getStore();

		var a_permissions = [];
		var a_nodes = i_view.getChecked();

		Ext.Array.each(a_nodes, function(i_node) {
			a_permissions.push(i_node.get("ID"));
		});

		f_callback = f_callback || Ext.emptyFn;

		Ext.Ajax.request({
			url: "/ip2-services/ip2transaction.aspx.json",
			method: "POST",
			jsonData: {
				action: "ip2NavProxy.updateRoleNavigationPermission",
				roletoken: i_record.get(i_record.idProperty),
				permissions: a_permissions
			},
			success: function() {
				i_controller.resetDirty(function() {
					f_callback();
				});
			}
		});
	},

	doDelete: function(f_callback) {

		var i_controller = this;
		var i_view = i_controller.getView();

		f_callback = f_callback || Ext.emptyFn;

		f_callback();
	},

	nodeCheckChange: function(i_node, b_checked) {

		var i_controller = this;
		var i_view = i_controller.getView();

		i_node.cascadeBy(function(i_child) {
			i_controller.checkAndLoad(i_child, b_checked);
		});

		var i_parent = i_node.parentNode;
		while (i_parent && i_parent.get("checked") != b_checked && !i_parent.isRoot()) {

			var a_childNodes = i_parent.childNodes || [];
			var b_hasChecked = false;
			if (!b_checked) {
				Ext.Array.each(a_childNodes, function(i_child) {
					if (i_child.get("checked")) {
						b_hasChecked = true;
						return false;
					}
				});
				if (!b_hasChecked) {
					if (!i_parent.dirty) {
						i_parent.oldChecked = i_parent.get("checked");
						i_parent.dirty = true;
					}
					else {
						i_parent.oldChecked = false;
						i_parent.dirty = false;
					}
					i_parent.set("checked", false);
				}
			}
			else {
				if (!i_parent.dirty) {
					i_parent.oldChecked = i_parent.get("checked");
					i_parent.dirty = true;
				}
				else {
					i_parent.oldChecked = true;
					i_parent.dirty = false;
				}
				i_parent.set("checked", true);
			}

			i_parent = i_parent.parentNode;
		}
	},

	checkAndLoad: function(i_node, b_checked) {

		var i_controller = this;
		var i_tree = i_controller.getView();
		var i_treeView = i_tree.getView();
		if (!i_node.isLeaf() && !i_node.isLoaded()) {
			i_node.expand(false, function() {
				i_node.set("loaded", true);
				setTimeout(function() {
					i_controller.nodeCheckChange(i_node, b_checked);
				}, 10);
			});
		}
		else {
			if (i_node.get("checked") != b_checked) {
				i_treeView.onCheckChange(i_node);
			}
		}
	},

	resetDirty: function(f_callback) {

		var i_controller = this;

		var i_view = i_controller.getView();
		var i_store = i_view.getStore();
		var a_nodes = i_store.getData().items;

		Ext.Array.each(a_nodes, function(i_node) {
			i_node.oldChecked = i_node.get("checked");
			i_node.commit(true);
		});

		f_callback = f_callback || Ext.emptyFn;

		i_view.fireEvent("dirtychange", i_view, false);

		f_callback();
	},

	isDirty: function() {

		var i_controller = this;
		var i_view = i_controller.getView();

		var i_store = i_view.getStore();
		var a_nodes = i_store.getData().items;
		var b_dirty = false;
		Ext.Array.each(a_nodes, function(i_node) {
			if (!b_dirty) {
				b_dirty = i_node.dirty;
			}
			if (b_dirty) {
				return false;
			}
		});
		return b_dirty;
	},

	doReload: function(a_result) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		i_viewModel.set("permissions", a_result);

		var i_store = i_view.getStore();
		var i_root = i_view.getRootNode();

		i_root.removeAll(false);
		i_store.load({
			node: i_root
		});
	}
});
