const { MESSAGES } = require("../../util/constants");
const { MessageEmbed } = require("discord.js");
const { readdirSync } = require("fs");
const categoryList = readdirSync('./commands');

module.exports.run = (client, message, args, settings) => {

    //let user = message.guild.member(message.mentions.users.first());
    message.delete();

    if(!args.length) {
        const embedIntro = new MessageEmbed()
            .setAuthor('Liste des categories', message.guild.iconURL())
            .setColor('#0c76e7')
            .setDescription(`Voici la liste des catégories.\nPour plus d'information, merci de faire la commande \`${settings.prefix}help <Commande>\` ou \`${settings.prefix}help <Catégorie>\``)
            .setTimestamp(new Date())
        message.channel.send(embedIntro)
        // message.member.send("**Les commandes sont à taper dans le serveur et pas en MP désolé** :innocent: ")
        // message.channel.send(embedIntro);
        // message.channel.send("Regarde tes MP :wink:").then(msg => {
        //     msg.delete({ timeout: 5000 /*time unitl delete in milliseconds*/});
        // })

        const embedCat = new MessageEmbed()
            .setColor('#0c76e7')
            .setAuthor('Help', client.user.displayAvatarURL())
            .setFooter('Help', client.user.displayAvatarURL())
            .setTimestamp(new Date())
            .addField('Voici les différentes catégories de LanBot', `**${categoryList.join('\n')}**`)
            message.channel.send(embedCat);
    } else if(categoryList.includes(args[0])) {
        const embed = new MessageEmbed()
            .setColor('#0c76e7')
            .setDescription(`Voici la liste des commandes de cette catégorie.\nPour plus d'informations, \`${settings.prefix}help <Commande>\``)
            .setAuthor(args[0], client.user.displayAvatarURL())
            .setFooter(args[0], client.user.displayAvatarURL())
            .setTimestamp(new Date())
            .addField(`Pas assez de commande ? Envie d'en avoir plus ?`, `Fait la commande \`${settings.prefix}sugg\` pour faire ta suggestion au développeur :blush:`)

        const cmds = client.commands.filter(cmd => cmd.help.category=== args[0].toLowerCase()).array();
        cmds.forEach(cmd => {
            
            const aliases = (!cmd.help.aliases)? 'aucun':cmd.help.aliases.join(', ');
            embed.addField(`${settings.prefix}${cmd.help.name}`, `Description: ${cmd.help.description}\nAliase(s): ${aliases}\nUtilisation(s): ${settings.prefix}${cmd.help.usage.join('\n')}`);
            
        });
        
        message.channel.send(embed);
        
    } else {
        const command = client.commands.find(cmd => cmd.help.name === args[0]);
        if(!command) return message.channel.send('Commande ou Catégorie invalide !')
        const aliases = (!command.help.aliases)? 'aucun':command.help.aliases.join('\n');

        const embed = new MessageEmbed()
            .setColor('#0c76e7')
            .setAuthor(args[0], client.user.displayAvatarURL())
            .setFooter(args[0], client.user.displayAvatarURL())
            .setTimestamp(new Date())
            .addField(`${settings.prefix}${command.help.name}`, `Description: ${command.help.description}\nAliase(s): ${aliases}\nUtilisation(s): ${settings.prefix}${command.help.usage.join('\n')}`);
            
            message.channel.send(embed);

    }

};

module.exports.help = MESSAGES.COMMANDS.UTILITAIRE.HELP;