const {Command} = require('../../lib.js');
const find = require('findit');
const path = require('path')
const {EmbedBuilder} = require('discord.js');


    class  help extends Command{
        constructor(client){
            super(client, {
                name: 'help',
                memberName: 'help',
                aliases:['command'],
    			description: 'Displays a list of available commands, or the details of a specified command.',
            })
            this.client = client;
        }
        async run(message, args)
        {
            if(args.length==0)
            {
                let finder = find(path.resolve('commands'));
                let payload = {embeds:[]};
                var Fields = [];
                finder.on('file', file=>
                {
                    let command = require(file);
                    if(typeof command === 'function')
                    {
                        let c = new command(this);
                        let _aliases = c.aliases;
                        let needsAdmin = c.needsAdmin;
                        if(!c.hidden)
                        {
                            Fields.push(
                                { 
                                    name: '!'+c.name+(needsAdmin?' - Requires administrator permissions:':' :'), 
                                    value: '–'+c.description + (_aliases?(' Can also be called using an aliases:'+_aliases.join(' ')):'')
                                });
                        }                        
                    }
                })
                finder.on('end', ()=>
                {
                    var i = 0;
                    for(var f  in Fields)
                    {
                        if (!payload.embeds[Math.floor(i / 25)]) 
                        { //checks if the embed with the required fields already exists in our array
                            payload.embeds.push(
                                    new EmbedBuilder()
                                    .setColor(0x0099FF)
                                    .setTitle('List of Commands:')
                                    .setAuthor({ name: 'Rem-Chan', iconURL: 'https://i.imgur.com/g6FSNhL.png', url:'https://rem.wordfights.com/addtodiscord'})
                                    .setTimestamp()
                                    .setFooter({ text: 'Rem-Chan on ', iconURL: 'https://i.imgur.com/g6FSNhL.png'})
                                )
                        }
                            payload.embeds[Math.floor(i / 25)].addFields(Fields[f]);
                            i++;
                    }
                })
                
                
                message.author.send(payload);
                
            }


            //message.author.send()
        }
    }
    module.exports = help;