'use strict'

const chalk = require('chalk')
const inquirer = require('inquirer')
const isGitUrl = require('is-git-url')
const helper = require('./helper')

const regex = /^[a-zA-Z0-9]+[a-zA-Z0-9_-]?$/
const regexBegin = /^[a-zA-Z0-9]/
const ssh = /^[A-Za-z][A-Za-z0-9_]*@[A-Za-z][A-Za-z0-9_.]*:(\/[A-Za-z][A-Za-z0-9_]*)*$/

class Questions {
  static gitContinue(callback) {
    const questions = [
      {
        type: 'confirm',
        name: 'gitContinue',
        message: `${chalk.red('Be careful')}, you seem to be already inside of a git repository. Continue?`,
      },
    ]
    inquirer.prompt(questions)
      .then((res) => {
        callback(res)
      })
  }

  static projectName(callback) {
    const questions = [
      {
        name: 'projectName',
        type: 'input',
        message: `How would you like to name your project? ${chalk.reset.italic('(kebab-case)')}`,
        validate(value) {
          if (value.length === 0) {
            return 'Please enter a project name.'
          } else if (!regex.test(value)) {
            if (!regexBegin.test(value.substr(0, 1))) {
              return 'The project must begin with an alphanumeric character'
            }
            return `${value} is not a valid project name.`
          }
          return true
        },
      },
    ]
    inquirer.prompt(questions)
      .then((res) => {
        callback(res)
      })
  }

  static projectDirectory(projectName, callback) {
    const questions = [
      {
        name: 'projectDirectory',
        type: 'input',
        message: `Where would you like to install this project? ${chalk.reset.italic('(leave empty for:')} ${chalk.reset.yellow.italic(`./${projectName}`)}${chalk.reset.italic(')')}`,
        validate(value) {
          if (value.length === 0) {
            if (helper.directoryExists(projectName)) {
              return `There is already a directory named ${projectName}`
            }
            return true
          }

          if (helper.directoryExists(value)) {
            return `There is already a directory named ${value}.`
          } else if (!regex.test(value)) {
            return `${value} is not a valid directory name.`
          }
          return true
        },
      },
    ]
    inquirer.prompt(questions)
      .then((res) => {
        callback(res)
      })
  }

  static projectGit(callback) {
    const questions = [
      {
        type: 'list',
        name: 'projectGit',
        message: 'Is there a git repository associated with this project? If so, do you wish to clone it now?',
        choices: [
          {
            name: 'No',
            value: 0,
          },
          {
            name: 'Yes, clone',
            value: 1,
          },
          {
            name: 'Yes, but continue without clonning',
            value: 2,
          },
        ],
        default: 0,
      },
    ]
    inquirer.prompt(questions)
      .then((res) => {
        callback(res)
      })
  }

  static gitRepo(callback) {
    const questions = [
      {
        name: 'gitRepo',
        type: 'input',
        message: 'Paste here the remote URL we should use? (https or ssh)',
        validate(value) {
          if (isGitUrl(value) || ssh.test(value)) {
            return true
          } else if (value.length === 0) {
            return 'Please enter a remote URL.'
          }
          return 'This git remote URL is not valid.'
        },
      },
    ]
    inquirer.prompt(questions)
      .then((res) => {
        callback(res)
      })
  }
}

module.exports = Questions

@@ -181,9 +181,11 @@
 0-9%5D
-+
+%7B1%7D
 %5Ba-z
@@ -194,12 +194,26 @@
 Z0-9
-_-%5D?
+-_%5D*%5Ba-zA-Z0-9%5D%7B1%7D
 $/%0Ac
@@ -222,21 +222,24 @@
 st regex
-Begin
+StartEnd
  = /%5E%5Ba-
@@ -1085,13 +1085,72 @@
 egex
-Begin
+StartEnd.test(value.substr(0, 1))%0A             %7C%7C !regexStartEnd
 .tes
@@ -1160,25 +1160,26 @@
 alue.substr(
-0
+-1
 , 1))) %7B%0A   
@@ -1220,16 +1220,22 @@
 t begin 
+/ end 
 with an 

update regex rule and validation for project name