const { Command } = require('discord.js-commando');
const { list } = require('../../util/Util');
const units = ['celsius', 'fahrenheit', 'kelvin'];

module.exports = class TemperatureCommand extends Command {
	constructor(client) {
		super(client, {
			name: 'temperature',
			aliases: ['temperature-convert', 'convert-temperature', 'temp-convert', 'convert-temp'],
			group: 'number-edit',
			memberName: 'temperature',
			description: `Converts temperatures to/from ${list(units, 'or')}.`,
			details: `**Units**: ${units.join(', ')}`,
			args: [
				{
					key: 'base',
					prompt: `What temperature unit do you want to use as the base? Either ${list(units, 'or')}.`,
					type: 'string',
					validate: base => {
						if (units.includes(base.toLowerCase())) return true;
						return `Invalid base, please enter either ${list(units, 'or')}.`;
					},
					parse: base => base.toLowerCase()
				},
				{
					key: 'target',
					prompt: `What temperature unit do you want to convert to? Either ${list(units, 'or')}.`,
					type: 'string',
					validate: target => {
						if (units.includes(target.toLowerCase())) return true;
						return `Invalid target, please enter either ${list(units, 'or')}.`;
					},
					parse: target => target.toLowerCase()
				},
				{
					key: 'amount',
					prompt: 'What temperature should be converted?',
					type: 'float'
				}
			]
		});
	}

	run(msg, { base, target, amount }) { // eslint-disable-line consistent-return
		if (base === target) return msg.say(`Converting ${base} to ${target} is the same value, dummy.`);
		if (base === 'celsius') {
			if (target === 'fahrenheit') return msg.say(`${amount}°C is ${(amount * 1.8) + 32}°F.`);
			if (target === 'kelvin') return msg.say(`${amount}°C is ${amount + 273.15}°K.`);
		}
		if (base === 'fahrenheit') {
			if (target === 'celsius') return msg.say(`${amount}°F is ${(amount - 32) / 1.8}°C.`);
			if (target === 'kelvin') return msg.say(`${amount}°F is ${(amount + 459.67) * (5 / 9)}°K.`);
		}
		if (base === 'kelvin') {
			if (target === 'celsius') return msg.say(`${amount}°K is ${amount - 273.15}°C.`);
			if (target === 'fahrenheit') return msg.say(`${amount}°K is ${(amount * 1.8) - 459.67}°F.`);
		}
	}
};
