(function () {
    global.dashModule
        .directive('profileSettings', profileSettingsDirective)
        .controller('profileSettingsCtrl', ['$scope', profileSettingsCtrl]);

    function profileSettingsDirective() {
        return {
            restrict: 'E',
            templateUrl: 'profileSettings/html/ProfileSettingsTemplate'
        };
    }

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

        function addUrlListener() {
            window.addEventListener('popstate', function (e) {
                $scope.$evalAsync($scope => {
                    $scope.subModule = e.state ? e.state.subModule : 'editProfile';
                });
            }, 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;
        }
    }
})();
