'use strict';

module.exports = function(kbox) {

  // Native modules
  var path = require('path');

  // Kalabox modules
  var meta = require('./meta.js');
  var util = require('./util.js')(kbox);

  /*
   * Adds the appropriate downloads to our list
   */
  if (util.needsDownloads()) {
    kbox.install.registerStep(function(step) {
      step.name = 'engine-docker-downloads';
      step.description = 'Queuing up provider downloads...';
      step.subscribes = ['core-downloads'];
      step.all = function(state) {

        // What platform are we on?
        var platform = process.platform;

        // Only grab profile if needed
        if (util.needsProfile()) {
          state.downloads.push(meta.PROVIDER_PROFILE_URL);
        }

        // Only grab B2D if needed
        if (util.needsB2D()) {
          state.downloads.push(meta.PROVIDER_DOWNLOAD_URL[platform].b2d);
        }

        // Add extra stuff for linux if appropriate
        if (platform === 'linux' && util.needsVB()) {
          var nix = kbox.install.linuxOsInfo.get();
          var vb = meta.PROVIDER_DOWNLOAD_URL.linux.vb;
          state.downloads.push(vb[nix.ID][nix.VERSION_ID]);
        }

        // Add extra stuff for Windosw if appropriate
        if (platform === 'win32' && util.needsInf()) {
          state.downloads.push(meta.PROVIDER_INF_URL);
        }

      };
    });
  }

  /*
   * Install the provider profile into the correct location
   */
  if (util.needsProfile()) {
    kbox.install.registerStep(function(step) {
      step.name = 'engine-docker-provider-profile';
      step.deps = ['core-downloads'];
      step.subscribes = ['engine-up'];
      step.description = 'Setting up the provider profile...';
      step.all = function(state) {

        // Install the profile
        util.installProfile(state);

        // Update our current install
        state.updateCurrentInstall({PROVIDER_PROFILE_VERSION: '0.10.0'});

      };
    });
  }

  /*
   * Installs the b2d bin into the correct location
   */
  if (util.needsB2D()) {
    kbox.install.registerStep(function(step) {
      step.name = 'engine-docker-provider-b2d';
      step.deps = ['core-downloads'];
      step.subscribes = ['engine-up'];
      step.description = 'Setting up the B2D binary...';
      step.all.linux = function(state) {

        // Install the profile
        util.installB2DLinux(state);

        // Update our current install
        state.updateCurrentInstall({PROVIDER_B2D_VERSION: '1.8.0'});

      };
    });
  }

  /*
   * Add in admin commands that are relevant to setting up the engine if
   * needed
   */
  if (util.needsAdminCommands()) {
    kbox.install.registerStep(function(step) {
      step.name = 'engine-docker-install-engine';
      step.description  = 'Queuing provider admin commands...';
      step.deps = ['core-auth'];
      step.subscribes = ['core-run-admin-commands'];
      step.all.darwin = function(state, done) {

        // Grab the target volume
        return kbox.util.disk.getMacVolume()

        // Extract the volume UUID and then add the command if
        // we need it
        .then(function(data) {

          // Analyze volume data
          var match = data.match(/Volume UUID:[ ]*(.*)\n/);

          // Fail if there is no match
          if (!match) {
            state.fail(state, 'Cannot get your Mac volume UUID!');
          }

          // Add the install B2D command if we need to
          if (util.needsB2D()) {

            // Get package location
            var pkgDir = kbox.util.disk.getTempDir();
            var pkgName = path.basename(meta.PROVIDER_DOWNLOAD_URL.darwin.b2d);
            var pkg = path.join(pkgDir, pkgName);

            // Generate and add the command
            var cmd = kbox.install.cmd.buildInstallCmd(pkg, match[1]);
            state.adminCommands.push(cmd);

          }
        })

        // Next step
        .nodeify(done);

      };
      step.all.linux = function(state) {

        // Add the VB install command if we need it
        if (util.needsVB()) {

          // Get the VB pkg location
          var nix = kbox.install.linuxOsInfo.get();
          var pkgDir = kbox.util.disk.getTempDir();
          var vb = meta.PROVIDER_DOWNLOAD_URL.linux.vb;
          var pkgName = path.basename(vb[nix.ID][nix.VERSION_ID]);
          var pkg = path.join(pkgDir, pkgName);

          // Add VB install as an admin command
          var cmd = kbox.install.cmd.buildInstallCmd(pkg, nix);
          state.adminCommands.push(cmd);
        }

      };
      step.all.win32 = function(state) {

        // Add the install B2D command if we need to
        if (util.needsB2D()) {

          // Get the B2D pkg location
          var pkgDir = kbox.util.disk.getTempDir();
          var pkgName = path.basename(meta.PROVIDER_DOWNLOAD_URL.win32.b2d);
          var pkg = path.join(pkgDir, pkgName);
          var infFile = path.join(pkgDir, path.basename(meta.PROVIDER_INF_URL));

          // Build the install command and add it
          var cmd = kbox.install.cmd.buildInstallCmd(pkg, infFile);
          state.adminCommands.push(cmd);
        }

      };

    });
  }

  /*
   * If we got this far we can assume our admin commands ran ok
   */
  kbox.install.registerStep(function(step) {
    step.name = 'engine-docker-verify-admin';
    step.deps = ['core-run-admin-commands'];
    step.description = 'Updating install info...';
    step.all = function(state) {

      // Update our current install to reflect B2D installed
      // NOTE: linux handles this seperately
      if (util.needsB2D() && process.platform !== 'linux') {
        state.updateCurrentInstall({PROVIDER_B2D_VERSION: '1.8.0'});
      }

      // Update our current install to reflect VB installed
      if (util.needsVB() && process.platform === 'linux') {
        state.updateCurrentInstall({PROVIDER_VB_VERSION: '5.0.3'});
      }

      // Update our current install to reflect INF installed
      if (util.needsInf() && process.platform === 'win32') {
        state.updateCurrentInstall({PROVIDER_INF_VERSION: '0.10.0'});
      }

    };
  });

  /*
   * Start up the engine
   */
  kbox.install.registerStep(function(step) {
    step.name = 'engine-up';
    step.deps = ['engine-docker-verify-admin'];
    step.description = 'Setting up and activating the engine...';
    step.all = function(state, done) {

      // Grab options if we have any
      // @todo: i think this is broken right now
      var options = {};
      if (state.disksize) {
        options.disksize = state.disksize;
      }

      // Init the engine
      kbox.engine.provider().call('up', options)

      // Fail the step on an error
      .catch(function(err) {
        state.fail(state, err);
      })

      // Debug log out output
      .then(function(data) {
        if (data) {
          state.log.debug(data);
        }
      })

      // Next step
      .nodeify(done);

    };
  });

};

@@ -2802,41 +2802,8 @@
 .';%0A
-      step.deps = %5B'core-auth'%5D;%0A
     

Remove core-auth as a dep