
'use strict'

var util   = require('util')
  , path   = require('path')
  , fs     = require('fs')
  , yeoman = require('yeoman-generator')
  , exec   = require('child_process').exec

module.exports = Generator

function Generator() {
  yeoman.generators.Base.apply(this, arguments)

  this.sourceRoot(path.join(__dirname, 'templates'))
}

util.inherits(Generator, yeoman.generators.NamedBase)

Generator.prototype.askFor = function askFor() {
  var cb   = this.async()
    , self = this

  var prompts = [
    {
      name: 'appName',
      message: 'Name of the app',
      default: 'app'
    },
    {
      name: 'compassBootstrap',
      message: 'Would you like to include Twitter Bootstrap for Compass instead of CSS?',
      default: 'Y/n',
      warning: 'Yes: All Twitter Bootstrap files will be placed into the css directory.'
    },
    {
      name: 'bootstrap',
      message: 'Would you like to include the Twitter Bootstrap JS plugins?',
      default: 'Y/n',
      warning: 'Yes: All Twitter Bootstrap plugins will be placed into the JavaScript vendor directory.'
    },
    {
      name: 'frozenFlask',
      message: 'Do you want to use Frozen-Flask to build a static version of the app?',
      default: 'Y/n',
      warning: 'Yes: You will be able to build a static version of your app with `$ python freeze.py`.'
    }
  ]

  self.prompt(prompts, function(e, props) {
    if (e) { return self.emit('error', e) }

    // set the properties
    self.appName = props.appName
    self.compassBootstrap = (/y/i).test(props.compassBootstrap)
    self.bootstrap = (/y/i).test(props.bootstrap)
    self.frozenFlask = (/y/i).test(props.frozenFlask)

    cb()
  })
}

// The Bootstrap functions are copies from the basic Yeoman app generator, edited to fit with the flask app structure
// Bootstrap plugins
Generator.prototype.fetchBootstrap = function fetchBootstrap() {
  // prevent the bootstrap fetch is user said NO
  if(this.bootstrap) {

    var cb   = this.async()
      , self = this

    // third optional argument is the branch / sha1. Defaults to master when ommitted.
    this.remote('twitter', 'bootstrap', 'v2.1.0', function(err, remote, files) {
      if (err) { return cb(err) }

      'affix alert button carousel collapse dropdown modal popover scrollspy tab tooltip transition typeahead'.split(' ')
      .forEach(function( el ) {
        var filename = 'bootstrap-' + el + '.js'
        remote.copy('js/' + filename, self.appName + '/static/js/vendors/bootstrap/' + filename)
      })

      cb()
    })
  }
}

// Bootstrap css / scss
Generator.prototype.compassBootstrapFiles = function compassBootstrapFiles() {
  if ( this.compassBootstrap ) {
    var cb   = this.async()
      , self = this

    this.write(this.appName + '/static/css/main.scss', '@import "compass_twitter_bootstrap";')

    this.remote('kristianmandrup', 'compass-twitter-bootstrap', 'c3ccce2cca5ec52437925e8feaaa11fead51e132', function(err, remote) {
      if (err) { return cb(err) }

      remote.directory('stylesheets', self.appName + '/static/css')

      cb()
    })
  }
  else {
    this.log.writeln('Writing compiled Bootstrap')
    this.copy('bootstrap.css', this.appName + '/static/css/bootstrap.css')
  }
}

Generator.prototype.bootstrapImages = function bootstrapImages() {
  this.copy('glyphicons-halflings.png', this.appName + '/static/img/glyphicons-halflings.png')
  this.copy('glyphicons-halflings-white.png', this.appName + '/static/img/glyphicons-halflings-white.png')
}

// create the basic files
Generator.prototype.createAppFiles = function createAppFiles() {
  this.template('init.py', this.appName + '/__init__.py')
  this.template('server.py', 'server.py')
  this.template('views.py', this.appName + '/views.py')
  this.copy('index.html', this.appName + '/templates/index.html')
  this.copy('base.html', this.appName + '/templates/base.html')
  this.copy('jquery-1.8.3.min.js', this.appName + '/static/js/vendors/jquery-1.8.3.min.js')

  // make the server file executable
  exec('chmod a+x server.py')
}

// create the frozen-flask file to build a static version of the app
Generator.prototype.createFreezeFile = function createFreezeFile() {
  if (this.frozenFlask) {
    this.template('freeze.py', 'freeze.py')
  }
}

// create the yeoman and git files
Generator.prototype.createYeomanFiles = function createYeomanFiles() {
  this.copy('Gruntfile.js', 'Gruntfile.js')
  this.copy('gitignore', '.gitignore')
  this.copy('gitattributes', '.gitattributes')
}

Generator.prototype.end = function end() {
  this.log.writeln('\nYour app is ready !\n')
}

@@ -590,32 +590,55 @@
 p'%0A    %7D,%0A    %7B%0A
+      type: 'confirm',%0A
       name: 'com
@@ -743,38 +743,16 @@
  CSS?',%0A
-      default: 'Y/n',%0A
       wa
@@ -837,32 +837,55 @@
 .'%0A    %7D,%0A    %7B%0A
+      type: 'confirm',%0A
       name: 'boo
@@ -971,38 +971,16 @@
 gins?',%0A
-      default: 'Y/n',%0A
       wa
@@ -1067,32 +1067,33 @@
 ndor directory.'
+,
 %0A    %7D,%0A    %7B%0A  
@@ -1086,24 +1086,47 @@
    %7D,%0A    %7B%0A
+      type: 'confirm',%0A
       name: 
@@ -1232,30 +1232,8 @@
 ?',%0A
-      default: 'Y/n',%0A
     
@@ -1375,19 +1375,16 @@
 unction(
-e, 
 props) %7B
@@ -1388,53 +1388,8 @@
 ) %7B%0A
-    if (e) %7B return self.emit('error', e) %7D%0A%0A
     
@@ -1467,36 +1467,24 @@
 Bootstrap = 
-(/y/i).test(
 props.compas
@@ -1485,33 +1485,32 @@
 compassBootstrap
-)
 %0A    self.bootst
@@ -1515,28 +1515,16 @@
 strap = 
-(/y/i).test(
 props.bo
@@ -1530,17 +1530,16 @@
 ootstrap
-)
 %0A    sel
@@ -1558,20 +1558,8 @@
 k = 
-(/y/i).test(
 prop
@@ -1571,17 +1571,16 @@
 zenFlask
-)
 %0A%0A    cb

Update to new prompt library - fix #5