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