(function () {
  'use strict';

  const pogobuf = require('pogobuf');

  angular.module('app').service('loginService', [LoginService]);

  function LoginService() {
    return {
      getLoginToken: getLoginToken
    };

    function getLoginToken(loginType, username, password) {
      var login = null;

      loginType = loginType.toLowerCase();
      if(loginType === 'google'){
        login = new pogobuf.GoogleLogin();
      } else if (loginType === 'ptc') {
        login = new pogobuf.PTCLogin();
      } else {
        throw 'Unrecognized login type specified.';
      }

      if (!login) {
        throw 'Failed to acquire login resource';
      }

      return new Promise((resolve, reject) => {
    		login.login(username, password).then(token => {
    			resolve(token);
    		}, err => {
    			reject(err);
    		});
      })
    }
  }
})();
