1window.angular && (function (angular) {
2    'use strict';
3
4    angular
5        .module('app.common.directives')
6        .directive('appHeader', ['APIUtils', function (APIUtils) {
7            return {
8                'restrict': 'E',
9                'template': require('./app-header.html'),
10                'scope': {
11                   'path': '='
12                },
13                'controller': ['$rootScope', '$scope','dataService', 'userModel', '$location', '$route',
14                function($rootScope, $scope, dataService, userModel, $location, $route){
15                    $scope.dataService = dataService;
16
17                    $scope.loadServerHealth = function(){
18                        APIUtils.getLogs().then(function(result){
19                            dataService.updateServerHealth(result.data);
20                        });
21                    }
22
23                    $scope.loadServerStatus = function(){
24                        if(!userModel.isLoggedIn()){
25                            return;
26                        }
27                        APIUtils.getHostState().then(function(status){
28                            if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
29                                dataService.setPowerOffState();
30                            }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
31                                dataService.setPowerOnState();
32                            }else{
33                                dataService.setErrorState();
34                            }
35                        }, function(error){
36                            dataService.activateErrorModal();
37                        });
38                    }
39
40                    $scope.loadNetworkInfo = function(){
41                        if(!userModel.isLoggedIn()){
42                            return;
43                        }
44                        APIUtils.getNetworkInfo().then(function(data){
45                            dataService.setNetworkInfo(data);
46                        });
47                    }
48
49                    function loadData(){
50                       $scope.loadServerStatus();
51                       $scope.loadNetworkInfo();
52                       $scope.loadServerHealth();
53                    }
54
55                    loadData();
56
57                    $scope.logout = function(){
58                        userModel.logout(function(status, error){
59                            if(status){
60                               $location.path('/logout');
61                            }else{
62                                console.log(error);
63                            }
64                        });
65                    }
66
67                    $scope.refresh = function(){
68                        //reload current page controllers and header
69                        loadData();
70                        $route.reload();
71                        //Add flash class to header timestamp on click of refresh
72                        var myEl = angular.element( document.querySelector( '.header__refresh' ) );
73                        myEl.addClass('flash');
74                        setTimeout(function () {
75                            myEl.removeClass("flash");
76                        },2000);
77
78                    }
79
80                    var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
81                        loadData();
82                    });
83
84                    $scope.$on('$destroy', function(){
85                        loginListener();
86                    });
87
88                    $scope.multiRecent = function(){
89                        $scope.multi_server_recent = !$scope.multi_server_recent;
90                    };
91                }]
92            };
93        }]);
94})(window.angular);
95