'use strict';

var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');

var NodeGenerator = module.exports = function NodeGenerator(args, options) {
  yeoman.generators.Base.apply(this, arguments);

  this.on('end', function () {
    this.installDependencies({
      bower: false,
      skipInstall: options['skip-install']
    });
  });

  this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname, '../package.json')));
};
util.inherits(NodeGenerator, yeoman.generators.NamedBase);

NodeGenerator.prototype.askFor = function askFor() {
  var cb = this.async();

  console.log(
    this.yeoman +
    '\nThe name of your project shouldn\'t contain "node" or "js" and' +
    '\nshould be a unique ID not already in use at search.npmjs.org.');

  var prompts = [{
    name: 'name',
    message: 'Module Name',
    default: path.basename(process.cwd())
  }, {
    name: 'description',
    message: 'Description',
    default: 'The best module ever.'
  }, {
    name: 'homepage',
    message: 'Homepage'
  }, {
    name: 'license',
    message: 'License',
    default: 'MIT'
  }, {
    name: 'githubUsername',
    message: 'GitHub username'
  }, {
    name: 'authorName',
    message: 'Author\'s Name'
  }, {
    name: 'authorEmail',
    message: 'Author\'s Email'
  }, {
    name: 'authorUrl',
    message: 'Author\'s Homepage'
  }];

  this.currentYear = (new Date()).getFullYear();

  this.prompt(prompts, function (props) {
    this.slugname = this._.slugify(props.name);
    this.safeSlugname = this.slugname.replace(
      /-([a-z])/g,
      function (g) { return g[1].toUpperCase(); }
    );

    if (props.githubUsername) {
      this.repoUrl = 'https://github.com/' + props.githubUsername + '/' + this.slugname;
    } else {
      this.repoUrl = 'user/repo';
    }

    if (!props.homepage) {
      props.homepage = this.repoUrl;
    }

    this.props = props;

    cb();
  }.bind(this));

};

NodeGenerator.prototype.askForModules = function askForModules() {
  var cb = this.async();

  var prompts = [{
    type: 'checkbox',
    name: 'modules',
    message: 'Which modules would you like to include?',
    choices: [{
      value: 'jscsModule',
      name: 'jscs (JavaScript Code Style checker)',
      checked: true
    }, {
      value: 'releaseModule',
      name: 'release (Bump npm versions with Gulp)',
      checked: true
    }, {
      value: 'istanbulModule',
      name: 'istanbul (JS code coverage tool)',
      checked: true
    }]
  }];


  this.prompt(prompts, function (props) {

    var hasMod = function (mod) { return props.modules.indexOf(mod) !== -1; };
    this.jscsModule = hasMod('jscsModule');
    this.releaseModule = hasMod('releaseModule');
    this.istanbulModule = hasMod('istanbulModule');
    this.coverallsModule = true;

  }.bind(this));

  if (this.istanbulModule) {

    var promptCoveralls = [{
      type: 'confirm',
      name: 'coverallsModule',
      message: 'Would you like add coveralls',
      default: true
    }];

    this.prompt(promptCoveralls, function (props) {

      this.coverallsModule = props.modules.indexOf('coverallsModule') !== -1;
      cb();

    }.bind(this));

  } else {
    cb();
  }

};

NodeGenerator.prototype.lib = function lib() {
  this.mkdir('lib');
  this.template('lib/name.js', 'lib/' + this.slugname + '.js');
};

NodeGenerator.prototype.test = function test() {
  this.mkdir('test');
  this.template('test/name_test.js', 'test/' + this.slugname + '_test.js');
};

NodeGenerator.prototype.example = function lib() {
  this.mkdir('example');
  this.template('example/simple.js', 'example/simple.js');
};

NodeGenerator.prototype.projectfiles = function projectfiles() {
  this.copy('jshintrc', '.jshintrc');
  this.copy('gitignore', '.gitignore');
  this.copy('travis.yml', '.travis.yml');
  this.copy('editorconfig', '.editorconfig');
  if (this.jscsModule) {
    this.copy('jscs.json', '.jscs.json');
  }

  this.template('README.md');
  this.template('gulpfile.js');
  this.template('_package.json', 'package.json');
};

@@ -2806,32 +2806,16 @@
 rue;%0A%0A  
-%7D.bind(this));%0A%0A
   if (th
@@ -2836,24 +2836,26 @@
 ule) %7B%0A%0A    
+  
 var promptCo
@@ -2869,24 +2869,26 @@
  = %5B%7B%0A      
+  
 type: 'confi
@@ -2894,24 +2894,26 @@
 irm',%0A      
+ 
+ 
 name: 'cover
@@ -2921,24 +2921,26 @@
 llsModule',%0A
+  
       messag
@@ -2976,24 +2976,26 @@
 lls',%0A      
+  
 default: tru
@@ -3000,21 +3000,25 @@
 rue%0A    
+ 
+ 
 %7D%5D;%0A%0A
+  
     this
@@ -3057,25 +3057,66 @@
 n (props) %7B%0A
-%0A
+        if (props && props.modules) %7B%0A    
       this.c
@@ -3181,16 +3181,85 @@
 !== -1;%0A
+        %7D else %7B%0A          this.coverallsModule = false;%0A        %7D%0A  
       cb
@@ -3267,16 +3267,18 @@
 );%0A%0A    
+ 
+ 
 %7D.bind(t
@@ -3285,16 +3285,18 @@
 his));%0A%0A
+  
   %7D else
@@ -3294,24 +3294,26 @@
    %7D else %7B%0A
+  
     cb();%0A  
@@ -3312,17 +3312,37 @@
 cb();%0A  
-%7D
+  %7D%0A%0A  %7D.bind(this));
 %0A%0A%7D;%0A%0ANo

fix coveralls prompt