(function() {

	'use strict';

	angular.module('uniflash').service('menuContribSrv', menuContribSrv);

	menuContribSrv.$inject = [];

	function menuContribSrv() {
		// the default menu for UniFlash
		var menuContribution = [
			{'id':'navSessionMenu',
			 'name':'Session',
			 'subMenu':[
				{'name':'New Session',
				 'funcStr':'ufDsProgressDialog.deconfigureDS();ufSession.new()'
				},
				{'name':'Load Session',
				 'funcStr':'ufDsProgressDialog.deconfigureDS();ufSession.load();'
				},
				{'name':'Save Session',
				 'funcStr':'ufSession.save()'
				}
			]}
			
			/*
			{'id':'navNewSessionMenu',
			 'name':'New',
			 'funcStr':'state.go(\'welcome\')'
			},
			{'id':'navLoadSessionMenu',
			 'name':'Load',
			 'funcStr':'ufSession.load()'
			},
			{'id':'navSaveSessionMenu',
			 'name':'Save',
			 'funcStr':'ufSession.save()'
			}
			*/

			/*
			{'id':'navSessionMenu',
			 'name':'Session',
			 'subMenu':[
				{'name':'New Session',
				 'funcStr':'state.go(\'welcome\')'
				},
				{'name':'Load Session',
				 'funcStr':'ufSession.load()'
				},
				{'name':'Save Session',
				 'funcStr':'ufSession.save()'
				}
			]},
			{'id':'navProgramMenu',
			 'name':'Program',
			 'subMenu':[
				{'name':'Load Program',
				 'funcStr':'ufDS.program()'
				},
				{'name':'Verify Program',
				 'funcStr':'ufDS.verify()'
				}
			]}
			*/
		];
		
		// return the menu list
		function getList() {
			return menuContribution;
		}
		
		// add a top menu item
		function addMenu(menu) {
			var foundMenu = _.find(menuContribution, function(curMenu) { return (curMenu.name === menu.name); });
			if ( !foundMenu ) {
				menuContribution.push(menu);
			}
		}

		// add a submenu to an existing top menu
		function addSubMenu(nameOfTopMenu,subMenuToAdd) {
			var foundMenu = _.find(menuContribution, function(curMenu) { return (curMenu.name === nameOfTopMenu); });
			if ( foundMenu ) {
				if ( !foundMenu.subMenu ) {
					foundMenu.subMenu = [];
				}
				foundMenu.subMenu.push(subMenuToAdd);
			}
		}
		
		// remove all submenus from a top menu
		function clearSubMenu(nameOfTopMenu) {
			var foundMenu = _.find(menuContribution, function(curMenu) { return (curMenu.name === nameOfTopMenu); });
			if ( foundMenu ) {
				foundMenu.subMenu = [];
			}
		}
		
		// find and edit a menu item based on the name
		function editMenu(nameOfMenu,disabled) {
			_.each(menuContribution, function(curTopMenu:any) {
				
				if ( curTopMenu.name === nameOfMenu ) {
					curTopMenu.disabled = disabled;
				}
				
				_.each(curTopMenu.subMenu, function(curSubMenu:any) {
					if ( curSubMenu.name === nameOfMenu ) {
						curSubMenu.disabled = disabled;
					}
				});
			});
		}
		
		return {
			getList : getList,
			addMenu : addMenu,
			addSubMenu : addSubMenu,
			clearSubMenu : clearSubMenu,
			editMenu : editMenu
		};
	}

})();