  'use strict';

  var angular = require('angular');
  require('../modules');
  require('../services/GestureScreenContext');
  require('../../login/services/LoginService');
  require('../services/GestureScreenPasswordService');
  require('../services/GesturePasswordService');
  require('../../login/LoginModal');
  require('../../../commons/services/AppConfig');

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

  module.controller('validateGestureScreenController', function(
    GestureScreenContext, $scope, LinkerService,LoginService, GestureScreenPasswordService, $state, $toast, AppConfig,
    $ionicPopup, GesturePasswordService, Timer, $rootScope, LoginModal) {
    var height = window.screen.height,
      width = window.screen.width,
      gesturepassword = null;

    $scope.count = 5;
    $scope.isStateNo = false;
    $scope.passwordLoginEnable = AppConfig.getCustomResult('passwordLoginEnable');

    var screenContext = new GestureScreenContext(width, height, function(passwords) {
      var validate = passwords.join('') === gesturepassword;
      if (validate) {
        GesturePasswordService.doComplete();
      } else if (!validate && passwords.length > 0) {
        $scope.$evalAsync(function() {
          $scope.isStateNo = true;
        });
        $scope.count--;
        if ($scope.count <= 0) {
          $scope.count++;
          $ionicPopup.confirm({
            title: '提示',
            content: '您已经连续5次输错手势，手势解锁已关闭，请重新登录。',
            okText: '确定',
            cancelType: 'button-cancel-display'
          }).then(function(res) {
            if (res) {
              LinkerService.getCurrentUser().then(function(user) {
                GestureScreenPasswordService.setPassword(user.userId, '');
                LoginService.logout().then(function() {
                  if(!AppConfig.getCustomResult('passwordLoginEnable')) {
                    $state.go('calogin');
                  } else {
                    $state.go('login'); // 定制是否有密码登录页面
                  }
                });
              });
            }
          });
        }

      }
      return validate;
    });

	function getLoginUser() {
		$scope.userId = LinkerService.currentUserId;
	}
    
    function getGesturePassword() {
      var user = LinkerService.currentUser;
      gesturepassword = GestureScreenPasswordService.getPassword(user.userId);
    }

    $scope.forgetPassword = function() {
      var user = LinkerService.currentUser;
      if (user) {
        GestureScreenPasswordService.setPassword(user.userId, '');
        LoginService.logout();
      }
      if(!AppConfig.getCustomResult('passwordLoginEnable')) {
        $state.go('calogin');
      } else {
        $state.go('login'); // 定制是否有密码登录页面
      }
    };

    $scope.otherLogin = function() {
      return LoginService.logout().then(function() {
        return LoginModal.show();
      });
    };

    getLoginUser();
    getGesturePassword();

    var layout = screenContext.layout;
    $scope.topcontentStartY = layout.top.content.startY;
    $scope.bottomContentStartY = layout.bottom.content.startY - 15;
    $scope.screenContext = screenContext;
    $scope.delayLoadGesturePassword = false;
    Timer.delay(function() {
      $scope.delayLoadGesturePassword = true;
    }, 200);


    var timerId;
    $scope.$on('gesture.touchend', function() {
      timerId = Timer.delay(function() {
        $scope.isStateNo = false;
        $scope.screenContext.startMove(0,0);
      }, 1500);
    });

    $scope.$on('gesture.touchstart', function() {
      $scope.isStateNo = false;
      Timer.clear(timerId);
    });

    $scope.$on('$destroy', function() {
      Timer.clear(timerId);
    });
  });

