'use strict';
var path = require('path');
var opts = {alias: {tmpl: 't'}, default: {tmpl: 'page'}};
var debug = require('debug')('generate:doc');
var utils = require('./lib/utils');

module.exports = function(app, base) {
  if (this.isRegistered('assemble-docs-generate-doc')) return;
  debug('initializing <%s>, from <%s>', __filename, module.parent.id);

  /**
   * Use `generate-mocha` to pull in instance and pipeline plugins
   */

  app.use(require('generate-mocha'));

  /**
   * Set options
   */

  app.option('renameFile', function(file) {
    file.stem = 'page';
    return file;
  });

  /**
   * Default variables
   */

  var cwd = process.cwd();
  var defaultDest = 'templates/pages';
  var defaultName = 'page';

  /**
   * Tasks
   */

  app.task('templates', function*() {
    app.debug('loading templates');
    app.templates(['./scaffolds/**/*.{hbs,md}'], { cwd: cwd });
    app.debug('loaded templates');
  });

  app.task('dest', {silent: true}, function(cb) {
    app.question('dest', 'Destination directory?', {default: defaultDest});
    app.ask('dest', {save: false}, function(err, answers) {
      if (err) return cb(err);
      app.option('dest', answers.dest);
      cb();
    });
  });

  app.task('name', {silent: true}, function(cb) {
    app.question('name', 'File name?', {default: defaultName});
    app.ask('name', {save: false}, function(err, answers) {
      if (err) return cb(err);
      defaultName = answers.name;
      cb();
    });
  });

  createTask('api', {defaultName: 'api', defaultDest: 'content/api'});
  createTask('page', {defaultName: 'page', defaultDest: 'templates/pages'});
  createTask('recipe', {defaultName: 'recipe', defaultDest: 'content/recipes'});
  createTask('subject', {defaultName: 'subject', defaultDest: 'content/subjects'});

  app.task('default', ['page']);

  /**
   * Dynamically create a set of tasks for a specific tempate being generated.
   */

  function createTask(name, options) {
    var opts = utils.extend({defaultName: 'page', defaultDest: 'templates/pages'}, options);

    app.task(name + ':set-dest', {silent: true}, function*() {
      defaultDest = opts.defaultDest;
    });

    app.task(name + ':set-name', {silent: true}, function*() {
      defaultName = opts.defaultName;
    });

    app.task(name, [name + ':set-dest', name + ':set-name', 'templates', 'name', 'dest'], function() {
      app.debug(`generating "${name}" file`);
      var dest = app.option('dest') || app.cwd;

      // add `options` to the context
      app.data({ options: app.options });

      return app.toStream('templates')
        .pipe(filter(name))
        .pipe(app.renderFile('*'))
        .pipe(app.renameFile(function(file) {
          file.path = path.join(cwd, dest, defaultName + file.extname);
          return file;
        }))
        .pipe(app.conflicts(dest))
        .pipe(app.dest(dest));
    });
  }

};

function filter(pattern, options) {
  var isMatch = utils.match.matcher(pattern, options);

  return utils.through.obj(function(file, enc, next) {
    if (file.isNull()) {
      next();
      return;
    }

    if (isMatch(file)) {
      next(null, file);
    } else {
      next();
    }
  });
}

@@ -39,66 +39,8 @@
 ');%0A
-var opts = %7Balias: %7Btmpl: 't'%7D, default: %7Btmpl: 'page'%7D%7D;%0A
 var 
@@ -1435,32 +1435,25 @@
 ask('api', %7B
-defaultN
+n
 ame: 'api', 
@@ -1450,31 +1450,24 @@
 e: 'api', de
-faultDe
 st: 'content
@@ -1493,32 +1493,25 @@
 sk('page', %7B
-defaultN
+n
 ame: 'page',
@@ -1505,39 +1505,32 @@
 name: 'page', de
-faultDe
 st: 'templates/p
@@ -1558,32 +1558,25 @@
 ('recipe', %7B
-defaultN
+n
 ame: 'recipe
@@ -1576,31 +1576,24 @@
 'recipe', de
-faultDe
 st: 'content
@@ -1630,24 +1630,17 @@
 ject', %7B
-defaultN
+n
 ame: 'su
@@ -1645,31 +1645,24 @@
 subject', de
-faultDe
 st: 'content
@@ -1870,24 +1870,17 @@
 extend(%7B
-defaultN
+n
 ame: 'pa
@@ -1886,23 +1886,16 @@
 age', de
-faultDe
 st: 'tem
@@ -2011,23 +2011,16 @@
  opts.de
-faultDe
 st;%0A    
@@ -2112,24 +2112,17 @@
  = opts.
-defaultN
+n
 ame;%0A   

clean up