'use strict';

const configPath = 'build/config.json';
const fs = require('fs');
const glob = require('glob');
const prompt = require('Inquirer').prompt;
const util = require('util');
const _ = require('lodash');

const validators = {
  isNotEmpty: value => !!value ? true : 'Value is required!'
};

module.exports = {
  getWebtasks: () => _.flatten([
      'src/monitors/**/config.json',
      'src/notifiers/**/config.json'
    ].map(path => glob.sync(path).map(
      file => require(file)
    ))
  ),

  prompt: questions => new Promise(resolve =>
    prompt(questions, resolve)
  ),

  promptWebtaskSettings: wt => new Promise(resolve =>
    prompt(wt.prompt.map(params => ({
      type: params.type,
      name: params.name,
      message: util.format('[%s | %s] %s:', wt.type, wt.name, params.name),
      default: params.default || null,
      validate: validators.isNotEmpty
    })), resolve)
  ),

  getConfig: () => require(configPath),

  updateConfig: (name, settings) => new Promise((resolve, reject) => {
    let config = {};

    if (fs.existsSync(configPath)) {
      config = require(configPath);
    }

    config[name] = settings;

    fs.writeFile(configPath, JSON.stringify(config), err => {
      if (err) return reject(err);
      resolve();
    });
  }),

  validators: validators
};

@@ -153,20 +153,23 @@
 ;%0Aconst 
-util
+Promise
  = requi
@@ -176,14 +176,26 @@
 re('
-util')
+bluebird').Promise
 ;%0Aco
@@ -243,16 +243,16 @@
 ors = %7B%0A
-
   isNotE
@@ -303,16 +303,98 @@
 quired!'
+,%0A  atLeastOneSelected: data =%3E data.length ? true : 'Select at least one option!'
 %0A%7D;%0A%0Amod
@@ -840,55 +840,37 @@
 ge: 
-util.format('%5B%25s %7C %25s%5D %25s:', 
+%60%5B$%7B
 wt.type
-, 
+%7D %7C $%7B
 wt.name
-, 
+%7D%5D $%7B
 para
@@ -876,17 +876,19 @@
 ams.name
-)
+%7D:%60
 ,%0A      

Update gulp helper methods.