const {Command,ErrorMessage, ErrorType, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, Strikes} = require('../../lib.js')
const strikeM = require('../../models/strikes');

    class strike extends Command{
        constructor(client){
            super(client, {
                name: 'strike',
                group:'admin',
                description: 'Strikes an user. Usage !strike @USER REASON',
                needsAdmin:true,
            })
            this.client = client;
            this.message;
        }
        async run(message, args)
        {
            //Check admin privilege
            this.message = message;
            const isActive = await new Strikes(this.client).getAreStrikesActive(message.guildId);
            if(!isActive) return new ErrorMessage(this.client).send(ErrorType.FeatureInnactive, message)
            var confirm = await this.confirmArgs(message,args);
            if(confirm=='Error') return new ErrorMessage(this.client).send(ErrorType.Arguments, message, ['Use the mention method to pass the user.', 'Make sure you provide a reason.','Make sure to only mention one person per strike.'])
            const Stroked = message.mentions.users.toJSON()[0];
            
            //! This gotta be encrypted.
            
            var strike = new strikeM();
            strike.strikerID = message.author.id;
            strike.strikerName = message.author.username;
            strike.strikerAvatar = message.author.avatar?message.author.avatar:'NOAVATAR';
            strike.strokedID = Stroked.id;
            strike.strokedName = Stroked.username;
            strike.strokedAvatar = Stroked.avatar?Stroked.avatar: 'NOAVATAR';
            strike.guildName = message.guild.name;
            strike.guildID = message.guild.id
            strike.reason = confirm.reason;
            strike.date = Date.now();
            strike.expireAt = (new Date(Date.now()+(15*24*60*60*1000)));
            strike.save(err=>
            {
                if(err)console.log(err);
                this.sendMessage(Stroked,confirm.reason);
                message.delete();    
            })
        }  
        async  sendMessage(Stroked, reason)
        {
            const embed = new EmbedBuilder()
            embed.setFooter({text:'Rem-chan on ', iconURL:"https://i.imgur.com/g6FSNhL.png"})
            embed.setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'}); 
            embed.setColor(0x110809);
            embed.setTimestamp()
            embed.setTitle(`Strike`)    
            embed.setDescription(`${Stroked.username} has been struck.`)   
            embed.addFields({name:'Reason:', value:reason}) 

            const row = new ActionRowBuilder()
            .addComponents(
                new ButtonBuilder()
                    .setCustomId('RemoveStrikeMessage')
                    .setLabel('Remove this.')
                    .setStyle(ButtonStyle.Primary),
            );
            const filter = i => i.customId === 'RemoveStrikeMessage';
    
            const Message = await this.message.channel.send({ephemeral: true, embeds: [embed], components: [row] }); 
            const collector = this.message.channel.createMessageComponentCollector({ filter, time: 60000 });
            collector.on('collect', async m => 
            {
                m.message.delete()
                collector.stop();
            });
            collector.on('end', async ()=>
            {
                Message.edit({ components: [] });
            })
        }
        async confirmArgs(message,args)
        {
            if(args.length<2) return 'Error' 
            if(!args[0].includes('@')) return 'Error'
            if(args.join('').split('@').length>2) return 'Error'
            if(isNaN(args[0].replaceAll(/[<@>]/g, ''))) return 'Error'
            args.splice(0,1)
            return {reason:args.join(' ')}
        }    
}module.exports = strike;