"use strict";

var chalk = require('chalk');
var inquirer = require('inquirer');
var fs = require('fs-extra');
var path = require('path');
var extend = require('extend');

var fileTemplater = require('../fileTemplater')();
var promptOptions = require('../promptOptions')();
var pkg = require(path.resolve(__dirname, '..', '..' ,'package.json'));

var _promptAnswers;

module.exports = function(appDir) {

    return {
        init: init
    };

    function init() {
        console.log('\n');
        console.log(chalk.bold('Running through setup for a new project.'));
        console.log(chalk.underline('This can be exited out by pressing [Ctrl+C]'));
        console.log('\n');

        inquirer.prompt(promptOptions.newOptions, inquirerCallback);
    }

    function getTemplateData() {
        var date = new Date();

        return {
            projectNameFileName: _promptAnswers.projectName.toLowerCase().replace(/ /g,"-"),
            projectGeneratedDate: [date.getDate(), date.getMonth() + 1, date.getFullYear()].join('/'),
            slateCurrentVersion: pkg.version
        }
    }ß

    function inquirerCallback(answers) {
        _promptAnswers = answers;

        if(_promptAnswers.isOkToCopyFiles) {
            console.log('\n');
            console.log('> Copying over files...');

            fs.copy(appDir, process.cwd(), {
                filter: function(path) {
                    return path.indexOf("node_modules") === -1;
                }
            }, function (err) {
                if (err) return console.error(err)

                templateCopiedFiles();
            })

        } else {
            console.log('User cancelled - no files copied')
        }
    }

    function templateCopiedFiles() {
        console.log('> Templating files...');

        var templateData = extend({}, _promptAnswers, getTemplateData())

        fileTemplater.setTemplateData(templateData);
        fileTemplater.setFileList([
            path.join('_config', 'creds.json'),
            'package.json',
            '.slaterc'
        ]);

        fileTemplater.run(templateFinished);
    }

    function templateFinished() {
        console.log('> Installation complete ✓');

        console.log('\n');
        console.log('You\'re nearly there!');
        console.log(chalk.underline('Next steps:'));
        console.log('  Run  `npm install` to setup all dependencies');
        console.log('  Run `gulp` for initial setup, `gulp watch` to setup and forget about the build');
        console.log('\n');
    }
}
@@ -469,34 +469,752 @@
        c
-onsole.log('%5Cn');%0A
+heckIfWorkingDirIsEmpty();%0A    %7D%0A%0A    function checkIfWorkingDirIsEmpty() %7B%0A        fs.readdir(process.cwd(), function(err, files) %7B%0A            if (err) return console.error(err);%0A%0A            if(files.length %3E 1) %7B%0A                console.log('%5Cn');%0A                console.log(chalk.underline('Warning: The directory you are currently in is not empty!'));%0A                console.log(chalk.underline('Going through the setup will perform a clean slate installation.'));%0A                console.log(chalk.underline('This will overwrite any user changes'));%0A                console.log('%5Cn');%0A%0A                inquirer.prompt(promptOptions.newOptions, inquirerCallback);%0A            %7D else %7B%0A                console.log('%5Cn');%0A        
         
@@ -1282,32 +1282,40 @@
 ct.'));%0A        
+  
+      
 console.log(chal
@@ -1375,32 +1375,40 @@
 +C%5D'));%0A        
+      
+  
 console.log('%5Cn'
@@ -1403,32 +1403,40 @@
 ole.log('%5Cn');%0A%0A
+        
         inquirer
@@ -1484,24 +1484,49 @@
 rCallback);%0A
+            %7D%0A        %7D)%0A
     %7D%0A%0A    f
@@ -2316,16 +2316,17 @@
 ror(err)
+;
 %0A%0A      

Check if current working dir is empty and display user message if it isn't