require! {
  'chalk' : {cyan, green, red}
  'inquirer'
  'fs'
  'js-yaml' : yaml
  '../../logger' : Logger
  '../../../package.json' : {version}
  'path'
  'tmplconv'
}

console.log 'We are about to add a new Exosphere service to the application!\n'

questions =

  * message: 'Name of the service to create:'
    type: 'input'
    name: 'service-name'
    filter: (input) -> input.trim!
    validate: (input) -> input.length > 0

  * message: 'Description:'
    type: 'input'
    name: 'description'
    filter: (input) -> input.trim!

  * message: 'Type:'
    type: 'list'
    name: 'template-name'
    choices: service-names!

inquirer.prompt(questions).then (answers) ->
  src-path = path.join __dirname, '..' '..' '..' 'templates' 'add-service' answers['template-name']
  target-path = path.join __dirname, '..' '..' '..' 'tmp' answers['service-name']
  app-config = yaml.safe-load fs.read-file-sync('application.yml', 'utf8')
  answers['app-name'] = app-config.name
  console.log answers
  console.log!
  tmplconv.render(src-path, target-path, data: answers).then ->
    console.log green "\ndone"


# Returns the names of all known service templates
function service-names
  fs.readdir-sync path.join(__dirname, '..' '..' '..' 'templates' 'add-service')

require! {
  'chalk' : {cyan, green, red}
  'inquirer'
  'fs'
  'js-yaml' : yaml
  '../../logger' : Logger
  '../../../package.json' : {version}
  'path'
  'tmplconv'
}

console.log 'We are about to add a new Exosphere service to the application!\n'

questions =

  * message: 'Name of the service to create:'
    type: 'input'
    name: 'service-name'
    filter: (input) -> input.trim!
    validate: (input) -> input.length > 0

  * message: 'Description:'
    type: 'input'
    name: 'description'
    filter: (input) -> input.trim!

  * message: 'Type:'
    type: 'list'
    name: 'template-name'
    choices: service-names!

inquirer.prompt(questions).then (answers) ->
  src-path = path.join __dirname, '..' '..' '..' 'templates' 'add-service' answers['template-name']
  target-path = path.join process.cwd!, answers['service-name']
  app-config = yaml.safe-load fs.read-file-sync('application.yml', 'utf8')
  answers['app-name'] = app-config.name
  console.log answers
  console.log!
  tmplconv.render(src-path, target-path, data: answers).then ->
    console.log green "\ndone"


# Returns the names of all known service templates
function service-names
  fs.readdir-sync path.join(__dirname, '..' '..' '..' 'templates' 'add-service')
