var Compiler = require('codius-engine').Compiler;
var FileManager = require('codius-engine').FileManager;
var Engine = require('codius-engine').Engine;

var concat = require('concat-stream');
var fs = require('fs');
var tar = require('tar-stream');
var zlib = require('zlib');
var request = require('request');
var winston = require('winston');

var UploadCommand = function (codius) {
  this.codius = codius;
};

/**
 * Upload a contract
 */
UploadCommand.prototype.run = function () {
  var _this = this;

  var currentDir = process.cwd();

  var engineConfig = this.codius.config.getEngineConfig();

  var compiler = new Compiler(engineConfig);
  var fileManager = new FileManager(engineConfig);

  // TODO redo all of the CLI's config to clean it up a lot
  var hostUrl = this.codius.config.getCodiusHostUrl();

  var pack = tar.pack();
  var gzip = zlib.createGzip();
  compiler.on('file', function (event) {
    if (event.name.indexOf(currentDir) !== 0) {
      throw new Error('File path does not have current directory prefix: ' + event.name);
    }
    var filename = event.name.slice(currentDir.length);
    if (filename.indexOf('/') === 0) {
      filename = filename.slice(1);
    }
    pack.entry({ name: filename }, event.data);
  });

  var contractHash = compiler.compileModule(currentDir);
  pack.finalize();
  //pack.pipe(gzip).pipe(fs.createWriteStream('test.tar.gz')); return;
  winston.info('Uploading contract '+contractHash);

  console.log(this.codius.config.get('allowUnauthorizedSsl'));
  pack.pipe(gzip).pipe(request.post({
    url: hostUrl.apiPrefix+'/contract',
    rejectUnauthorized: !this.codius.config.get('allowUnauthorizedSsl')
  })).on('end', function () {
    winston.info('Generating token');
    request.post({
      url: hostUrl.apiPrefix+'/token?contract='+contractHash,
      rejectUnauthorized: !_this.codius.config.get('allowUnauthorizedSsl')
    }, function (error, res, body) {
      body = JSON.parse(body);
      winston.info('Contract ready at https://'+body.token+'.'+hostUrl.tokenSuffix+'/');

      // TODO This is temporary
      //request(hostUrl+body.token+'/').pipe(process.stdout);
    });
  });
};

exports.UploadCommand = UploadCommand;

var Compiler = require('codius-engine').Compiler;
var FileManager = require('codius-engine').FileManager;
var Engine = require('codius-engine').Engine;

var concat = require('concat-stream');
var fs = require('fs');
var tar = require('tar-stream');
var zlib = require('zlib');
var request = require('request');
var winston = require('winston');

var UploadCommand = function (codius) {
  this.codius = codius;
};

/**
 * Upload a contract
 */
UploadCommand.prototype.run = function () {
  var _this = this;

  var currentDir = process.cwd();

  var engineConfig = this.codius.config.getEngineConfig();

  var compiler = new Compiler(engineConfig);
  var fileManager = new FileManager(engineConfig);

  // TODO redo all of the CLI's config to clean it up a lot
  var hostUrl = this.codius.config.getCodiusHostUrl();

  var pack = tar.pack();
  var gzip = zlib.createGzip();
  compiler.on('file', function (event) {
    if (event.name.indexOf(currentDir) !== 0) {
      throw new Error('File path does not have current directory prefix: ' + event.name);
    }
    var filename = event.name.slice(currentDir.length);
    if (filename.indexOf('/') === 0) {
      filename = filename.slice(1);
    }
    pack.entry({ name: filename }, event.data);
  });

  var contractHash = compiler.compileModule(currentDir);
  pack.finalize();
  //pack.pipe(gzip).pipe(fs.createWriteStream('test.tar.gz')); return;
  winston.info('Uploading contract '+contractHash);

  pack.pipe(gzip).pipe(request.post({
    url: hostUrl.apiPrefix+'/contract',
    rejectUnauthorized: !this.codius.config.get('allowUnauthorizedSsl')
  })).on('end', function () {
    winston.info('Generating token');
    request.post({
      url: hostUrl.apiPrefix+'/token?contract='+contractHash,
      rejectUnauthorized: !_this.codius.config.get('allowUnauthorizedSsl')
    }, function (error, res, body) {
      body = JSON.parse(body);
      winston.info('Contract ready at https://'+body.token+'.'+hostUrl.tokenSuffix+'/');

      // TODO This is temporary
      //request(hostUrl+body.token+'/').pipe(process.stdout);
    });
  });
};

exports.UploadCommand = UploadCommand;

Remove stray debug statement.