'use strict';

const _yeoman = require('yeoman-generator');
const _decamelize = require('decamelize');

const _consts = require('../../utils/constants');

module.exports = _yeoman.Base.extend({
    /**
     * Initializes the generator.
     */
    constructor: function() {
        _yeoman.Base.apply(this, arguments);
    },

    /**
     * Shows a the title of the sub generator, and a brief description.
     */
    showTitle: function() {
        this.log(_consts.SEPARATOR);
        this.log('Create a model for a REST request/response:\n');
    },

    /**
     * Gathers model information
     */
    gatherModelInfo: function () {
        const prompts = [{
            type: 'input',
            name: 'apiModelName',
            message: 'Model Name (ex: CreateUserRequest)?',
            validate: (response) => {
                const pattern = /^[a-zA-Z0-9]+$/;
                if(response.match(pattern)) {
                    return true;
                }
                return 'A valid model name must be specified';
            }
        }, {
            type: 'input',
            name: 'apiModelDescription',
            message: 'Model Description?',
        }];

        return this.prompt(prompts).then((props) => {
            this.props = Object.assign(this.props || {}, props);
        });
    },

    /**
     * Generates target file names.
     */
    generateTargetFileNames: function() {
        const templateFile = _decamelize(this.props.apiModelName)
                                .replace(/_/g, '-');

        this.props.modelTemplateFile = `${templateFile}-model`;
    },

    /**
     * Creates cloud formation template for the api model
     */
    createModelTemplate: function() {
        this.fs.copyTpl(
            this.templatePath(`resources/api/_models/model.js`),
            this.destinationPath(`resources/api/_models/${this.props.modelTemplateFile}.js`),
            this.props
        );
    },

    /**
     * Finish up and show user messae.
     */
    finish() {
        this.log('A new model file will be created. You will have to update the model with the appropriate JSON schema');
    }
});

@@ -448,195 +448,426 @@
     
-this.log(_consts.SEPARATOR);%0A        this.log('Create a model for a REST request/response:%5Cn');%0A    %7D,%0A%0A    /**%0A     * Gathers model information%0A     */%0A    gatherModelInfo: function () %7B
+const title = this.options.apiModelTitle %7C%7C 'Create a model for a REST request/response';%0A        this.log(_consts.SEPARATOR);%0A        this.log(title);%0A    %7D,%0A%0A    /**%0A     * Gathers model information%0A     */%0A    gatherModelInfo: function () %7B%0A        this.props = this.props %7C%7C %7B%7D;%0A        this.props.apiModelName = this.options.apiModelName;%0A        this.props.apiModelDescription = this.options.apiModelDescription;
 %0A   
@@ -1264,32 +1264,87 @@
 ';%0A            %7D
+,%0A            when: answers =%3E !this.props.apiModelName
 %0A        %7D, %7B%0A  
@@ -1448,24 +1448,85 @@
 cription?',%0A
+            when: answers =%3E !this.props.apiModelDescription%0A
         %7D%5D;%0A

Add support to be invoked as a sub generator