(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()'
                    }
                ] }
        ];
        // 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) {
                if (curTopMenu.name === nameOfMenu) {
                    curTopMenu.disabled = disabled;
                }
                _.each(curTopMenu.subMenu, function (curSubMenu) {
                    if (curSubMenu.name === nameOfMenu) {
                        curSubMenu.disabled = disabled;
                    }
                });
            });
        }
        return {
            getList: getList,
            addMenu: addMenu,
            addSubMenu: addSubMenu,
            clearSubMenu: clearSubMenu,
            editMenu: editMenu
        };
    }
})();
