
/*!
 * Picky
 * @license MIT
 */

/*
 * Module dependencies
 */

var Suite = require('../suite');
var defaults = require('../defaults');

/*
 * Module variables
 */

var knownPlugins = [
	'picky-browser',
	'picky-mobile'
];

var knownReporters = [];
Object.keys(Suite.reporters).forEach(function (name) {
	if (knownReporters.indexOf(name.toLowerCase()) === -1) {
		knownReporters.push(name);
	}
});

/*
 * Module
 */

function getReporter(command, config) {
	return command.prompt('Choose a reporter', function () {
		if (!knownReporters.length) {
			return;
		}
		return {
			name: 'reporter',
			type: 'list',
			default: config._original.reporter || defaults.reporter,
			choices: knownReporters.map(function (name) {
				return {
					name: name
				};
			})
		};
	});
}

function getPlugins(command, config) {
	return command.prompt('Choose the plugins', function () {
		var availablePlugins = [].concat(knownPlugins);
		if (!config._original.plugins) {
			config._original.plugins = [];
		}
		config._original.plugins.forEach(function (name) {
			if (availablePlugins.indexOf(name) === -1) {
				availablePlugins.push(name);
			}
		});
		if (!availablePlugins.length) {
			return;
		}
		return {
			name: 'plugins',
			type: 'checkbox',
			choices: availablePlugins.map(function (name) {
				var short = name.replace(/^picky-/, '');
				return {
					name: short,
					value: name,
					short: short,
					checked: config._original.plugins.indexOf(name) !== -1
				};
			})
		};
	});
}

function getConfirmation(command, config) {
	return command.prompt('Confirm', function () {
		console.log();
		console.log(JSON.stringify(config, null, '  '));
		console.log();
		return {
			name: 'confirm',
			type: 'confirm',
			default: true
		};
	});
}

/*
 * Module exports
 */

exports.getReporter = getReporter;
exports.getPlugins = getPlugins;
exports.getConfirmation = getConfirmation;
