/// <reference path="../../../Scripts/underscore.js" />
/// <reference path="../../common/utils/enums.js" />
/// <reference path="../nav-bar-util.js" />

// AppNavBarController controller for the navigation bar of the application. This controller is responsible for 
// manage the different types of the tabs and corresponding menus.
frameworkModule.controller('AppNavController', ['$rootScope', '$scope', '$log', '$state' ,'loginContext', 'application', 'userService', '$translate', 'vatSessionService', '$interval', 'workflowService',
    function ($rootScope, $scope, $log, $state ,loginContext, application, userService, $translate, vatSessionService, $interval, workflowService) {
        'use strict';
        $log.debug('AppNavController.ctor()...');

        $scope.companyName = loginContext.projectCustomerName;
        $scope.loginUserName = loginContext.userName;
        $scope.tpUrl = loginContext.tpUrl;
        $scope.hasUnreadMsg = true;
        $scope.msgNum = 0;
        $scope.showSubMenu = true;
        $scope.linkAdmin = false;
        $scope.specialUser = $scope.loginUserName.toLowerCase();

        $scope.hasQuerySummaryDashboardCode = false;

        var interval = $interval(function () {
            var data = vatSessionService.userPermission;

            if (data.isAdmin == undefined) return;

            if (data.isAdmin || (data.navigationUrlList && data.navigationUrlList.length > 0)) {
                $scope.linkAdmin = true;
            }
            $interval.cancel(interval);

            $scope.hasQuerySummaryDashboardCode = window.PWC.isHavePermission(constant.vatPermission.dataAnalysis.dashboard.querySummaryDashboardCode, data);
        }, 500);

        //userService.getUserPermission(loginContext.userName).success(function (data) {
        //    if (data) {
        //        if (data.isAdmin || (data.navigationUrlList && data.navigationUrlList.length > 0)) {
        //            $rootScope.linkAdmin = true;
        //        }

        //        $scope.hasQuerySummaryDashboardCode = window.PWC.isHavePermission(constant.vatPermission.dashboard.querySummaryDashboardCode, data)
        //    }
        //});
        $rootScope.$on('notification-read', function () {
            $scope.msgNum--;
        });
        
       var getNotificationInfo = function () {
            $scope.notificationCriteriaX = {
                isFirstTimeLoading: false,
                filterType: "MAIN",
                serviceType: 2,
                mainFilter: "UNREAD",
                singleNode: null,
                singleTask: null,
                multipleNode: null,
                multipleTask: null,
                multipleStatus: null,
                multipleDueDate: null,
                multiplePriority: null,
                multipleType: null,
                searchKeyword: ""
            };

            workflowService.getPushNotifications($scope.notificationCriteriaX).success(function (notifications) {
                if (notifications != null && notifications.notificationList != null)
                    $scope.msgNum = notifications.notificationList.length;
            });
        };

        $scope.setDashboardFlag = function () {
            vatSessionService.urlFromAnalyze = false;
            return true;
        };

        $scope.toggleSubMenu = function (showsub) {
            $scope.showSubMenu = !$scope.showSubMenu;
            //$scope.$emit(application.events.showSubMenu, {direction:'up', show:$scope.showSubMenu});
        };

        $scope.showChangePassword = function () {
            $("#changePasswordModal").modal('show');
            $('.user-menu').css('display', 'none');
        }
        $scope.toggleUserMenu = function (event) {
            if (event.target.parentElement.className === 'user-info' || event.target.parentElement.className === 'ng-binding') {
                $('.user-menu').toggle();
            }
            event.stopPropagation();
        };

        var initSubHeader = function () {
            var enterSubHeader = false;
            $('[hover-show]').hover(function () {
                var className = $(this).attr('hover-show');
                $('.nav-sub-container').hide();
                $('.nav-sub-container.' + className).show();
            }, function () {
                setTimeout(function () { //等待 nav-sub-container hover 设置上对应的值
                    if (enterSubHeader) {
                        var className = $(this).attr('hover-show');
                        $('.nav-sub-container.' + className).hide();
                    }
                }, 200);
            });

            $('.nav-sub-container').hover(function () {
                enterSubHeader = true;
            }, function () {
                enterSubHeader = false;
                $('.nav-sub-container').hide();
            });


            
        };

        //设置 菜单选中状态
        var initState = function(){
            $($('.nav-container .nav-element-left a')[0]).addClass('active');

            $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {                
                $log.debug("$stateChangeStart: toState="+toState.name);

                $('.nav-container .nav-element-left a').removeClass('active');
                $($('.nav-container .nav-element-left a')[getMenuIndex(toState.name)]).addClass('active');
            });
        };

        var getMenuIndex = function(name){
            switch(name){
                case 'GlobalSearch': return 0;
                case 'summary': return 0;
                case 'summaryDashboard': return 3;
                case 'RiskPanel': return 3;
                case 'DataSummary': return 3;
                default: return 2;
            };       
        };

        $scope.search = function(){
            PWC.simulateProgress();
            $state.go('GlobalSearch');
        };


        $(document).click(function (event) {
            $('.user-menu').css('display', 'none');
            event.stopPropagation();
        });

        (function initialize() {
            initSubHeader();
            initState();
            getNotificationInfo();
        })();

    }
]);