Ext.define("Ext.ux.folderorganizer.Toolbar", {
	extend: "Ext.toolbar.Toolbar",

	border: false,
	listeners: {
		afterrender: {
			fn: function() {
				this.keyMapping();
			}
		},
		nodeselection: {
			fn: function(node) {
				this.setButtonsStatus(node);
			}
		}
	},
	initComponent: function() {

		var me = this;
		var i_wrapper = me.wrapper;

		me.allowAdd = ip2.isAccessible("app." + i_wrapper.dmdProject + ".data." + me.wrapper.entity + ".create");
		me.allowEdit = (ip2.isAccessible("app." + i_wrapper.dmdProject + ".data." + me.wrapper.entity + ".update") ||
						ip2.isAccessible("app." + i_wrapper.dmdProject + ".data." + me.wrapper.entity + ".read"));
		me.allowRemove = ip2.isAccessible("app." + i_wrapper.dmdProject + ".data." + me.wrapper.entity + ".delete");

		me.xpd = me.wrapper.xpd;

		me.items = [];
		if (Ext.isArray(me.wrapper.folder.buttons)) {
			Ext.Array.each(me.wrapper.folder.buttons, function(button) {
				switch (button) {
				case "add":
					var cfg = {
						xtype: "button",
						itemId: "add",
						text: me.getI18N("add"),
						iconCls: "pf-add",
						disabled: false
					};

					var menuCids = [];
					var buttonCount = 0;
					var addCollection;
					var traverseAllowedChilds = function(allowedchilds, menuCids) {
						for (var i = 0; i < allowedchilds.length; i++) {
							if (menuCids.indexOf(allowedchilds[i]) == -1) {
								menuCids.push(allowedchilds[i]);
								var collection = me.xpd.flatten.collections[allowedchilds[i]];
								traverseAllowedChilds(collection.allowedchilds, menuCids);
							}
						}
					};
					var collection = me.xpd.flatten.collections[me.wrapper.folder.cid];
					if (collection && collection.allowedchilds) {
						traverseAllowedChilds(collection.allowedchilds, menuCids);
					}
					else {
						for (var index in me.xpd.flatten.collections) {
							if (index != "all") {
								menuCids.push(index);
							}
						}
					}

					//if (me.xpd && me.wrapper.cid.length != 1) {
					if (me.xpd && me.wrapper.folder.multiAddChoice) {
						var a_items = [];
						for (var index in menuCids) {
							var collection = me.xpd.flatten.collections[menuCids[index]];
							var showAddButton = (Ext.isBoolean(collection.showAddButton)) ? collection.showAddButton : true;
							if (me.allowCreation(collection) && showAddButton) {

								var s_cls = collection.iconCls ? collection.iconCls.split("-").pop() : "pf-add";

								if (s_cls == "gear") {
									s_cls = "gearwheel";
								}

								if (s_cls != "pf-add") {
									s_cls = [
										"pf",
										s_cls
									].join("-");
								}

								var i_menu = Ext.widget("menuitem", {
									text: ip2.xpd.translate(me.xpd, collection.title),
									itemId: "add" + collection.id,
									collection: collection,
									iconCls: s_cls,
									handler: function(menu) {
										me.wrapper.treegrid.addRow(menu.collection);
									}
								});
								a_items.push(i_menu);

								buttonCount++;
								addCollection = collection;
							}
						};
						if (buttonCount == 1) {
							Ext.apply(cfg, {
								handler: function() {
									me.wrapper.treegrid.addRow(addCollection);
								}
							});
						}
						else {
							Ext.apply(cfg, {
								menu: Ext.widget("menu", {
									items: a_items
								})
							});
						}
					}
					else {
						buttonCount++;
						if (me.wrapper.folder.childCid) {
							Ext.apply(cfg, {
								handler: function() {
									me.wrapper.treegrid.addRow(me.xpd.flatten.collections[me.wrapper.folder.childCid]);
								}
							});
						}
						else {
							Ext.apply(cfg, {
								handler: function() {
									me.wrapper.treegrid.addRow();
								}
							});
						}
					}

					if (buttonCount == 1) {
						me.items.push(
							Ext.widget("button", cfg)
						);
					}
					else {
						me.items.push(
							Ext.widget("splitbutton", cfg)
						);
					}
					break;
				case "edit":
					me.items.push(
						{
							xtype: "button",
							itemId: "edit",
							text: me.getI18N("update"),
							iconCls: "pf-edit",
							disabled: true,
							handler: function () {
								me.wrapper.treegrid.updateRow();
							}
						}
					);
					break;
				case "remove":
					me.items.push(
						{
							xtype: "button",
							itemId: "remove",
							text: me.getI18N("remove"),
							iconCls: "pf-navigate_cross",
							disabled: true,
							handler: function () {
								me.wrapper.treegrid.removeRow();
							}
						}
					);
					break;
				case "refresh":
					me.items.push(
						{
							xtype: "button",
							itemId: "refresh",
							text: me.getI18N("refresh"),
							iconCls: "pf-refresh",
							handler: function () {
								me.wrapper.treegrid.refresh();
							}
						}
					);
					break;
				case "deselect":
					me.items.push(
						{
							xtype: "button",
							itemId: "deselect",
							text: me.getI18N("deselect"),
							iconCls: "pf-deselect",
							handler: function () {
								me.wrapper.treegrid.deselect();
							}
						}
					);
					break;
				}
			});
		}

		if (me.wrapper.displayFilterField) {
			me.items.push(
				Ext.create("Ext.ux.folderorganizer.SearchField", {
					itemId: "search",
					wrapper: me.wrapper
				})
			);
		}

		me.callParent();
	},
	/* ----------------------------------------------------------------------------------------------------
			Setup key mapping
		 ---------------------------------------------------------------------------------------------------- */
	keyMapping: function() {

		var i_self = this;

		var i_keyMap = Ext.create("Ext.util.KeyMap", {
			target: i_self.getEl(),
			binding: [
				{
					key: "a",
					ctrl: true,
					fn: function() {
						var addButton = i_self.getComponent("add");
						if (addButton.menu) {
							addButton.showMenu();
						}
						else {
							i_self.addRow();
						}
					}
				},
				{
					key: "e",
					ctrl: true,
					fn: function() {
						i_self.updateRow();
					}
				}
			]
		});

		return i_keyMap;
	},

	/* ----------------------------------------------------------------------------------------------------
			The checkbox for a node is changed
		 ---------------------------------------------------------------------------------------------------- */
	setButtonsStatus: function(node) {
		var me = this;

		var parentCollection = Ext.ux.folderorganizer.Util.getXpdCollectionByNode(me, node);

		me.wrapper.treegrid.node = node;

		// disable all buttons
		Ext.Array.each(me.items.items, function(button) {
			if (button.itemId != "refresh" && button.itemId != "deselect" && button.itemId != "search") {
				button.disable();
			}
		});

		if (me.isAddAllow(node)) {
			var addButton = me.getComponent("add");
			addButton.enable();
		}
		if (me.allowAdd && me.wrapper.folder.cid.length == 1) {
			var addButton = me.getComponent("add");
			addButton.enable();
		}

		// edit allow, if node allows it
		if (me.allowEdit) {
			if (node.data.allowEdit) {

				var button = me.getComponent("edit");
				if (button) {
					if (parentCollection) {
						button.enable();
					}
					else {
						button.enable();
					}
				}
			}
		}

		// remove is allow. if node allows it
		if (node.get("allowRemove")) {
			if (node && !node.isRoot()) {
				var button = me.getComponent("remove");
				if (button) {
					if (parentCollection) {
						button.enable();
					}
					else {
						button.enable();
					}
				}
			}
		}

		if (!parentCollection) {
			return;
		}

		// add is allow, if any children collection not having a entity property
		if (Ext.isArray(parentCollection.allowedchilds) && parentCollection.allowedchilds.length > 0) {
			for (var i = 0; i < parentCollection.allowedchilds.length; i++) {
				var collection = me.xpd.flatten.collections[parentCollection.allowedchilds[i]];
				if (me.allowCreation(collection)) {
					var addButton = me.getComponent("add");
					if (addButton) {
						// enable menu items
						if (addButton.menu) {
							// disable all buttons
							Ext.Array.each(addButton.menu.items.items, function(menu) {
								menu.hide();
							});

							if (parentCollection.childlimit && node.childNodes.length >= parentCollection.childlimit) {
								return;
							}

							// enable allow add menu buttons
							for (var i = 0; i < parentCollection.allowedchilds.length; i++) {
								var collection = me.xpd.flatten.collections[parentCollection.allowedchilds[i]];
								if (collection) {
									var menu = addButton.menu.getComponent("add" + collection.id);
									if (menu) {
										menu.show();
										addButton.enable();
									}
								}
							}
						}
					}
				}
			}
		}
		else {
			var addButton = me.getComponent("add");
			if (addButton) {
				addButton.disable();
			}
		}
	},
	/* ----------------------------------------------------------------------------------------------------
			Check if add allowed
		 ---------------------------------------------------------------------------------------------------- */
	allowCreation: function(collection) {
		if (!collection) {
			return false;
		}

		if (collection.organizer && collection.organizer.allowCreation) {
			return true;
		}

		if (collection.entity) {
			return false;
		}

		return true;
	},
	/* ----------------------------------------------------------------------------------------------------
			Check if add allowed
		 ---------------------------------------------------------------------------------------------------- */
	isAddAllow: function(node) {
		var me = this;

		if (me.wrapper.folder.cid == "" && node.data.ID == -1) {
			return true;
		}
		else {
			return false;
		}
	},
	/* ----------------------------------------------------------------------------------------------------
			I18N translation
		 ---------------------------------------------------------------------------------------------------- */
	getI18N: function(key) {
		return ip2.folderorganizer.getTextValue(key);
	}
});
