'use strict';

var angular = require('angular');
require('ionic');
require('./services/LoginService');
require('./modules');

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

module.controller('LoginModalController', function($scope, LoginService,
    LoginModal, $ionicLoading, $rootScope, $toast, $q) {
  $scope.newUser = {};
  $scope.closeLoginModal = function() {
    LoginModal.hide();
  };

  $scope.showLoading = function() {
    $ionicLoading.show({
      content: '登录中..',
      animation: 'fade-in',
      showBackdrop: true
    });
  };

  $scope.loginInModal = function() {
    var response = null;
    $scope.login($scope.newUser, function(_response) {
      response = _response;
      return LoginModal.hide();
    }).then(function() {
      return LoginService.defaultLoginSuccess(response);
    }).then(function() {
      $rootScope.$broadcast('loginNewUser');
    });
  };

  $scope.login = function(user, successFn) {
    if(user.userName.toLocaleLowerCase() === 'admin'){
      $toast.showLongCenter('管理员权限暂没开放！');
      throw new Error('管理员权限暂没开放！');
    }
    $scope.showLoading();
    return LoginService.login(user, successFn).then(function(data) {
      if (!data.success) {
        $toast.showLongCenter('用户名或密码错误，请核实。');
        throw new Error('用户名或密码错误，请核实。');
      }
      if (data.mask) {
        $toast.showLongCenter('当前登录用户被屏蔽掉，请联系管理员在黑白名单设置中查找原因！');
        throw new Error('当前登录用户被屏蔽掉，请联系管理员在黑白名单设置中查找原因！');
      }
    }, function() {
      $toast.showLongCenter('请求登录失败，请先配置服务器访问地址或检查网络连接。');
    })['finally'](function() {
      $ionicLoading.hide();
    });
  };
});

module.service('LoginModal', function($rootScope, $ionicModal, $controller, $injector, $q) {
  var loginModalPromise, loginModal, $scope;

  var LoginModal = {
    show: function() {
      if (!loginModalPromise) {
        createLoginModal();
      }
      loginModalPromise.then(function() {
        loginModal.show();
      });
    },

    hide: function() {
      if (loginModal) {
        var promise = loginModal.remove();
        loginModalPromise = null;
        loginModal = null;
        return promise;
      } else {
        return $q.when();
      }
    }
  };

  function createLoginModalScope() {
    var $scope = $rootScope.$new();
    $controller('LoginModalController', {
      '$scope': $scope,
      'LoginService': $injector.get('LoginService'),
      'LoginModal': LoginModal,
      '$ionicLoading': $injector.get('$ionicLoading'),
      '$rootScope': $rootScope,
      '$toast': $injector.get('$toast')
    });
    return $scope;
  }

  $rootScope.$on('destroy', function() {
    if (loginModal) {
      loginModal.remove();
    }
  });

  function createLoginModal() {
    $scope = createLoginModalScope();
    loginModalPromise = $ionicModal.fromTemplateUrl('views/login/modal/login.modal.tpl.html', function(modal) {
      loginModal = modal;
    }, {
      scope: $scope,
      controller: 'LoginModalController',
      animation: 'slide-in-up'
    });
  }

  return LoginModal;
});
