var angular = require('angular');
require('../modules');
'use strict';

var module = angular.module('core.services');

module.run(function($rootScope) {

  document.addEventListener('offline', function() {
    $rootScope.$broadcast('offline');
  }, false);

  document.addEventListener('online', function() {
    $rootScope.$broadcast('online');
  }, false);

  document.addEventListener('server.beginTryConnecting', function(event) {
    $rootScope.$evalAsync(function() {
      $rootScope.inTryConnecting = true;
      $rootScope.$broadcast('server.beginTryConnecting', event.netStatus);
    });
  }, false);

  document.addEventListener('server.endTryConnecting', function(event) {
    $rootScope.$evalAsync(function() {
      $rootScope.inTryConnecting = false;
      $rootScope.$broadcast('server.endTryConnecting', event.netStatus);
    });
  }, false);

  document.addEventListener('resume', function() {
    $rootScope.$broadcast('resume');
  });

  document.addEventListener('active', function() {
    $rootScope.$broadcast('active');
  });

  $rootScope.$on('server.online', function() {
    $rootScope.netWorkStatus = 'online';
    $rootScope.inTryConnecting = false;
  });

  $rootScope.$on('server.offline', function() {
    $rootScope.netWorkStatus = 'offline';
  });

  $rootScope.$on('server.beginTryConnecting', function() {
    $rootScope.inTryConnecting = true;
  });

  $rootScope.$on('server.endTryConnecting', function() {
    $rootScope.inTryConnecting = false;
  });
});