'use strict';

var inquirer = require('inquirer'),
       chalk = require('chalk'),
      series = require('async-series'),
      prompt = require('prompt'),
      yeoman = require('yeoman-environment'),
         env = yeoman.createEnv(),
           s = require('underscore.string'),
          fs = require('fs'),
           _ = require('lodash');

exports.generationWizard = function(options) {

  series([
    function(done) {
      var choices  = [{
        value: 'ng-routable',
        name: 'Routable Angular Component --> Front-End',
      }];

      var prompts = [{
        type: 'list',
        name: 'blueprint',
        default: choices[0],
        message: 'What would you like to scaffold?',
        choices: choices
      }];

      inquirer.prompt(prompts, function(results) {

        console.log();

        switch (results.blueprint) {

          case 'ng-routable':
            generateNgRoute(options);
            break;

          default:
            console.log('Error: default switch case hit');
            break;
        }
      });
    }
  ],

  function(err) {

    if(err)
      throw err;

    terminateGracefully(options);
  });
}

var generateNgRoute = exports.generateNgRoute = function(options) {

  var cachedName = '',
      cachedPath = '',
   modulesSource = 'src/';

  series([

    function(done){

      var questions = [{
        type: 'list',
        name: 'moduleName',
        default: 'app',
        message: 'Which module does this route belongs to?',
        choices: []
      }, {
        type: 'input',
        name: 'name',
        message: 'What would you like to name this routable component?',
        default: options.name
      }, {
        type: 'input',
        name: 'path',
        message: 'What would you like the path to this component to be?',
        default: options.name
      }];

      // Add module choices
      if (fs.existsSync(modulesSource)) {

        fs.readdirSync(modulesSource).forEach(function (folder) {
          var stat = fs.statSync(modulesSource + '/' + folder);

          if (stat.isDirectory()
                // Exclude the `assets` and `sass` directories
                && folder !== 'assets'
                && folder !== 'sass') {
            console.log(folder);
            prompts[0].choices.push({
              value: folder,
              name: folder
            });
          }
        });
      }

      inquirer.prompt(questions, function(results) {

        var slugifiedName = s(results.name).humanize().slugify().value(),
           classifiedName = s(slugifiedName).classify().value(),
            humanizedName = s(slugifiedName).humanize().value(),
            camelizedName = s(slugifiedName).camelize().value(),
        decapitalizedName = s(results.name).humanize().decapitalize().value();

        var dest = modulesSource  + results.moduleName + '/' + decapitalizedName + '/';

        console.log();

        if(results.name.length > 0 ||
           results.path.length > 0) {

               cachedName = results.name;
               cachedPath = results.path;

               env.lookup(function () {
                 env.run('vulgar:ng-route', { 'vulgarcli': true, 'name': results.name, 'path': results.path, 'dest': dest }, done);
               });
           }

        else done();
      });
    }
  ], function(err) {

    if (err)
      throw err;

    console.log();
    console.log(chalk.green('  Generation of routable Angular component successful!'));
    console.log();
    console.log('  To utilize the ' + cachedName + ' component, open your main application')
    console.log('  component, import ' + cachedName + '-root.component.ts, and add')
    console.log('  it into the router config:');
    console.log();
    console.log('    @RouteConfig([');
    console.log('      { path: \'' + s(cachedName).camelize().value() + '/...\', name: \'' + s(cachedName).classify().value() + 'Root\', component: ' + s(cachedName).classify().value() + 'Root }');
    console.log('    ])');
    console.log('');

    terminateGracefully(options);
  });
}

function terminateGracefully(options) {

  console.log(
    chalk.green('Generation complete. ') +
    chalk.red('Process self-destructing...')
  );
  console.log('...');

  process.exit(0);
}

@@ -2272,40 +2272,8 @@
  %7B%0D%0A
-            console.log(folder);
 %0D%0A  
@@ -2282,22 +2282,24 @@
         
-prompt
+question
 s%5B0%5D.cho
@@ -3274,19 +3274,35 @@
 h, '
-dest': dest
+module': results.moduleName
  %7D, 

call proper array & add option to generator call