global.dashModule
    .controller('dashboardsCtrl', ['$scope', dashboardsCtrl])
    .directive('dashboards', dashboardsDirective);

function dashboardsDirective() {
    return {
        restrict: 'E',
        templateUrl: 'dashboards/html/dashboardsTemplate'
    };
}

function dashboardsCtrl($scope) {
    $scope.isSubmoduleVisible = isSubmoduleVisible;
    $scope.setSubModule = setSubModule;
    $scope.getHeaderKey = getHeaderKey;
    $scope.subModule = global.getParameterByName('subModule') || 'dashborardsView';
    addUrlListener();

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

    function getHeaderKey(action){
        return action === 'create' ? 'CREATE_DASHBOARD' : 'EDIT_DASHBOARD';
    }
}
