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', function($rootScope, $scope, dataService, userModel, $location){
14                    $scope.dataService = dataService;
15
16                    $scope.loadServerHealth = function(){
17                        APIUtils.getLogs().then(function(result){
18                            dataService.updateServerHealth(result.data);
19                        });
20                    }
21
22                    $scope.loadServerStatus = function(){
23                        if(!userModel.isLoggedIn()){
24                            return;
25                        }
26                        APIUtils.getHostState(function(status){
27                            if(status == 'xyz.openbmc_project.State.Host.HostState.Off'){
28                                dataService.setPowerOffState();
29                            }else if(status == 'xyz.openbmc_project.State.Host.HostState.Running'){
30                                dataService.setPowerOnState();
31                            }else{
32                                dataService.setBootingState();
33                            }
34                        });
35                    }
36
37                    $scope.loadNetworkInfo = function(){
38                        if(!userModel.isLoggedIn()){
39                            return;
40                        }
41                        APIUtils.getNetworkInfo().then(function(data){
42                            dataService.setNetworkInfo(data);
43                        });
44                    }
45
46                    function loadData(){
47                       $scope.loadServerStatus();
48                       $scope.loadNetworkInfo();
49                       $scope.loadServerHealth();
50                    }
51
52                    loadData();
53
54                    $scope.logout = function(){
55                        userModel.logout(function(status, error){
56                            if(status){
57                               $location.path('/logout');
58                            }else{
59                                console.log(error);
60                            }
61                        });
62                    }
63
64                    $scope.refresh = function(){
65                        loadData();
66                        $scope.$emit('refresh-data');
67                        //Add flash class to header timestamp on click of refresh
68                        var myEl = angular.element( document.querySelector( '.header__refresh' ) );
69                        myEl.addClass('flash');
70                        setTimeout(function () {
71                            myEl.removeClass("flash");
72                        },2000);
73
74                    }
75
76                    var loginListener = $rootScope.$on('user-logged-in', function(event, arg){
77                        loadData();
78                    });
79
80                    $scope.$on('$destroy', function(){
81                        loginListener();
82                    });
83
84                    $scope.multiRecent = function(){
85                        $scope.multi_server_recent = !$scope.multi_server_recent;
86                    };
87                }]
88            };
89        }]);
90})(window.angular);
91