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


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

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

  this.pkg = JSON.parse(this.readFileAsString(path.join(__dirname,
                                                        '../package.json')));
};

util.inherits(FirefoxOSGenerator, yeoman.generators.NamedBase);

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

  // welcome message
  var welcome =
  '\n     _-----_' +
  '\n    |       |' +
  '\n    |' + '--(o)--'.red + '|   .--------------------------.' +
  '\n   `---------´  |    ' + 'Welcome to Yeoman,'.yellow.bold + '    |' +
  '\n    ' + '( '.yellow + '_' + '´U`'.yellow + '_' + ' )'.yellow + '   |   ' + 'ladies and gentlemen!'.yellow.bold + '  |' +
  '\n    /___A___\\   \'__________________________\'' +
  '\n     |  ~  |'.yellow +
  '\n   __' + '\'.___.\''.yellow + '__' +
  '\n ´   ' + '`  |'.red + '° ' + '´ Y'.red + ' `\n';

  console.log(welcome);

  var prompts = [{
    name: 'appName',
    message: 'What do you want to call your app',
    default: 'My Firefox OS App'
  }, {
    name: 'devUserName',
    message: 'What is your Github username',
    default: 'rick-astley'
  }, {
    type: 'confirm',
    name: 'shallUseGaiaBB',
    message: 'Include Gaia\'s Building Blocks (default: ' + 'YES'.bold + ')',
    default: true
  }];

  this.prompt(prompts, function (props) {
    this.appName = props.appName;
    this.devUserName = props.devUserName;
    this.shallUseGaiaBB = props.shallUseGaiaBB;

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

FirefoxOSGenerator.prototype._copyGaiaBB = function (dst) {
  var done = this.async();

  console.log('Gaia repository will now be downloaded.\nDepending on your' +
              'connection, this will probably take a while.\n' +
              'Don\'t panic!'.yellow);

  this.remote('mozilla-b2g', 'gaia', function (err, remote) {
    if (err) {
      console.log('Error fetching Gaia');
    }
    else {
      remote.directory('shared/style', dst);
    }
    done();
  });
};

FirefoxOSGenerator.prototype.app = function app() {
  // app skeleton
  this.mkdir('app');
  this.mkdir('app/icons');
  this.mkdir('app/scripts');
  this.mkdir('app/scripts/vendor');
  this.mkdir('app/images');
  this.mkdir('app/styles');
  this.copy('app/_manifest.webapp', 'app/manifest.webapp');
  this.copy('app/_index.html', 'app/index.html');
  this.copy('app/icons/120x120.png', 'app/icons/120x120.png');
  this.copy('app/icons/128x128.png', 'app/icons/128x128.png');
  this.copy('app/icons/60x60.png', 'app/icons/60x60.png');
  this.copy('app/scripts/main.js', 'app/scripts/main.js');
  this.copy('app/styles/main.sass', 'app/styles/main.sass');

  // test framework
  this.mkdir('test');
  this.mkdir('test/lib');
  this.copy('test/index.html', 'test/index.html');
  this.copy('test/lib/mocha.js', 'test/lib/mocha.js');
  this.copy('test/lib/mocha.css', 'test/lib/mocha.css');
  this.copy('test/lib/sinon.js', 'test/lib/sinon.js');
  this.copy('test/lib/chai.js', 'test/lib/chai.js');

  // copy gaia's BB
  if (this.shallUseGaiaBB) {
    this.mkdir('app/styles/gaiabb');
    this._copyGaiaBB('app/styles/gaiabb');
  }
};

FirefoxOSGenerator.prototype.projectfiles = function projectfiles() {
  this.copy('package.json', 'package.json');
  this.copy('_Gruntfile.js', 'Gruntfile.js');
  this.copy('jshintrc', '.jshintrc');
  this.copy('_README.md', 'README.md');
};


@@ -1883,24 +1883,44 @@
 his.async();
+%0A  var _this = this;
 %0A%0A  console.
@@ -2097,40 +2097,141 @@
 %0A%0A  
-this.remote('mozilla-b2g', 'gaia
+var bbSrc = 'https://github.com/buildingfirefoxos/' +%0A    'Building-Blocks/archive/gh-pages.tar.gz';%0A%0A  this.tarball(bbSrc, '.tmp/bb/
 ', f
@@ -2246,16 +2246,8 @@
 (err
-, remote
 ) %7B%0A
@@ -2299,17 +2299,37 @@
 ing Gaia
-'
+ Building Blocks'.red
 );%0A    %7D
@@ -2350,14 +2350,89 @@
     
-remote
+var root = _this.sourceRoot();%0A      _this.sourceRoot(process.cwd());%0A      _this
 .dir
@@ -2443,14 +2443,15 @@
 ry('
-shared
+.tmp/bb
 /sty
@@ -2461,16 +2461,46 @@
 , dst);%0A
+      _this.sourceRoot(root);%0A
     %7D%0A  

ADD downloading gaia BB from tarball instead of cloning repo