var generators = require('yeoman-generator');
var util = require('../util');

var controller = {};

module.exports = generators.Base.extend({
  
  // The name `constructor` is important here
  constructor: function () {
    // Calling the super constructor is important so our generator is correctly set up
    generators.Base.apply(this, arguments);

    var self = this;
    
    self.argument('name', { type: String, required: false });
    self.option('path');
    
    controller.name = self.name;
    controller.path = util.normalizePath(self.options.path);
    
    var parentAssets = self.config.get("assetsFolder");
    
    var appAssets = (parentAssets == "public/") ? "public/app/" : "app/";
    
    controller.assets = appAssets + "controllers/";

  },
  
  prompting: function () {
    
    var questions = [{
      type    : 'input',
      name    : 'name',
      message : 'Controller Name',
      default : 'myController',
      when    : function(answers){
        
        answers.name = controller.name;
        
        return !controller.name;
        
      }
    }];
    
    return this.prompt(questions).then(function (answers) {

      controller.name = answers.name || controller.name;
      
    }.bind(this));
    
  },
  
  writing: function () {
    
    var self = this;
    
    self.composeWith('bookon:component', {

        args: [controller.name],

        options: {
          type: "controller",
          path: (controller.path || controller.assets)
        }

    });
    
  }

});