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