const inquirer = require('inquirer');
const files = require('./files');

module.exports = {

    generateEnvConfigQuestion: function( name, message, type, env, defaultVal=null ) {
        return {
                name: name,
                type: type=="boolean"?'confirm':'input',
                default:defaultVal,
                message: `${message} (${env}):`,
                validate: function (value) {
                    if (type==="url" && !value.match(/^(https?|chrome):\/\/[^\s$.?#].[^\s]*$/)) {
                        return 'Please enter a correct url.';
                    } else if (type=="input"){
                        return 'Please enter a value.';
                    }else {
                        return true;
                    }
                }
            }
    },

    askAppConfig: () => {
        const questions = [{
                name: 'name',
                type: 'input',
                default: files.getCurrentDirectoryBase() || null,
                message: 'Enter application name:',
                validate: function (value) {
                    if (value.length) {
                        return true;
                    } else {
                        return 'Please enter a valid application name.';
                    }
                }
            },
            {
                name: 'version',
                type: 'input',
                default: '1.0.0',
                message: 'Enter application version:',
                validate: function (value) {
                    if (value.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/)) {
                        return true;
                    } else {
                        return 'Please enter a valid version.';
                    }
                }
            }
        ];
        return inquirer.prompt(questions);
    },
    askEnvConfig: (env="local", defaultVal=null)=>{
        const envVariables=!defaultVal?[
            {name:"production", message:"Production",  type:"boolean", default:true},
            {name: "api_base_url", message:"API URL", type:"url", default:"https://localhost:5001/api"},
            {name: "app_url", message:"APP URL", type:"url", default:"http://localhost:4200"},
            {name: "identity_server_url", message:"Identity Server URL", type:"url", default:"https://dimeidentityserver-dev3.azurewebsites.net" },
            {name: "core_api_url", message:"CORE API URL", type:"url", default:"https://dimeservices-dev.azurewebsites.net/api" }
        ]:[
            {name:"production", message:"Production",  type:"boolean", default:defaultVal["production"]},
            {name: "api_base_url", message:"API URL", type:"url", default:defaultVal["api_base_url"]},
            {name: "app_url", message:"APP URL", type:"url", default:defaultVal["app_url"]},
            {name: "identity_server_url", message:"Identity Server URL", type:"url", default:defaultVal["identity_server_url"]},
            {name: "core_api_url", message:"CORE API URL", type:"url", default:defaultVal["core_api_url"] }
        ]
        const questions = envVariables.map((variable)=>{
            return module.exports.generateEnvConfigQuestion(variable.name, variable.message, variable.type, env, variable.default)
        })

        return inquirer.prompt(questions);
    },
    askAuthConfig: (env="local", defaultVal)=>{
        const envVariables=[
            {name: "stsServer", message:"Identity Server Url",  type:"url", default:defaultVal["identity_server_url"]},
            {name: "client_id", message:"Client ID", type:"input"},
            {name: "scope", message:"Scope", type:"input"},
            {name: "redirect_url", message:"Redirect Url",  type:"url", default:defaultVal["app_url"]},
            {name: "post_logout_redirect_uri", message:"Post logout redirect URL", type:"url", default:defaultVal["identity_server_url"] },
            {name: "silent_renew_url", message:"Silent Renewal URL", type:"url", default:defaultVal["core_api_url"] }
        ]
        const questions = envVariables.map((variable)=>{
            return module.exports.generateEnvConfigQuestion(variable.name, variable.message, variable.type, env, variable.default)
        })

        return inquirer.prompt(questions);
    }
};