'use strict';
const yeoman = require('yeoman-generator');
const chalk = require('chalk');
const _ = require('lodash');
const yosay = require('yosay');
const fs = require('fs');
const path = require('path');

const templatesFolder = path.join(__dirname, 'templates');

module.exports = yeoman.Base.extend({
  prompting: function() {
    return this.prompt([{
      type    : 'input',
      name    : 'name',
      message : 'Your service name',
      default : this.appname
    },
    {
      type    : 'input',
      name    : 'description',
      message : 'A brief description',
      default : ''
    },
    {
      type    : 'checkbox',
      name    : 'setup',
      message : 'Choose your components',
      choices: [
        { name: 'express'},
        { name: 'mongo'},
        { name: 'redis'},
        { name: 'postgres'}
      ]
    }]).then(function (_answers) {
      this.props = _answers;
      this.props.setup.push('basics');
    }.bind(this));
  },
  writing: {
    config: function() {
      this._copyConfigFiles();
      this._copyRootFiles();
    },
    app: function() {

    }
  },
  install: function() {
    this.installDependencies();
  },
  _copyRootFiles: function() {
    const prefix = 'root';
    const rootFiles = fs.readdirSync(path.join(templatesFolder, prefix));
    const self = this;
    _.forEach(rootFiles, function(file) {
      self.fs.copyTpl( self.templatePath(`./${prefix}/${file}`), self.destinationPath(file.replace(/^_/, '')), self.props );
    });
  },
  _copyConfigFiles: function() {
    const prefix = 'conf';
    const configFiles = fs.readdirSync(path.join(templatesFolder, prefix));
    const self = this;
    _.forEach(configFiles, function(file) {
      self.fs.copyTpl( self.templatePath(`./${prefix}/${file}`), self.destinationPath(prefix + '/' + file.replace(/^_/, '')), self.props );
    });
  }
});

// The available priorities are (in running order):

// initializing - Your initialization methods (checking current project state, getting configs, etc)
// prompting - Where you prompt users for options (where you'd call this.prompt())
// configuring - Saving configurations and configure the project (creating .editorconfig files and other metadata files)
// default - If the method name doesn't match a priority, it will be pushed to this group.
// writing - Where you write the generator specific files (routes, controllers, etc)
// conflicts - Where conflicts are handled (used internally)
// install - Where installation are run (npm, bower)
// end - Called last, cleanup, say good bye, etc


@@ -1019,20 +1019,28 @@
 copy
-Config
 Files(
+'conf', 'conf'
 );%0A 
@@ -1058,18 +1058,25 @@
 copy
-Root
 Files(
+'root', '.'
 );%0A 
@@ -1187,12 +1187,8 @@
 copy
-Root
 File
@@ -1203,375 +1203,19 @@
 ion(
-) %7B%0A    const prefix = 'root';%0A    const rootFiles = fs.readdirSync(path.join(templatesFolder, prefix));%0A    const self = this;%0A    _.forEach(rootFiles, function(file) %7B%0A      self.fs.copyTpl( self.templatePath(%60./$%7Bprefix%7D/$%7Bfile%7D%60), self.destinationPath(file.replace(/%5E_/, '')), self.props );%0A    %7D);%0A  %7D,%0A  _copyConfigFiles: function() %7B%0A    const prefix = 'conf';
+from, to) %7B
 %0A   
@@ -1277,22 +1277,20 @@
 Folder, 
-prefix
+from
 ));%0A    
@@ -1398,22 +1398,20 @@
 th(%60./$%7B
-prefix
+from
 %7D/$%7Bfile
@@ -1440,14 +1440,10 @@
 ath(
-prefix
+to
  + '

Refactor into a single copyFile func