(function () {
    global.dashModule
        .controller('processesController', ['$scope', processesController])
        .directive('processes', [processesDirective]);

    function processesDirective() {
        return {
            restrict: 'E',
            templateUrl: 'processes/html/processesTemplate'
        };
    }

    function processesController($scope) {
        $scope.subModule = global.getParameterByName('subModule') || 'viewPackageProcesses';
        $scope.setSubModule = setSubModule;
        $scope.isSubmoduleVisible = isSubmoduleVisible;
        addUrlListener();

        function addUrlListener() {
            window.addEventListener('popstate', function (e) {
                $scope.$evalAsync($scope => {
                    $scope.subModule = e.state ? e.state.subModule : 'viewPackageProcesses';
                });
            }, false);
        }

        function setSubModule($event) {
            $scope.subModule = $event.currentTarget.attributes.subModule.value;
            history.pushState({
                subModule: $scope.subModule
            }, null, '?subModule=' + $scope.subModule);
        }

        function isSubmoduleVisible(subModule) {
            return subModule === $scope.subModule;
        }
    }
})();
