Ext.define('Ext.ux.dmd.uiexplorer.Menu', {
	
	extend: 'Ext.menu.Menu',
	alias: ['widget.dmdUiExplorerMenu'],

	record: null,
	addHandler: null,
	removeHandler: null,
	renameHandler: null,

	buildAddConfig: function(info, fn) {

		fn = ( typeof fn === 'function' ) ? fn : null;

		var menu = this,
			type = info.type || '',
			text,
			add = false;

		switch ( info.type ) {
			case 'entity': text = 'Add a new UI folder'; break;
			case 'uitype': text = 'Add a new UI'; break;
			case 'ui': text = 'Insert a new UI'; break;
			default: break;
		}

		if ( text ) {
			add = {
				text: text,
				name: 'add',
				iconCls: menu.iconClasses['add']
			};
			if ( fn )
				add.handler = fn;
		}

		return add;
	},
	buildRemoveConfig: function(info, fn) {
		
		fn = ( typeof fn === 'function' ) ? fn : null;

		var menu = this,
			type = info.type || '',
			text,
			remove = false;

		switch ( info.type ) {
			case 'ui': text = 'Remove UI'; break;
			default: break;
		}

		if ( text ) {
			remove = {
				text: text,
				name: 'remove',
				iconCls: menu.iconClasses['remove']
			};
			if ( fn )
				remove.handler = fn;
		}

		return remove;
	},
	buildRenameConfig: function(info, fn) {
		
		fn = ( typeof fn === 'function' ) ? fn : null;
		
		var menu = this,
			type = info.type || '',
			text,
			rename = false;

		switch ( info.type ) {
			case 'ui': text = 'Rename'; break;
			default: break;
		}

		if ( text ) {
			rename = {
				text: text,
				name: 'rename',
				iconCls: menu.iconClasses['edit']
			};
			if ( fn )
				rename.handler = fn;
		}

		return rename;
	},
	buildMenu: function(record) {

		var menu = this,
			info = record.raw || {};

		add = menu.buildAddConfig(info, menu.addHandler);
		remove = menu.buildRemoveConfig(info, menu.removeHandler);
		rename = menu.buildRenameConfig(info, menu.renameHandler);

		if ( add ) menu.add(add);
		if ( rename ) menu.add(rename);
		if ( remove ) menu.add(remove);

		return menu;
	},

	initComponent: function() {

		this.callParent(arguments);

		var menu = this,
			renderPosition = menu.renderPosition;

		if ( menu.record )
			menu.buildMenu(menu.record);

	}

});