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(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.setBootingState();
34                            }
35                        });
36                    }
37
38                    $scope.loadNetworkInfo = function(){
39                        if(!userModel.isLoggedIn()){
40                            return;
41                        }
42                        APIUtils.getNetworkInfo().then(function(data){
43                            dataService.setNetworkInfo(data);
44                        });
45                    }
46
47                    function loadData(){
48                       $scope.loadServerStatus();
49                       $scope.loadNetworkInfo();
50                       $scope.loadServerHealth();
51                    }
52
53                    loadData();
54
55                    $scope.logout = function(){
56                        userModel.logout(function(status, error){
57                            if(status){
58                               $location.path('/logout');
59                            }else{
60                                console.log(error);
61                            }
62                        });
63                    }
64
65                    $scope.refresh = function(){
66                        //reload current page controllers and header
67                        loadData();
68                        $route.reload();
69                        //Add flash class to header timestamp on click of refresh
70                        var myEl = angular.element( document.querySelector( '.header__refresh' ) );
71                        myEl.addClass('flash');
72                        setTimeout(function () {
73                            myEl.removeClass("flash");
74                        },2000);
75
76                    }
77
78                    var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
79                        loadData();
80                    });
81
82                    $scope.$on('$destroy', function(){
83                        loginListener();
84                    });
85
86                    $scope.multiRecent = function(){
87                        $scope.multi_server_recent = !$scope.multi_server_recent;
88                    };
89                }]
90            };
91        }]);
92})(window.angular);
93