Bitly = {};

// Request Bitly credentials for the user
// @param options {optional}
// @param credentialRequestCompleteCallback {Function} Callback function to call on
//   completion. Takes one argument, credentialToken on success, or Error on
//   error.
Bitly.requestCredential = function (options, credentialRequestCompleteCallback) {
  // support both (options, callback) and (callback).
  if (!credentialRequestCompleteCallback && typeof options === 'function') {
    credentialRequestCompleteCallback = options;
    options = {};
  } else if (!options) {
    options = {};
  }

  var config = ServiceConfiguration.configurations.findOne({service: 'bitly'});
  if (!config) {
    credentialRequestCompleteCallback && credentialRequestCompleteCallback(
      new ServiceConfiguration.ConfigError());
    return;
  }

  var credentialToken = Random.secret();

  var loginStyle = OAuth._loginStyle('google', config, options);

  var loginUrl =
        'https://bitly.com/oauth/authorize' +
        '?client_id=' + config.clientId +
        '&redirect_uri=' + OAuth._redirectUri('bitly', config) +
        '&state=' + OAuth._stateParam(loginStyle, credentialToken);

  OAuth.launchLogin({
    loginService: "bitly",
    loginStyle: loginStyle,
    loginUrl: loginUrl,
    credentialRequestCompleteCallback: credentialRequestCompleteCallback,
    credentialToken: credentialToken,
    popupOptions: { width: 900, height: 550 }
  });
};

//Google = {};
//
//// Request Google credentials for the user
//// @param options {optional}
//// @param credentialRequestCompleteCallback {Function} Callback function to call on
////   completion. Takes one argument, credentialToken on success, or Error on
////   error.
//Google.requestCredential = function (options, credentialRequestCompleteCallback) {
//  // support both (options, callback) and (callback).
//  if (!credentialRequestCompleteCallback && typeof options === 'function') {
//    credentialRequestCompleteCallback = options;
//    options = {};
//  } else if (!options) {
//    options = {};
//  }
//
//  var config = ServiceConfiguration.configurations.findOne({service: 'google'});
//  if (!config) {
//    credentialRequestCompleteCallback && credentialRequestCompleteCallback(
//      new ServiceConfiguration.ConfigError());
//    return;
//  }
//
//  var credentialToken = Random.secret();
//
//  // always need this to get user id from google.
//  var requiredScope = ['profile'];
//  var scope = ['email'];
//  if (options.requestPermissions)
//    scope = options.requestPermissions;
//  scope = _.union(scope, requiredScope);
//  var flatScope = _.map(scope, encodeURIComponent).join('+');
//
//  // https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl
//  var accessType = options.requestOfflineToken ? 'offline' : 'online';
//  var approvalPrompt = options.forceApprovalPrompt ? 'force' : 'auto';
//
//  var loginStyle = OAuth._loginStyle('google', config, options);
//
//  var loginUrl =
//    'https://accounts.google.com/o/oauth2/auth' +
//    '?response_type=code' +
//    '&client_id=' + config.clientId +
//    '&scope=' + flatScope +
//    '&redirect_uri=' + OAuth._redirectUri('google', config) +
//    '&state=' + OAuth._stateParam(loginStyle, credentialToken) +
//    '&access_type=' + accessType +
//    '&approval_prompt=' + approvalPrompt;
//
//  // Use Google's domain-specific login page if we want to restrict creation to
//  // a particular email domain. (Don't use it if restrictCreationByEmailDomain
//  // is a function.) Note that all this does is change Google's UI ---
//  // accounts-base/accounts_server.js still checks server-side that the server
//  // has the proper email address after the OAuth conversation.
//  if (typeof Accounts._options.restrictCreationByEmailDomain === 'string') {
//    loginUrl += '&hd=' + encodeURIComponent(Accounts._options.restrictCreationByEmailDomain);
//  }
//
//  OAuth.launchLogin({
//    loginService: "google",
//    loginStyle: loginStyle,
//    loginUrl: loginUrl,
//    credentialRequestCompleteCallback: credentialRequestCompleteCallback,
//    credentialToken: credentialToken,
//    popupOptions: { height: 600 }
//  });
//};
