const Command = require('../Command.js');
const {parseEmoji, EmbedBuilder} = require('discord.js');
const {parse} = require('twemoji-parser');
const _emojis = require('../../utils/emojis.json');
const {SlashCommandBuilder} = require('discord.js');

module.exports = class AddEmojiCommand extends Command {
    constructor(client) {
        super(client, {
            name: 'addemoji',
            aliases: ['add', 'em', 'emoji', 'emoji', 'addemote', 'ae'],
            usage: 'addemoji <emoji> <name>',
            description:
                'Add emoji from a server, or an image link.\nMultiple emojis can be added by typing all of them at once seperated by spaces.',
            type: client.types.MOD,
            clientPermissions: [
                'SendMessages',
                'EmbedLinks',
                'ManageEmojisAndStickers',
            ],
            userPermissions: ['ManageRoles'],
            examples: [
                'addemoji ðŸ™„ feelsbad',
                'em https://i.imgur.com/iYU1mgQ.png coolEmoji',
                'em ðŸ˜‚ ðŸ˜™ ðŸ˜Ž',
            ],
            slashCommand: new SlashCommandBuilder()
                .addStringOption(emoji => emoji.setName('emojis').setDescription('The emojis to add').setRequired(true))
                .addStringOption(name => name.setName('name').setDescription('The name of the emoji').setRequired(false))
        });
    }

    run(message, args) {
        if (!args[0] || args.length < 2) {
            return message.reply({embeds: [this.createHelpEmbed(message, 'Add Emoji', this)]});
        }

        this.handle(args, message, false);
    }

    async interact(interaction) {
        await interaction.deferReply();
        let emojis = interaction.options.getString('emojis');
        const name = interaction.options.getString('name');

        emojis = emojis.split(' ');
        const args = [...emojis];
        if (name) args.push(name);

        await this.handle(args, interaction, true);
    }

    handle(args, context, isInteraction) {
        try {
            let emoji;
            if (args.length > 1) {
                const isSecondArgEmoji = /^(ftp|http|https):\/\/[^ "]+$/.test(args[1]) || parseEmoji(args[1]).id;
                if (isSecondArgEmoji) {
                    args.forEach((emoji) => {
                        addEmoji.call(this, emoji, context, this, null);
                    });
                    return this.sendModLogMessage(context, null, {
                        Emoji: 'Multiple Emojis',
                    });
                }
                else { //second arg is not an emoji
                    emoji = addEmoji.call(this,
                        args[0],
                        context,
                        this,
                        args.slice(1).join('_'),
                        isInteraction
                    );
                }
            }
            else emoji = addEmoji.call(this, args[0], context, this, args.slice(1).join('_'));

            this.sendModLogMessage(context, null, {Emoji: emoji});
        }
        catch (err) {
            this.client.logger.error(err);
            this.sendReplyAndDelete(context, `${_emojis.fail} A error occured while adding the emoji. Common reasons are:- unallowed characters in emoji name, 50 emoji limit.`);
        }
    }
};

async function addEmoji(emoji, context, command, emojiName) {
    const urlRegex = new RegExp(/^(ftp|http|https):\/\/[^ "]+$/);
    if (!emoji) {
        this.sendReplyAndDelete(context, {embeds: [this.createErrorEmbed('Please provide a valid emoji.')]});
    }

    let name;
    let customemoji = parseEmoji(emoji); //Check if it's a emoji

    //If it's a custom emoji
    if (customemoji.id) {
        const url = `https://cdn.discordapp.com/emojis/${customemoji.id}.${
            customemoji.animated ? 'gif' : 'png'
        }`;
        name = emojiName || customemoji.name;
        const emoji = await context.guild.emojis.create({attachment: url, name: name});
        const payload = {
            embeds: [
                new EmbedBuilder().setDescription(
                    `${_emojis.success} ${emoji} added with name "${name}"`
                ),
            ],
        };

        await this.sendReply(context, payload);

        return emoji;
    }
    else if (urlRegex.test(emoji)) { //Check if it's a link
        //check for image urls
        name = emojiName || Math.random().toString(36).slice(2); //make the name compatible or just choose a random string
        try {
            const addedEmoji = await context.guild.emojis.create(
                `${emoji}`,
                `${name || `${customemoji.name}`}`
            );
            const payload = {
                embeds: [
                    new EmbedBuilder()
                        .setDescription(
                            `${addedEmoji} added with name "${addedEmoji.name}"`
                        )
                        .setFooter({
                            text: context.member.displayName,
                            iconURL: this.getAvatarURL(context.author),
                        }),
                ],
            };

            return this.sendReply(context, payload);
        }
        catch (e) {
            const payload = {
                embeds: [
                    new EmbedBuilder()
                        .setDescription(
                            `${_emojis.fail} Failed to add emoji\n\`\`\`${e.message}\`\`\``
                        )
                        .setFooter({
                            text: context.member.displayName,
                            iconURL: this.getAvatarURL(context.author),
                        }),
                ],
            };

            return this.sendReplyAndDelete(context, payload);
        }
    }
    else {
        let CheckEmoji = parse(emoji, {assetType: 'png'});
        if (!CheckEmoji[0])
            return this.sendReplyAndDelete(context, {embeds: [this.createErrorEmbed(`Please mention a valid emoji. ${emoji} is invalid`)]});
    }
}
