var args = require('yargs').argv;
var chalk = require('chalk');
var wct = require('web-component-tester').test;
var _ = require('lodash');
var gutil = require('gulp-util');

function cleanDone(done) {
  return function(error) {
    if (error) {
      // Pretty error for gulp.
      error = new Error(chalk.red(error.message || error));
      error.showStack = false;
    }
    done(error);
  };
}

function checkArguments(arguments) {
  _.forEach(arguments, function(a) {
    if(!args.hasOwnProperty(a)) {
      throw Error('Required argument \'--'+ a +'\' is missing.');
    }
  });
}

function localAddress() {
  var ip, tun, ifaces = require('os').networkInterfaces();
  Object.keys(ifaces).forEach(function (ifname) {
    ifaces[ifname].forEach(function (iface) {
      if ('IPv4' == iface.family && !iface.internal) {
        if (!ip) ip = iface.address;
        if (/tun/.test(ifname)) tun = iface.address;
      }
    });
  });
  return tun || ip;
}

function test(options, done) {
  wct(options, cleanDone(done));
}

module.exports = {
  localAddress: localAddress,
  test: test,
  checkArguments: checkArguments,

  testSauce: function(suites, browsers, build, done) {
        checkArguments(['sauceUsername', 'sauceAccessKey']);

        test(
          {
            suites: suites,
            browserOptions: {
              name: localAddress() + ' / ' + new Date(),
              build: build
            },

            plugins: {
              //local: {
              //  browsers: ['chrome']
              //},
              sauce: {
                username: args.sauceUsername,
                accessKey: args.sauceAccessKey,
                browsers: browsers
              },
              'teamcity-reporter': args.teamcity
            },
            webserver: {
              hostname: localAddress()
            }
          }, done);
  },

  autoRevert: function(err, handler, done) {
    if(err) {
      gutil.log(err.toString());
      if(args.autoRevert) {
        handler();
      } else {
        gutil.log('No action. Use --auto-revert to revert changes.')
        done(err);
      }
    } else {
      done();
    }
  }
};

@@ -1176,74 +1176,8 @@
 ) %7B%0A
-        checkArguments(%5B'sauceUsername', 'sauceAccessKey'%5D);%0A%0A    
     
@@ -1192,18 +1192,10 @@
     
- 
-   %7B%0A    
+%7B%0A
     
@@ -1218,28 +1218,24 @@
 es,%0A        
-  
-  
 browserOptio
@@ -1236,28 +1236,24 @@
 rOptions: %7B%0A
-    
           na
@@ -1293,20 +1293,16 @@
 Date(),%0A
-    
         
@@ -1328,20 +1328,12 @@
     
-    %7D,%0A%0A    
+%7D,%0A%0A
     
@@ -1357,20 +1357,16 @@
         
- 
-   
 //local:
@@ -1364,28 +1364,24 @@
  //local: %7B%0A
-    
           //
@@ -1417,21 +1417,13 @@
     
-   
- 
 //%7D,%0A
-    
     
@@ -1433,28 +1433,24 @@
    sauce: %7B%0A
-    
             
@@ -1491,20 +1491,16 @@
         
-    
 accessKe
@@ -1531,28 +1531,24 @@
             
-    
 browsers: br
@@ -1560,35 +1560,27 @@
 s%0A          
-  
-  
 %7D,%0A
-    
           't
@@ -1616,35 +1616,27 @@
 ity%0A        
-   
- 
 %7D,%0A
-    
         webs
@@ -1640,28 +1640,24 @@
 ebserver: %7B%0A
-    
           ho
@@ -1691,20 +1691,10 @@
     
- 
-   %7D%0A      
+%7D%0A
     

Make Sauce credentials optional in test tasks.