const {Command, aniList, Random} = require('../../lib.js')

const {EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle} = require('discord.js');


  
class anime extends Command{
        constructor(client){
            super(client, {
                name: 'anime',
                group:'pesquisa',
                memberName: 'anime',
                description: 'Shows info about a anime.'

            })
        }
        async run(message, args){
            if(!args[0]) return  new ErrorMessage(this.client).send(ErrorType.Arguments, message)
            const data = await new aniList().searchAnime(args.join(' '));
            if(data=='Error') return new ErrorMessage(this.client).send(ErrorType.Arguments, message, [`Couldn't find any anime with the search argument you provided.`])
            const embed = new EmbedBuilder()
                .setTitle('First 4 results of search:')
                .setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'})
                .setColor(0x003284)
                .setURL('https://www.rem.wordfights.com')
                .setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' })
                .setImage(data[0].coverImage)
                .setTimestamp()  
                .addFields(
                    {name:data[0].title.romaji+' ['+data[0].title.english+']', value:'1'},
                    {name:data[1].title.romaji+' ['+data[0].title.english+']', value:'2'},
                    {name:data[2].title.romaji+' ['+data[0].title.english+']', value:'3'},
                    {name:data[3].title.romaji+' ['+data[0].title.english+']', value:'4'},
                    );
            var embeds = [embed]
            embeds.push(new EmbedBuilder().setImage(data[1].coverImage).setURL('https://www.rem.wordfights.com'));
            embeds.push(new EmbedBuilder().setImage(data[2].coverImage).setURL('https://www.rem.wordfights.com'));
            embeds.push(new EmbedBuilder().setImage(data[3].coverImage).setURL('https://www.rem.wordfights.com'));
            
            const randomID = Random();
            const row = new ActionRowBuilder()
            .addComponents(
                new ButtonBuilder()
                    .setCustomId(randomID+1)
                    .setLabel('1')
                    .setStyle(ButtonStyle.Success),
                new ButtonBuilder()
                    .setCustomId(randomID+2)
                    .setLabel('2')
                    .setStyle(ButtonStyle.Success),
                new ButtonBuilder()
                    .setCustomId(randomID+3)
                    .setLabel('3')
                    .setStyle(ButtonStyle.Success),
                new ButtonBuilder()
                    .setCustomId(randomID+4)
                    .setLabel('4')
                    .setStyle(ButtonStyle.Success),

            );
            const filter = i => i.customId.slice(0,-1) === randomID;
            await message.channel.send({ephemeral: true, embeds, components: [row] }); 
            const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
            collector.on('collect', async m => 
            {
                message.delete();
                m.message.delete();
                sendMessage(data[m.customId[m.customId.length-1]-1].id);
            });
            /**
             * @param {Number} id  
             */
            async function sendMessage(id)
            {
                const data = await new aniList().getAnimeInfo(id);
                const embed = new EmbedBuilder()
                    .setTitle(`${data.title.romaji} (${data.title.native} / ${data.title.english})`)
                    .setAuthor({name:"Rem-chan", iconURL:"https://i.imgur.com/g6FSNhL.png",url:'https://rem.wordfights.com/addtodiscord'})
                    .setColor(0x003284)
                    .setDescription(data.description.replaceAll('<br>',' '))
                    .setFooter({ text: 'Rem-Chan on', iconURL: 'https://i.imgur.com/g6FSNhL.png' })
                    .setImage(data.coverImage)
                    .setThumbnail(data.coverImage)
                    .setTimestamp()  
                    .addFields(
                        {name:'Status:', value:data.status, inline:true},
                        {name:'Episodes:', value:data.episodes?data.episodes.toString():'N/A', inline:true},
                        {name:'Trailer:', value:data.trailer?(data.trailer.site=='youtube'?`https://www.youtube.com/watch?v=${data.trailer.id}`:`https://www.dailymotion.com/video/${data.trailer.id}`):'N/A'});
                    const randomID = Random();
                    const row = new ActionRowBuilder()
                    .addComponents(
                        new ButtonBuilder()
                            .setCustomId(randomID)
                            .setLabel('Remove this.')
                            .setStyle(ButtonStyle.Primary),
                    );
                    const filter = i => i.customId === randomID;
                    await message.channel.send({ephemeral: true, embeds: [embed], components: [row] }); 
                    const collector = message.channel.createMessageComponentCollector({ filter, time: 60000 });
                    collector.on('collect', async m => 
                    {
                        m.message.delete();
                    });
            }
        }                
                
}module.exports = anime;