const Command = require("../../base/Command"),
	Discord = require("discord.js"),
	md5 = require("md5");

class Lovecalc extends Command {
	constructor(client) {
		super(client, {
			name: "lovecalc",
			dirname: __dirname,
			enabled: true,
			guildOnly: true,
			aliases: ["lc"],
			memberPermissions: [],
			botPermissions: ["SEND_MESSAGES", "EMBED_LINKS"],
			nsfw: false,
			ownerOnly: false,
			cooldown: 1000
		});
	}

	async run(message, args, data) {
		const firstMember = message.mentions.members.filter(m => m.id !== message.author.id).first();
		if (!firstMember) return message.error("fun/lovecalc:MISSING");
		const secondMember = message.mentions.members
			.filter(m => m.id !== firstMember.id)
			.filter(m => m.id !== message.author.id)
			.first() || message.member;
		if (!secondMember) return message.error("fun/lovecalc:MISSING");

		const members = [firstMember, secondMember].sort((a, b) => parseInt(a.id, 10) - parseInt(b.id, 10));
		const hash = md5(`${members[0].id}${members[1].user.username}${members[0].user.username}${members[1].id}`);

		const string = hash
			.split("")
			.filter(e => !isNaN(e))
			.join("");
		const percent = parseInt(string.substr(0, 2), 10);

		const embed = new Discord.MessageEmbed()
			.setAuthor({
				name: `❤️ ${message.translate("fun/lovecalc:DESCRIPTION")}`
			})
			.setDescription(message.translate("fun/lovecalc:CONTENT", {
				percent,
				firstUsername: firstMember.user.username,
				secondUsername: secondMember.user.username
			}))
			.setColor(data.config.embed.color)
			.setFooter({
				text: data.config.embed.footer
			});

		message.channel.send({
			embeds: [embed]
		});
	}
}

module.exports = Lovecalc;