const Discord = require('discord.js');
const math = require('mathjs');

module.exports = {
    config: {
        name: "calculate",
        aliases: ['calc', 'calculator'],
        description: "Shows Calculated Answers Of User's Query",
        usage: "[query](mathematical)",
        accessableby: "everyone"
    },
    run: async (bot, message, args) => {

        if (!args[0]) return message.channel.send("**Vnesite račun, ki želite izračunati**");

        let result;
        try {
            result = math.evaluate(args.join(" ").replace(/[x]/gi, "*").replace(/[,]/g, ".").replace(/[÷]/gi, "/"));
        } catch (e) {
            return message.channel.send("**Vnesite pravilen zapis računa!**\n\n**Primeri računov** - \n1. **sqrt enačba** - `sqrt(3^2 + 4^2) = 5`\n2. **Enote v enoto** - `2 km to m = 2.000`\n3. **Kompleksni izrazi** - `cos(45 deg) = 0.7071067811865476`\n4. **Preprosti matematični izrazi** - `+, -, ^, /, decimalke` = **2.5 - 2 = 0.5**");
        }

        let embed = new Discord.MessageEmbed()
            .setColor("ORANGE")
            .setAuthor(`${bot.user.username} Računalo`, message.author.displayAvatarURL({ dynamic: true }))
            .addField("**Računska operacija**", `\`\`\`Js\n${args.join("").replace(/[x]/gi, "*").replace(/[,]/g, ".").replace(/[÷]/gi, "/")}\`\`\``)
            .addField("**Rezultat**", `\`\`\`Js\n${result}\`\`\``)
            .setFooter(message.guild.name, message.guild.iconURL());
        message.channel.send(embed);
    }
}