var wd = require('wd');

var SauceLauncher = function(args, sauceConnect, /* config.sauceLabs */ config, logger, helper,
    baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator,
    /* sauce:jobMapping */ jobMapping) {

  baseLauncherDecorator(this);
  captureTimeoutLauncherDecorator(this);
  retryLauncherDecorator(this);

  config = config || {};

  var username = process.env.SAUCE_USERNAME || args.username || config.username;
  var accessKey = process.env.SAUCE_ACCESS_KEY || args.accessKey || config.accessKey;
  var tunnelIdentifier = args.tunnelIdentifier || config.tunnelIdentifier;
  var browserName = args.browserName + (args.version ? ' ' + args.version : '') +
                    (args.platform ? ' (' + args.platform + ')' : '');
  var startConnect = config.startConnect !== false;
  var log = logger.create('launcher.sauce');

  var self = this;
  var driver = wd.remote('ondemand.saucelabs.com', 80, username, accessKey);

  var pendingCancellations = 0;
  var sessionIsReady = false;

  if (startConnect && !tunnelIdentifier) {
    tunnelIdentifier = 'karma' + Math.round(new Date().getTime() / 1000);
  }

  this.name = browserName + ' on SauceLabs';

  var pendingHeartBeat;
  var heartbeat = function() {
    pendingHeartBeat = setTimeout(function() {
      log.debug('Heartbeat to Sauce Labs (%s) - fetching title', browserName);
      driver.title();
      heartbeat();
    }, 60000);
  };

  var formatSauceError = function(err) {
    return err.message + '\n  ' + err.data.split('\n').shift();
  };

  var start = function(url) {
    var options = helper.merge(config.options, args, {
      browserName: args.browserName,
      version: args.version || '',
      platform: args.platform || 'ANY',
      tags: args.tags || config.tags || [],
      name: args.testName || config.testName || 'Karma test',
      'tunnel-identifier': tunnelIdentifier,
      'record-video': args.recordVideo || config.recordVideo || false,
      'record-screenshots': args.recordScreenshots || config.recordScreenshots || true,
      'build': args.build || config.build || process.env.TRAVIS_BUILD_NUMBER ||
              process.env.BUILD_NUMBER || process.env.BUILD_TAG ||
              process.env.CIRCLE_BUILD_NUM || null,
      'device-orientation': args.deviceOrientation || null
    });

    // Adding any other option that was specified in args, but not consumed from above
    // Useful for supplying chromeOptions, firefoxProfile, etc.
    for (var key in args){
      if (typeof options[key] === 'undefined') {
        options[key] = args[key];
      }
    }

    driver.init(options, function(err, jobId) {
      if (pendingCancellations > 0) {
        pendingCancellations--;
        return;
      }

      if (err) {
        log.error('Can not start %s\n  %s', browserName, formatSauceError(err));
        return self._done('failure');
      }

      // Record the job details, so we can access it later with the reporter
      jobMapping[self.id] = {
        jobId: jobId,
        credentials: {
          username: username,
          password: accessKey
        }
      };

      sessionIsReady = true;

      log.info('%s session at https://saucelabs.com/tests/%s', browserName, driver.sessionID);
      log.debug('WebDriver channel for %s instantiated, opening %s', browserName, url);
      driver.get(url, heartbeat);
    });
  };

  this.on('start', function(url) {
    if (pendingCancellations > 0) {
      pendingCancellations--;
      return;
    }

    if (startConnect) {
      sauceConnect.start(username, accessKey, tunnelIdentifier).then(function() {
        if (pendingCancellations > 0) {
          pendingCancellations--;
          return;
        }

        start(url);
      }, function(err) {
        pendingCancellations--;
        log.error('Can not start %s\n  Failed to start Sauce Connect:\n  %s', browserName, err.message);
        self._retryLimit = -1; // don't retry
        self._done('failure');
      });
    } else {
      start(url);
    }
  });

  this.on('kill', function(done) {
    var allDone = function() {
      self._done();
      done();
    };

    if (sessionIsReady) {
      if (pendingHeartBeat) {
        clearTimeout(pendingHeartBeat);
      }

      log.debug('Shutting down the %s driver', browserName);
      // workaround - navigate to other page to avoid re-connection
      driver.get('about:blank', function() {
        driver.quit(allDone);
      });

      sessionIsReady = false;
    } else {
      pendingCancellations++;
      process.nextTick(allDone);
    }
  });
};


module.exports = SauceLauncher;

@@ -2310,16 +2310,53 @@
  %7C%7C null
+,%0A      'disable-popup-handler': true
 %0A    %7D);

disable popup blocking by default