Ext.define("Portfolio.ux.navigation.view.UserRolePanel", {

	extend: "Ext.form.Panel",

	alias: [
		"widget.userrolepanel"
	],

	alternateClassName: [
		"Ext.ux.navigation.view.UserRolePanel"
	],

	autoScroll: true,

	bodyPadding: 15,

	border: false,

	layout: "anchor",

	defaults: {
		anchor: "100%"
	},

	defaultType: "textfield",

	serviceProgram: "/ip2-services/ip2NavProxy.aspx",

	controller: "navigation.userrole",

	config: {
		entity: ""
	},

	initComponent: function() {

		var i_self = this;
		var s_model = "userRoleRows";
		var i_model;
		i_self.numberChanged = 0;

		if (!Ext.ClassManager.isCreated(s_model)) {

			i_model = Ext.define(s_model, {
				extend: "Ext.data.Model",
				proxy: {
					type: "ajax",
					url: "/ip2-services/ip2transaction.aspx",
					paramsAsJson: true,
					reader: {
						type: "json",
						rootProperty: "rows",
						totalProperty: "totalRows"
					},
					actionMethods: {
						read: "POST"
					}
				}
			});
		}
		else {
			i_model = Ext.ClassManager.get(s_model);
		}

		var i_store = Ext.create("Ext.data.Store", {
			model: i_model,
			autoLoad: true,
			pageSize: 1000,
			listeners: {
				beforeload: {
					fn: function(i_store, i_operation) {

						var i_proxy = i_store.getProxy();
						var o_extraParams = i_proxy.getExtraParams();
						var i_record = i_self.getRecord();

						Ext.apply(o_extraParams, {
							token: i_record.get(i_record.idProperty) || i_self.oldID,
							action: "ip2NavProxy.listroles",
							entity: i_self.getEntity()
						});
					}
				},
				load: {
					fn: function(i_store, a_records) {

						var a_items = [];

						Ext.Array.each(a_records, function(i_record) {

							var s_description = "";
							var b_checked = false;
							var n_token;
							var b_selected = false;
							var b_readyOnly = false;

							var s_type = "";

							s_description = i_record.get("DESCRIPTION") || i_record.get("description") || "";
							n_token = i_record.get("ID") || i_record.get("id");

							s_type = i_record.get("TYPE") || i_record.get("type") || "Default";

							if (i_record.get("SELECTED") !== undefined) {
								b_selected = b_checked = i_record.get("SELECTED") ? true : false;
							}
							else {
								b_selected = b_checked = i_record.get("selected") ? true : false;
							}
							if (i_record.get("readonly")) {
								b_readyOnly = true;
							}

							var o_config = {
								xtype: "checkbox",
								boxLabel: s_description,
								checked: b_checked,
								token: n_token,
								selected: b_selected,
								readOnly: b_readyOnly,
								listeners: {
									change: {
										fn: function() {
											i_self.fireEvent("rolechange", i_self);
										}
									}
								}
							};

							var a_split = s_description.split("-");

							var s_name = a_split[0];

							s_name = s_name.trim();

							var o_group = Ext.Array.findBy(a_items, function(o_group) {
								return o_group.name == s_name;
							});

							if (!o_group) {
								o_group = {
									xtype: "fieldset",
									title: a_split[0],
									name: s_name,
									collapsible: true,
									collapsed: false,
									items: []
								};

								a_items.push(o_group);
							}

							o_group.items.push(o_config);
						});

						Ext.Array.each(a_items, function(o_group) {

							var a_items = o_group.items;

							a_items =  Ext.Array.sort(a_items, function(o_first, o_second) {

								var n_index = 0;

								if(o_first.boxLabel < o_second.boxLabel) {
									n_index = -1;
								}

								if(o_first.boxLabel > o_second.boxLabel) {
									n_index = 1;
								}

								return n_index;
							});

							o_group.items = a_items;

							var b_checked = false;

							Ext.Array.each(a_items, function(o_item) {

								b_checked = o_item.checked;
								if (b_checked) {
									return false;
								}
							});

							if (!b_checked) {
								o_group.collapsed = true;
							}
						});

						a_items =  Ext.Array.sort(a_items, function(o_first, o_second) {

							var n_index = 0;

							if(o_first.name < o_second.name) {
								n_index = -1;
							}

							if(o_first.name > o_second.name) {
								n_index = 1;
							}

							return n_index;
						});

						i_self.add(a_items);
						i_self.fireEvent("rolesLoaded", i_self);
					}
				}
			}
		});

		i_self.store = i_store;

		i_self.callParent();

		i_self.loadRecord(i_self.record);
	}
});
