'use strict';

const config = require('config');
const crypto = require('crypto');

const style = require('../utils/style');

const dieFaces = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅'];

function onCommand(args, context) {
	const rolls = Number(args[0]) || 1;
	const faces = Number(args[1]) || 6;

	if (rolls > config.dice.maxRolls) {
		context.sendMessage(`You can only roll ${config.dice.maxRolls} dice at one time`, context.room.id);
		return;
	}

	if (rolls > config.dice.maxFaces) {
		context.sendMessage(`Your dice can have at most ${config.dice.maxFace} faces`, context.room.id);
		return;
	}

	const results = Array.from({ length: rolls }, () => {
		const result = crypto.randomInt(1, faces);

		// using U+2003 Em Space after dice to create double space that doesn't get filtered in SChat
		return `${style.grey(dieFaces[result - 1] || '▦')} ${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace
	});

	context.sendMessage(results.join(' | '), context.room.id);
}

module.exports = {
	onCommand,
	commands: ['dice', 'die', 'roll'],
};