'use strict';

var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var Q = require('q');
var _ = require('lodash');
var parallelize = require('concurrent-transform');

var Config = require('./config');

/* jshint camelcase:false*/
var webdriverUpdate = require('gulp-protractor').webdriver_update;

var publisher;

var uploadScreenshots = function(){
    var deferred = Q.defer();
    var date = new Date();
    var prefix = '' + date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate() +
        '-' + Config.projectName + '/';
    var publisherConfig = {
        key: Config.credentials.s3.key,
        secret: Config.credentials.s3.secret,
        bucket: Config.credentials.s3.reportsBucket
    };
    var region = Config.credentials.s3.region;
    if(_.isString(region)){
        publisherConfig.region = region;
    }
    var protocol = Config.credentials.s3.protocol;
    if(_.isString(protocol) && protocol === 'http'){
        publisherConfig.secure = false;
    }
    publisher = $.awspublish.create(publisherConfig);

    if(Config.isOnTravis) {
        var reportURL =
            'http://' + Config.credentials.s3.reportsBucket + '.s3.amazonaws.com/' +
                prefix + 'report.html';
        gulp.src('**/*', { cwd: Config.paths.e2eReportsDir })
            .pipe($.awspublishRouter({
                routes: {
                    '^.+$': prefix + '$&'
                }
            }))
            .pipe($.awspublish.gzip())
            .pipe(parallelize(publisher.publish(), 10))
            .pipe($.awspublish.reporter())
            .on('error', deferred.reject)
            .on('end', function(data){
                $.util.log('report available at: ' + $.util.colors.green(reportURL));
                deferred.resolve(data);
            });
    } else {
        deferred.resolve();
    }
    return deferred.promise;
};

//update webdriver if necessary, this task will be used by e2e task
gulp.task('tests:update-webdriver', webdriverUpdate);

// Run e2e tests using protractor, make sure serve task is running.
gulp.task('tests:e2e', ['tests:update-webdriver'], function() {
  var configFile = Config.paths.protractorConfigLocal;
  //var configFile = Config.isOnTravis ? Config.paths.protractorConfigTravis : Config.paths.protractorConfigLocal;
  var args = [];
  if(Config.isOnTravis) {
      args.push('--baseUrl');
      args.push(Config.websiteUrl);
  }
  var specs = Config.paths.e2eSpecs;
  $.util.log('specs: ' + specs);
  return gulp.src(specs)
    .pipe($.protractor.protractor({
      configFile: configFile,
          args: args
    }))
    .on('error', function(e) {
        uploadScreenshots()
            .then(function() {
                throw new $.util.PluginError(__filename, e);
            });
    });
});

gulp.task('tests:unit', function (done) {
    var karma = require('karma').server;
    karma.start({
        configFile: __dirname + '/../karma.conf.js',
        singleRun: true
    }, done);
});

gulp.task('test:dev', [ 'config:load' ], function (done) {
    $.runSequence('tests:e2e', done);
});
@@ -2710,50 +2710,53 @@
     
-throw new $.util.PluginError(__filename, e
+$.util.log(e);%0A                process.exit(1
 );%0A 

address review comment.