import { getAnivTimeouts } from "db/dbAnivTimeouts";
import { Command, sendMessage } from "lib/commandUtils";
import parseCommandArgs from "lib/parseCommandArgs";
import User from "user";

export default new Command({
	name: "anivtimeouts",
	aliases: ["anivtimeouts", "anivtimeout"],
	usertype: "chatter",
	execution: async (msg, user) => {
		const args = parseCommandArgs(msg.messageText);
		const target = args[0]
			? await User.initUsername(args[0].toLowerCase())
			: user;
		if (!target) {
			await sendMessage(`Chatter ${args[0]} doesn't exist!`, msg.messageId);
			return;
		}
		const { dodge, dead } = await getAnivTimeouts(target);
		const percentage = (dodge / (dead + dodge)) * 100;
		const message = `Aniv timeouts of ${target.displayName}: Dodge: ${dodge}, Timeout: ${dead}. Dodge percentage: ${Number.isNaN(percentage) ? "0" : percentage.toFixed(1)}%`;
		await sendMessage(message, msg.messageId);
	},
});
