/* dendry
 * http://github.com/idmillington/dendry
 *
 * MIT License
 */
/*jshint indent:2 */
(function() {
  "use strict";

  var path = require('path');
  var fs = require('fs');
  var handlebars = require('handlebars');
  var async = require('async');
  var browserify = require('browserify');
  var uglify = require('uglify-js');

  var utils = require('../utils');
  var compiler = require('../../parsers/compiler');
  var cmdCompile = require('./compile').cmd;

  var loadGameAndSource = function(data, callback) {
    compiler.loadCompiledGame(data.compiledPath, function(err, game, json) {
      if (err) return callback(err);
      data.gameFile = json;
      data.game = game;
      callback(null, data);
    });
  };

  var browserifyUI = function(data, callback) {
    var b = browserify();
    b.add(path.resolve(__dirname, "../../ui/browser.js"));
    b.bundle(function(err, buffer) {
      if (err) return callback(err);
      var str = buffer.toString();
      data.browserify = str;
      return callback(null, data);
    });
  };

  var uglifyBundle = function(data, callback) {
    var gameFile = data.gameFile.toString();
    var wrappedGameFile = JSON.stringify({compiled:gameFile});
    var content = ["window.game="+wrappedGameFile+";", data.browserify];
    var result = uglify.minify(content, {fromString: true, compress:{}});
    data.code = content.join("");
    return callback(null, data);
  };

  var getTemplateDir = function(data, callback) {
    utils.getTemplatePath(
      data.template, "html",
      function(err, templateDir, name) {
        if (err) return callback(err);
        data.templateDir = templateDir;
        data.template = name;
        return callback(null, data);
      });
  };

  var getDestDir = function(data, callback) {
    data.destDir = path.join(data.projectDir, "out", "html");
    callback(null, data);
  };

  var notifyUser = function(data, callback) {
    console.log(("Creating HTML build in: "+data.destDir).grey);
    console.log(("Using template: "+data.template).grey);
    callback(null, data);
  };

  var createHTML = function(data, callback) {
    fs.exists(data.destDir, function(exists) {
      if (exists) {
        console.log("Warning: Overwriting existing HTML content.".red);
      }
      utils.copyTemplate(
        data.templateDir, data.destDir, data,
        function(err) {
          if (err) return callback(err);
          else return(null, data);
        });
    });
  };

  // ----------------------------------------------------------------------
  // Make-HTML: Creates a playable HTML version of the game.
  // ----------------------------------------------------------------------

  var cmdMakeHTML = new utils.Command("make-html");
  cmdMakeHTML.createArgumentParser = function(subparsers) {
    var parser = subparsers.addParser(this.name, {
      help: "Make a project into a playable HTML page.",
      description: "Builds a HTML version of a game, compiling it first "+
        "if it is out of date. The compilation uses a template which "+
        "can be a predefined template, or the path to a directory. "+
        "Templates use the handlebars templating system. The default "+
        "HTML template (called 'default') compresses the browser interface "+
        "and your game content and embeds it in a single HTML file, for the "+
        "most portable game possible."
    });
    parser.addArgument(['project'], {
      nargs: "?",
      help: "The project to compile (default: the current directory)."
    });
    parser.addArgument(['-t', '--template'], {
      help: "A theme template to use (default: the 'default' theme). "+
        "Can be the name of a built-in theme, or the path to a theme."
    });
    parser.addArgument(['-f', '--force'], {
      action: "storeTrue",
      default: false,
      help: "Always recompiles, even if the compiled game is up to date."
    });
  };
  cmdMakeHTML.run = function(args, callback) {
    var getData = function(callback) {
      cmdCompile.run(args, callback);
    };

    async.waterfall([getData, loadGameAndSource,
                     browserifyUI, uglifyBundle,
                     getTemplateDir, getDestDir,
                     notifyUser, createHTML], callback);
  };

  module.exports = {
    cmd: cmdMakeHTML
  };
}());

@@ -1279,18 +1279,94 @@
     
-var result
+if (data.no_uglify) %7B%0A      data.code = content.join('');%0A    %7D else %7B%0A      data.code
  = u
@@ -1423,43 +1423,20 @@
 %7B%7D%7D)
-;%0A    data.code = content.join(%22%22);
+.code;%0A    %7D
 %0A   
@@ -3764,32 +3764,200 @@
 theme.%22%0A    %7D);%0A
+    parser.addArgument(%5B'--no-uglify'%5D, %7B%0A      action: %22storeTrue%22,%0A      default: false,%0A      help: %22Doesn't run the output through uglify for compression.%22%0A    %7D);%0A
     parser.addAr

Add uglification back into the tool chain, but make it optional.