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

	alias: "controller.navigation.rolepermissiontree",

	requieres: [
		"Ext.registry.util.Service"
	],

	doSave: function() {

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

		var o_permissions = {};
		var a_nodes = i_view.getChecked();
		Ext.Array.each(a_nodes, function(i_node) {
			i_controller.setObject(i_node.get("id"), true, o_permissions);
		});

		Ext.Ajax.request({
			url: Ext.registry.util.Service.getTransactionUrl({
				file: s_registry
			}),
			method: 'POST',
			jsonData: {
				action: 'ip2NavProxy.updaterolepermission',
				roletoken: i_record.get("ROLROLTKN"),
				permissions: o_permissions
			},
			success: function() {

				i_controller.resetDirty();
			}
		});
	},
	setObject: function(sourceObject, value, targetObject) {
		var parts = sourceObject.split(".");
		var p = parts.pop();
		for (var i = 0, j; targetObject && (j = parts[i]); i++) {
			targetObject = (j in targetObject ? targetObject[j] : targetObject[j] = {});
		}
		return targetObject && p ? (targetObject[p] = value) : undefined;
	},
	doDelete: function() {

		var i_controller = this;
		var i_view = i_controller.getView();
	},
	nodeCheckChange: function(i_node, b_checked) {

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

		var i_grid = i_viewModel.get('grid');
		if (i_grid) {
			i_grid.getController().upsertRowFromTreeNode(i_node, b_checked);
		}
	},
	initializeRolePermissionGrid: function(treestore, nodes) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var a_checked = i_view.getChecked();
		var i_viewModel = i_controller.getViewModel();
		var i_root = i_view.getRootNode();
		var a_children = i_root.childNodes;

		var i_grid = i_viewModel.get('grid');
		if (i_grid) {
			i_grid.getController().loadGridStoreFromTreeStore(a_checked);
		}

		i_view.collapseAll();
		Ext.Array.each(a_children, function(i_node) {
			i_node.expand();
		});
	},
	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);
		});
		i_view.fireEvent("dirtychange", i_view, false);

		f_callback && 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_roles) {

		var i_controller = this;
		var i_view = i_controller.getView();
		var i_viewModel = i_controller.getViewModel();
		var i_store = i_view.getStore();
		var i_root = i_view.getRootNode();

		i_viewModel.set("roles", a_roles);

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