const fs = require('fs');
const path = require('path');
const crypto = require('crypto');

module.exports = (ui, modules) => function* () {
    const ask = ui.input.text;
    const { bold, dim, yellow, green, cyan } = ui.colors;

    const CONFIG_PATH = modules.project.CONFIG_PATH;

    const checkAlreadyInitialized = () => {
        if (fs.existsSync(`${process.cwd()}${path.sep}${CONFIG_PATH}`)) {
            ui.error(
                `${yellow('comfy is already initialized!')}` +
                `\nYou can update your configuration by editing ${dim(CONFIG_PATH)}.`
            );
            ui.exit(1);
        }
    };

    const askProjectInfos = function* () {
        const folders = process.cwd().split(path.sep);
        const defaultProjectName = folders[folders.length - 1];
        const projectName = yield ask(`- What is your project name? [${defaultProjectName}]`);

        const defaultEnvironment = process.env.NODE_ENV || 'development';
        const environment = yield ask(`- What is your first environment? [${defaultEnvironment}]`);

        const defaultPassphrase = crypto.randomBytes(256).toString('hex');
        const passphrase = yield ask('- What is your encryption passphrase? [generated]');

        return {
            projectName: projectName || defaultProjectName,
            environment: environment || defaultEnvironment,
            passphrase: passphrase || defaultPassphrase,
        };
    };

    const addConfigToGitignore = function* () {
        const gitIgnoreConfig = yield ask(
            `${bold('You are in a git directory.')} ` +
            `Would you add ${dim(CONFIG_PATH)} to the ${dim('.gitignore')}? [yes]`
        );

        if (!['n', 'no', 'No', 'NO'].includes(gitIgnoreConfig)) {
            const gitignore = `${process.cwd()}${path.sep}.gitignore`;
            const alreadyExist = fs.existsSync(gitignore);

            yield cb => fs.writeFile(gitignore, `${CONFIG_PATH}\n`, { flag: alreadyExist ? 'a' : 'w' }, cb);
        }
    };

    // Starting
    checkAlreadyInitialized();

    ui.print('We just need a few informations about your project:');
    const { projectName, environment, passphrase } = yield askProjectInfos();

    ui.print('\nInitializing project configuration...');
    const project = yield modules.project.create(projectName, environment);

    ui.print(`\nConfiguration saved in ${dim(CONFIG_PATH)}.`);
    const isGitDirectory = fs.existsSync(`${process.cwd()}${path.sep}.git`);

    if (isGitDirectory) {
        yield addConfigToGitignore();
    }

    yield modules.project.saveToConfig(project, passphrase);

    ui.print(`
${bold(green('Successfully initialized comfy for this project'))}
`);

    ui.exit();
};

@@ -834,20 +834,19 @@
 What is 
-your
+the
  project
@@ -1003,20 +1003,19 @@
 What is 
-your
+the
  first e
@@ -1177,20 +1177,19 @@
 What is 
-your
+the
  encrypt
@@ -1541,104 +1541,38 @@
    %60
-$%7Bbold('You are in a git directory.')%7D %60 +%0A            %60Would you add $%7Bdim(CONFIG_PATH)%7D to the
+- Add the comfy config file to
  $%7Bd
@@ -1999,140 +1999,320 @@
 nt('
-We just need a few informations about your project:');%0A    const %7B projectName, environment, passphrase %7D = yield askProjectInfos();
+To create a comfy project for this directory, please answer the following questions:');%0A    const %7B projectName, environment, passphrase %7D = yield askProjectInfos();%0A    const isGitDirectory = fs.existsSync(%60$%7Bprocess.cwd()%7D$%7Bpath.sep%7D.git%60);%0A    if (isGitDirectory) %7B%0A        yield addConfigToGitignore();%0A    %7D
 %0A%0A  
@@ -2457,18 +2457,16 @@
 .print(%60
-%5Cn
 Configur
@@ -2503,160 +2503,11 @@
 TH)%7D
-.%60);%0A    const isGitDirectory = fs.existsSync(%60$%7Bprocess.cwd()%7D$%7Bpath.sep%7D.git%60);%0A%0A    if (isGitDirectory) %7B%0A        yield addConfigToGitignore();%0A    %7D
+%60);
 %0A%0A  
@@ -2580,17 +2580,16 @@
 .print(%60
-%0A
 $%7Bbold(g
@@ -2598,60 +2598,46 @@
 en('
-Successfully initialized comfy for this project
+comfy project successfully created
 '))%7D
-%0A
 %60);%0A

Update init command wording