var spawn = require('cross-spawn'),
  path = require('path'),
  fs = require('fs'),
  async = require('async'),
  MobProxy = require('browsermob-proxy-api');

function Proxy(config) {
  this.port = config.port;
  if (config.basicAuth) {
    this.basicAuth = config.basicAuth;
  }
  if (config.headers) {
    this.headers = config.headers;
  }
  if (config.limit) {
    this.limit = config.limit;
  }

}

Proxy.prototype.start = function(cb) {
  // start the browsermob proxy
  this.java = spawn('java', ['-jar', path.join(__dirname, 'bmpwrapper-0.0.1-SNAPSHOT-full.jar'), '-port', '9093']);


  this.java.stdout.on('data', function(data) {
    console.log(data);
  });
  this.java.stderr.on('data', function(data) {
    console.log('e:' + data);
  });

  var self = this;

  setTimeout(function() {

    self.proxy = new MobProxy({
      'host': 'localhost',
      'port': '9093'
    });


    self.proxy.startPort(this.port, function(err, data) {
      async.series([

          function(callback) {
            self.proxy.createHAR(self.port, {
              'initialPageRef': 'foo',
              'captureHeaders': true
            }, callback);
          }
          /*,
          function(callback) {
            if (self.headers) {
              self.proxy.setHeaders(self.port, JSON.stringify(self.headers), callback);
            } else {
              callback();
            }
          },
          function(callback) {
            if (self.basicAuth) {
              self.proxy.setAuthentication(443, domain, json, callback);
            }
          },
          function(callback) {
            if (self.limit) {
              self.proxy.setLimit(self.port, self.limit, callback);
            }
          }*/
        ],
        // optional callback
        function(err, results) {
          cb();
        });
    });
  }, 2000);
};

Proxy.prototype.newPage = function(name) {
  this.proxy.startNewPage(this.port, name, function() {});
};

Proxy.prototype.stop = function(cb) {
  // close the proxy
  var self = this;
  this.proxy.stopPort(this.port, function() {

    try {
      self.java.kill('SIGHUP');
    } catch (error) {
      console.log(error);
    }
    cb();
  });

};

Proxy.prototype.saveHar = function(filename, cb) {
  this.proxy.getHAR(this.port, function(err, har) {
    // TODO for each page insert the timings

    fs.writeFile(filename, har, function(err) {
      if (err) {
        throw err;
      }
      cb();
    });
  });
};

module.exports = Proxy;
@@ -585,17 +585,21 @@
 93'%5D);%0A%0A
+  /*
 %0A
-
   this.j
@@ -748,16 +748,21 @@
 );%0A  %7D);
+%0A  */
 %0A%0A  var 
@@ -774,17 +774,68 @@
 = this;%0A
+  console.log('Waiting on the proxy to start ...');
 %0A
-
   setTim
@@ -848,25 +848,24 @@
 unction() %7B%0A
-%0A
     self.pro
@@ -963,28 +963,28 @@
 y.startPort(
-this
+self
 .port, funct
@@ -1216,21 +1216,8 @@
    %7D
-%0A          /*
 ,%0A  
@@ -1569,32 +1569,79 @@
 son, callback);%0A
+            %7D else %7B%0A              callback();%0A
             %7D%0A  
@@ -1772,32 +1772,79 @@
 mit, callback);%0A
+            %7D else %7B%0A              callback();%0A
             %7D%0A  
@@ -1833,32 +1833,32 @@
 ;%0A            %7D%0A
+
           %7D*/%0A  
@@ -1852,18 +1852,16 @@
        %7D
-*/
 %0A       

call the callbacks