'use strict';

var angular = require('angular');
require('../modules');
require('../services/GestureScreenContext');
require('../services/GestureScreenPasswordService');
require('../services/GesturePasswordService');
require('../../appconfig/services/appFlowConfigService');

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

module.controller('setGestureScreenController', function(
  GestureScreenContext, $scope, GestureScreenPasswordService,
  LinkerService, $state, $toast, $rootScope, $location, Timer,
  GesturePasswordService, CacheService, $ionicPopup,
  appFlowConfigService) {
  var height = window.screen.height,
    width = window.screen.width,
    pageState = 'SET_PASSWORD',
    firstPassword = [];


  var screenContext = new GestureScreenContext(width, height,
    function(password) {
      if (pageState === 'SET_PASSWORD') {
        return validateFirstPassword(password);
      } else {
        return validateRePassword(password);
      }
    }, true);

  var layout = screenContext.layout;
  $scope.topcontentStartY = layout.top.content.startY;
  $scope.bottomContentStartY = layout.bottom.content.startY - 15;
  $scope.screenContext = screenContext;
  if ($state.current.data) {
    $scope.ignore = $state.current.data.ignore;
    $scope.back = $state.current.data.back;
  }
  $scope.ignoreGesturePassword = ignoreGesturePassword;

  function setValidateState(state) {
    $scope.validateState = state;
    $scope.$evalAsync(function() {
      $scope.validateState = state;
    });
    if (state === 'repassword') {
      screenContext.setState(GestureScreenContext.STATE_PRIMITIVE);
      screenContext.setFirspassword(firstPassword);
      fireEvent();
    }
  }

  setValidateState('init');

  function validateFirstPassword(password) {
    firstPassword = password.join('');
    if (password.length < 4) {
      setValidateState('error-length');
      return false;
    } else {
      pageState = 'CONFIRM_PASSWORD';
      setValidateState('repassword');
      return false;
    }
  }

  function validateRePassword(password) {
    if (password.join('') !== firstPassword) {
      setValidateState('error-repassword');
      return false;
    } else {
      setValidateState('success');
      LinkerService.getCurrentUser().then(function(user) {
        appFlowConfigService.setNeedGesturePassword(true);
        GestureScreenPasswordService.setPassword(user.userId, firstPassword);
        GesturePasswordService.doComplete();
      });
      return true;
    }
  }

  $scope.reset = function() {
    setValidateState('init');
    screenContext.setState(GestureScreenContext.STATE_PRIMITIVE);
    screenContext.setFirspassword(null);
    fireEvent();
    firstPassword = '';
    pageState = 'SET_PASSWORD';
  };

  function fireEvent() {
    $rootScope.$broadcast('$gesturepassword.updatefirstpassword');
    $rootScope.$emit('$gesturepassword.updatefirstpassword');
  }

  $scope.delayLoadGesturePassword = false;
  Timer.delay(function() {
    $scope.delayLoadGesturePassword = true;
  },200);


  /**
   * 忽略掉手势密码
   */
  function ignoreGesturePassword() {
    $ionicPopup.confirm({
      title: '温馨提示',
      content: '忽略手势密码会开启账号登陆模式（可以在设置页面更改当前设置），是否继续？',
      okText: '继续',
      cancelText: '取消'
    }).then(function(res) {
      if (res) {
        appFlowConfigService.setNeedGesturePassword(false);
        GesturePasswordService.doComplete();
      }
    });
  }
});

