const {Command, ErrorMessage, ErrorType, Client, Message} = require('../../lib.js')



class clearchat extends Command{
    constructor(client){
        super(client, {
            name: 'clearchat',
            needsAdmin: true,
            aliases:['clear'],
            description: 'Clears the channel where it was used, you can pass a message id to clear around it.'
        })
        this.client = client;
    }
    async run(message, args)
    {
        //verificar admin
        const Author = message.author.username;
        const channel = message.channel.name;
        console.log('ClearChat:',' - ', Author, 'on:', channel, args?args:'No limit' )
        //Verificar se está na sala bemvindo
        if(bemvindo(message))
        {
            clear(null, true, null);
        }
        else
        {
            //verificar se tem argumentos
            if(args.length>0)
            {
                console.log("Info: !clearchat: has Args");
                if(correctArgs(this.client))
                {
                    console.log("Info: !clearchat: arround argument:",args);
                    clear(null,false, args,this.client);
                }
                else
                {
                    console.log("Info: !clearchat: Ammount:",args);
                    clear(args, false, null,this.client);
                }
            //Quais-quer eventuais problemas são tratados na função
            }
            else
            {
                //caso normal, apenas apaga tudo.
                clear(null, false,null,this.client);
            }
        }
        //verificar se é a sala bem vindo
        /**
         * 
         * @param {Message} message 
         * @returns {boolean}
         */
        function bemvindo(message){
           
            if(message.channel.id==='434713876836122626')return true;
            return false;
        }
        /**
         * 
         * @param {number} costumLimit 
         * @param {boolean} bemvindo 
         * @param {string} around 
         * @param {Client} client 
         * @returns {void}
         */
         async function clear(costumLimit, bemvindo, around,client) 
         {
             message.delete();
             let limit = costumLimit || 99;
             if (limit > 100) {
                 limit = 100;
             }
             var filter = {};
             filter.limit = limit;
             if(bemvindo) filter.after = '436122906565804032'
             filter.around = around;
             const fetched = await message.channel.messages.fetch(filter);
             //console.log("Info: !clearchat: ",fetched)
             message.channel.bulkDelete(fetched, true)
             .then(()=>
             {
                 console.log("Info: !clearchat", 'CostumLimit?:',limit?limit:'N/A', 'Bemvindo?:', bemvindo,'Around?:', around?around:'N/A');
             })
             .catch((err)=>
             {   
                 //console.log(err)
                 new ErrorMessage(client).send(ErrorType.OldMessages,message, ['Messages are too old and powerfull!']);
             })
         }
         
        //verificar se os argumentos estão corretos
        /**
         * 
         * @param {Client} client 
         * @returns {boolean}
         */
        function correctArgs(client){
            
            if(message.content.split(' ').length >2) {
                new ErrorMessage(client).send(ErrorType.Arguments, message, ['Use just one ID.']);
            }else{
                    var arg = parseInt(message.content.split(' ')[1], 10);
                    var arglenght = message.content.split(' ')[1].split('').length;
           
          
                if(typeof arg == "number"){
                
                    if(arglenght == 18) {
                        return true;
                    }else{
                         return false;
                    }
                }else {
                    new ErrorMessage(client).send(ErrorType.NoArguments, message, ['Messages are too old and powerfull!']);
                }
            }
        }
}}module.exports = clearchat;