const inquirer = require('inquirer');

const defaultConfig = {
  appName: 'my-app',
  appScope: '@app',
  appType: 'angular',
  appOptions: [],
  appOptionsEclTheme: 'ecl-ec',
  appFeatures: [],
  groupId: 'ec.europa.digit',
  artifactId: 'myapp',
  useYarn: true,
  npmInstall: true,
  appStart: true,
};

module.exports.start = (options) => {

  // if inputConfig is provided, we merge it with defaultConfig and return without CLI interactions
  if (options.inputConfig) {
    return {
      ...defaultConfig,
      ...options.inputConfig
    };
  }

  // CLI interactions
  return Promise.resolve()
    .then(() => {
      return inquirer.prompt([
        {
          type: 'list',
          name: 'appType',
          message: 'Select an application type :',
          choices: [
            { name: 'eUI Angular - Desktop', value: 'angular' },
            { name: 'eUI Angular - Mobile Ionic', value: 'mobile' },
            { name: 'eUI Angular + Spring Boot web module - JDK + Maven required !!!', value: 'web-spring-boot' },
            { name: 'eUI Angular + Maven web module (JEE Spring maven web module) - JDK + Maven required !!!', value: 'web-maven' },
            { name: 'eUI Angular + PHP/Symfony starter - PHP7 and composer required !!!', value: 'web-symfony' },
          ],
          default: 0,
          validate: function (value) {
            if (value.length) {
              return true;
            }
          }
        },
        {
          name: 'groupId',
          type: 'input',
          message: 'Enter the groupId :',
          default: defaultConfig.groupId,
          validate: function (value) {
            if (value.length) {
              return true;
            }
          },
          when: function (answers) {
            return (answers.appType === 'web-spring-boot');
          }
        },
        {
          name: 'artifactId',
          type: 'input',
          message: 'Enter the artifactId :',
          default: defaultConfig.artifactId,
          validate: function (value) {
            if (value.length) {
              return true;
            }
          },
          when: function (answers) {
            return (answers.appType === 'web-spring-boot');
          }
        },
        {
          type: 'checkbox',
          name: 'appOptions',
          default: defaultConfig.appOptions,
          message: 'Select one or more additional configuration options :',
          choices: [
            { name: 'eUI ECL app-shell template and configuration - for ec.europa.eu apps', value: 'ecl' },
          ],
          when: function (answers) {
            return (answers.appType !== 'mobile');
          }
        },

        {
          type: 'list',
          name: 'appOptionsEclTheme',
          default: defaultConfig.appOptionsEclTheme,
          message: 'Select which eUI ECL theme flavor you need :',
          choices: [
            { name: 'eUI ECL - EC theme', value: 'ecl-ec' },
            { name: 'eUI ECL - EU theme', value: 'ecl-eu' },
          ],
          when: function (answers) {
            return (answers.appOptions && answers.appOptions.includes('ecl') && answers.appType !== 'mobile');
          }
        },

        {
          name: 'npmInstall',
          type: 'confirm',
          message: 'Yarn install dependencies when the app is generated :',
          default: defaultConfig.npmInstall
        }
      ]);
    })
    .then((answers) => {
      let outputConfig;
      outputConfig = {
        ...defaultConfig,
        ...answers
      };
      console.log('--provided config : \n', outputConfig);
      return outputConfig;
    })
    .catch((e) => {
      throw e;
    })

}

