const chalk = require('chalk');
const generator = require('yeoman-generator');


module.exports = {
    askForTableName,
    askForColumnsName
};

/**
 * Ask the table name for an entity
 */
function askForTableName() {
    let messageAddendum = '';
    let defaultValue = null;

    if (this.tableNameDBH !== undefined) {
        messageAddendum = `(currently : ${this.tableNameDBH})`;
        defaultValue = this.tableNameDBH;
    } else {
        defaultValue = this.entityTableName;
    }

    const done = this.async();
    this.prompt([
        {
            type: 'input',
            name: 'tableNameDBH',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'The table name cannot contain special characters';
                } else if (input === '') {
                    return 'The table name cannot be empty';
                } else if (this.prodDatabaseType === 'oracle' && input.length > 14) {
                    return 'The table name is too long for Oracle, try a shorter name';
                } else if (input.length > 30) {
                    return 'The table name is too long, try a shorter name';
                }
                return true;
            },
            message: 'What is the table name for this entity ?' + messageAddendum,
            default: defaultValue
        }
    ]).then((props) => {
        this.tableNameInput = props.tableNameDBH;
        done();
    });
}

/** For each field of an entity, ask the actual column name */
function askForColumnsName() {
    // Don't ask columns name if there aren't any field
    if (this.fields === undefined || this.fields.length === 0) {
        return;
    }

    this.log(chalk.green(`Asking column names for ${this.fields.length} field(s)`));
    const done = this.async();

    // work on a copy
    this.fieldsPile = this.fields.slice();
    // feed the first item for the first question
    this.field = this.fieldsPile.pop();
    askForColumnName.call(this, done);
}

/**
 * Ask the column name for the field of an entity
 * This function use this.fieldsPile, at each call it will pop an item from it and ask its question about it.
 * Then it will associate the answer with this item and push it to this.columnsInput.
 * So at the end of the recursion, this.fieldsPile will be empty and this.columnsInput full with what was in the former.
 **/
function askForColumnName(done) {
    let messageAddendum = '';
    let defaultValue = null;

    if (this.field.dbh_columnName !== undefined) {
        messageAddendum = `(currently : ${this.field.dbh_columnName})`;
        defaultValue = this.field.dbh_columnName;
    } else {
        defaultValue = this.field.fieldName;
    }

    const prompts = [
        {
            type: 'input',
            name: 'dbh_columnName',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'Your column name cannot contain special characters';
                } else if (input === '') {
                    return 'Your column name cannot be empty';
                } else if (this.prodDatabaseType === 'oracle' && input.length > 30) {
                    return 'The column name cannot be of more than 30 characters';
                }
                return true;
            },
            message: `What column name do you want for the field "${this.field.fieldName}" ? ${messageAddendum}`,
            default: defaultValue
        }
    ];

    this.prompt(prompts).then((props) => {
        this.field.columnNameInput = props.dbh_columnName;

        // push just processed item
        this.columnsInput.push(this.field);
        // pop item for next recursion
        this.field = this.fieldsPile.pop();

        if (this.field !== undefined) {
            askForColumnName.call(this, done);
        } else {
            done();
        }
    });
}


@@ -2021,24 +2021,116 @@
 ne);%0A%7D%0A%0A/**%0A
+ * Use $%7Bthis.field%7D which is set either by askForColumnsName or previous recursive call%0A *%0A
  * Ask the c
@@ -2188,16 +2188,18 @@
 ion use 
+$%7B
 this.fie
@@ -2205,16 +2205,17 @@
 eldsPile
+%7D
 , at eac
@@ -2347,16 +2347,18 @@
 h it to 
+$%7B
 this.col
@@ -2366,16 +2366,17 @@
 mnsInput
+%7D
 .%0A * So 
@@ -2404,16 +2404,18 @@
 ursion, 
+$%7B
 this.fie
@@ -2421,16 +2421,17 @@
 eldsPile
+%7D
  will be
@@ -2603,36 +2603,32 @@
  if (this.field.
-dbh_
 columnName !== u
@@ -2621,16 +2621,19 @@
 lumnName
+DBH
  !== und
@@ -2687,36 +2687,32 @@
  : $%7Bthis.field.
-dbh_
 columnName%7D)%60;%0A 
@@ -2705,16 +2705,19 @@
 lumnName
+DBH
 %7D)%60;%0A   
@@ -2747,20 +2747,16 @@
 s.field.
-dbh_
 columnNa
@@ -2753,24 +2753,27 @@
 d.columnName
+DBH
 ;%0A    %7D else
@@ -2905,20 +2905,16 @@
  name: '
-dbh_
 columnNa
@@ -2915,16 +2915,19 @@
 lumnName
+DBH
 ',%0A     
@@ -3684,12 +3684,8 @@
 ops.
-dbh_
 colu
@@ -3690,16 +3690,19 @@
 lumnName
+DBH
 ;%0A%0A     

replace forgotten prefix dbh_ with suffix DBH