'use strict';

var angular = require('angular');
require('./modules');
require('../appInitFlow/services/appInitFlow');
require('./services/GesturePasswordService');

var module = angular.module('gesturepassword');

module.run(function(AppConfig, $rootScope, LinkerService, $state, GesturePasswordService) {
  $rootScope.$on('logout', function() {
    LinkerService.getCurrentUser().then(function(user) {
      if (user) {
        localStorage.removeItem(user.userId + '_' + user.userNamePinyin[0]);
      }
    });
  });

  AppConfig.addSetItem({
    text: '手势密码',
    state: 'gesturepassword.configGesturepassword',
    order: 2
  });
});

module.run(function($rootScope, $ionicPlatform, $state, $location, 
  appInitFlowService, $window, GesturePasswordService) {
  var startTime = null;
  var search = $window.location.search;
  if (search && search.indexOf('develop') > 0) {
    return;
  }
  $ionicPlatform.on('resume', function() {
    if (startTime) {
      var time = new Date() - startTime, userId = window.localStorage.userId;
      if (time > 120000 && !!userId && appInitFlowService.inited &&
          (!appInitFlowService.initing)) {
        GesturePasswordService.completed = false;
        GesturePasswordService.judgeGesturePassword();
      }
      startTime = null;
    }
  });

  $ionicPlatform.on('pause', function() {
    startTime = new Date();
    $rootScope.pausePath = $location.path();
  });
});
