'use strict'

const path = require('path')
const chalk = require('chalk')
const fs = require('fs')
const Config = require('./config')
const Templates = require('./templates')
const Prompt = require('./prompt')
const Parser = require('./parser')

const config = new Config()
const templatesPath = config.read().templatesPath
const templates = new Templates(templatesPath)
const prompt = new Prompt()
const parser = new Parser()

module.exports = class Commands {
  config () {
    let cfg = config.read()
    let questions = [
      {
        name: 'templatesPath',
        message: 'Templates Path',
        default: cfg.templatesPath,
        validate: function (val) {
          if (fs.existsSync(val)) {
            return true
          } else {
            return 'Directory "' + val + '" does not exist'
          }
        }
      },
      {
        name: 'handlebarsDelimiters',
        message: 'Handlebars Delimiter',
        default: cfg.handlebarsDelimiters,
        validate: function (val) {
          if (val.indexOf('[') !== -1 && val.indexOf(']')) {
            return 'Can not contain "[]"' // due to handlebars-delimiters regex problem
          }

          if (val.split(' ').length !== 2) {
            return 'Must have format "open[SPACE]close", for example "<% %>"'
          }

          return true
        }
      }
    ]

    prompt.ask(questions, (answers) => {
      config.save(answers)
      console.log(chalk.green('Saved.'))
    })
  }

  list () {
    let templatesObj = templates.getTemplates()
    let templatesLog = []

    for (let templateName in templatesObj) {
      let template = templatesObj[templateName]
      let meta = chalk.green(templateName)
      let files = chalk.grey(template.files.join(' | '))

      if (template.description) {
        meta += chalk.yellow(' - ' + template.description)
      }

      templatesLog.push(meta + '\n' + files)
    }

    console.log(templatesLog.join('\n\n'))
  }

  create (templateName) {
    let templatesObj = templates.getTemplates()
    let parseTemplate = function (templateName) {
      let templatePath = path.join(templatesPath, templateName)
      let filesArr = templatesObj[templateName].files
      let variables = parser.parseFiles(templatePath, filesArr)
      let questions = prompt.questionsFromVariables(variables)

      prompt.ask(questions, (answers) => {
        console.log(answers)
      })
    }

    if (templateName) {
      if (templatesObj[templateName]) {
        parseTemplate(templateName)
      } else {
        console.log(
          chalk.red('Can not find template "' + templateName + '".\n') +
          chalk.yellow('List templates with: ') +
          'lehm list'
        )
      }
    } else {
      let questions = prompt.questionsFromTemplates(templatesObj)

      prompt.ask(questions, (answers) => {
        parseTemplate(answers.template)
      })
    }
  }
}

@@ -1041,10 +1041,10 @@
  -1 
-&&
+%7C%7C
  val
@@ -1056,16 +1056,23 @@
 xOf('%5D')
+ !== -1
 ) %7B%0A    

correct check for []