import confirmationMessage from "../../utils/confirmationMessage.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import { LoadingEmbed } from "../../utils/defaults.js";
import Discord, {
    ActionRowBuilder,
    ButtonBuilder,
    ButtonStyle,
    ContextMenuCommandBuilder,
    GuildMember,
    GuildTextBasedChannel,
    Message,
    MessageContextMenuCommandInteraction
} from "discord.js";
import client from "../../utils/client.js";
import { messageException } from "../../utils/createTemporaryStorage.js";

const command = new ContextMenuCommandBuilder().setName("Purge up to here");

async function waitForButton(m: Discord.Message, member: Discord.GuildMember): Promise<boolean> {
    let component;
    try {
        component = m.awaitMessageComponent({
            time: 200000,
            filter: (i) => i.user.id === member.id && i.channel!.id === m.channel.id && i.message.id === m.id
        });
    } catch (e) {
        return false;
    }
    (await component).deferUpdate();
    return true;
}

const callback = async (interaction: MessageContextMenuCommandInteraction) => {
    await interaction.targetMessage.fetch();
    const targetMessage = interaction.targetMessage;
    const targetMember: Discord.User = targetMessage.author;
    let allowedMessage: Discord.Message | undefined = undefined;
    const channel = interaction.channel;
    if (!channel) return;
    await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true });
    // Option for "include this message"?
    // Option for "Only selected user"?

    const history: Discord.Collection<string, Discord.Message> = await channel.messages.fetch({ limit: 100 });
    if (Date.now() - targetMessage.createdTimestamp > 2 * 7 * 24 * 60 * 60 * 1000) {
        const m = await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Purge")
                    .setDescription(
                        "The message you selected is older than 2 weeks. Discord only allows bots to delete messages that are 2 weeks old or younger."
                    )
                    .setEmoji("CHANNEL.PURGE.RED")
                    .setStatus("Danger")
            ],
            components: [
                new ActionRowBuilder<ButtonBuilder>().addComponents(
                    new ButtonBuilder()
                        .setCustomId("oldest")
                        .setLabel("Select first allowed message")
                        .setStyle(ButtonStyle.Primary)
                )
            ]
        });
        if (!(await waitForButton(m, interaction.member as Discord.GuildMember))) return;
    } else if (!history.has(targetMessage.id)) {
        const m = await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Purge")
                    .setDescription(
                        "The message you selected is not in the last 100 messages in this channel. Discord only allows bots to delete 100 messages at a time."
                    )
                    .setEmoji("CHANNEL.PURGE.YELLOW")
                    .setStatus("Warning")
            ],
            components: [
                new ActionRowBuilder<ButtonBuilder>().addComponents(
                    new ButtonBuilder()
                        .setCustomId("oldest")
                        .setLabel("Select first allowed message")
                        .setStyle(ButtonStyle.Primary)
                )
            ]
        });
        if (!(await waitForButton(m, interaction.member as Discord.GuildMember))) return;
    } else {
        allowedMessage = targetMessage;
    }

    if (!allowedMessage) {
        // Find the oldest message thats younger than 2 weeks
        const messages = history.filter((m) => Date.now() - m.createdTimestamp < 2 * 7 * 24 * 60 * 60 * 1000);
        allowedMessage = messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp).first();
    }

    if (!allowedMessage) {
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Purge")
                    .setDescription(
                        "There are no valid messages in the last 100 messages. (No messages younger than 2 weeks)"
                    )
                    .setEmoji("CHANNEL.PURGE.RED")
                    .setStatus("Danger")
            ],
            components: []
        });
        return;
    }

    let reason: string | null = null;
    let confirmation;
    let chosen = false;
    let timedOut = false;
    let deleteSelected = true;
    let deleteUser = false;
    do {
        confirmation = await new confirmationMessage(interaction)
            .setEmoji("CHANNEL.PURGE.RED")
            .setTitle("Purge")
            .setDescription(
                `[[Selected Message]](${allowedMessage.url})\n\n` +
                    (reason ? "\n> " + reason.replaceAll("\n", "\n> ") : "*No reason provided*") +
                    "\n\n" +
                    `Are you sure you want to delete all messages from below the selected message?`
            )
            .addCustomBoolean(
                "includeSelected",
                "Include selected message",
                false,
                undefined,
                "The selected message will be deleted as well.",
                "The selected message will not be deleted.",
                "CONTROL." + (deleteSelected ? "TICK" : "CROSS"),
                deleteSelected
            )
            .addCustomBoolean(
                "onlySelectedUser",
                "Only selected user",
                false,
                undefined,
                `Only messages from <@${targetMember.id}> will be deleted.`,
                `All messages will be deleted.`,
                "CONTROL." + (deleteUser ? "TICK" : "CROSS"),
                deleteUser
            )
            .setColor("Danger")
            .addReasonButton(reason ?? "")
            .setFailedMessage("No changes were made", "Success", "CHANNEL.PURGE.GREEN")
            .send(true);
        reason = reason ?? "";
        if (confirmation.cancelled) timedOut = true;
        else if (confirmation.success !== undefined) chosen = true;
        else if (confirmation.newReason) reason = confirmation.newReason;
        else if (confirmation.components) {
            deleteSelected = confirmation.components["includeSelected"]!.active;
            deleteUser = confirmation.components["onlySelectedUser"]!.active;
        }
    } while (!chosen && !timedOut);
    if (timedOut || !confirmation.success) return;
    const filteredMessages = history
        .filter((m) => m.createdTimestamp >= allowedMessage!.createdTimestamp) // older than selected
        .filter((m) => (deleteUser ? m.author.id === targetMember.id : true)) // only selected user
        .filter((m) => (deleteSelected ? true : m.id !== allowedMessage!.id)); // include selected

    const deleted = await (channel as GuildTextBasedChannel).bulkDelete(filteredMessages, true);
    if (deleted.size === 0) {
        return await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setEmoji("CHANNEL.PURGE.RED")
                    .setTitle("Purge")
                    .setDescription("No messages were deleted")
                    .setStatus("Danger")
            ],
            components: []
        });
    }
    if (deleteUser) {
        await client.database.history.create(
            "purge",
            interaction.guild!.id,
            targetMember,
            interaction.user,
            reason === "" ? "*No reason provided*" : reason,
            null,
            null,
            deleted.size.toString()
        );
    }
    const { log, NucleusColors, entry, renderUser, renderChannel } = client.logger;
    const data = {
        meta: {
            type: "channelPurge",
            displayName: "Channel Purged",
            calculateType: "messageDelete",
            color: NucleusColors.red,
            emoji: "PUNISH.BAN.RED",
            timestamp: Date.now()
        },
        list: {
            memberId: entry(interaction.user.id, `\`${interaction.user.id}\``),
            purgedBy: entry(interaction.user.id, renderUser(interaction.user)),
            channel: entry(interaction.channel!.id, renderChannel(interaction.channel! as Discord.GuildChannel)),
            messagesCleared: entry(deleted.size.toString(), deleted.size.toString())
        },
        hidden: {
            guild: interaction.guild!.id
        }
    };
    log(data);
    const messages: Message[] = deleted
        .map((m) => m)
        .filter((m) => m instanceof Message)
        .map((m) => m as Message);
    if (messages.length === 1) messageException(interaction.guild!.id, interaction.channel.id, messages[0]!.id);
    const messageArray: Message[] = messages
        .filter(
            (message) =>
                !message!.components.some((component) =>
                    component.components.some((child) => child.customId?.includes("transcript") ?? false)
                )
        )
        .map((message) => message as Message);
    const transcript = await client.database.transcripts.createTranscript(
        messageArray,
        interaction,
        interaction.member as GuildMember
    );

    const [code, key, iv] = await client.database.transcripts.create(transcript);

    await interaction.editReply({
        embeds: [
            new EmojiEmbed()
                .setEmoji("CHANNEL.PURGE.GREEN")
                .setTitle("Purge")
                .setDescription("Messages cleared")
                .setStatus("Success")
        ],
        components: [
            new Discord.ActionRowBuilder<ButtonBuilder>().addComponents([
                new ButtonBuilder()
                    .setLabel("View")
                    .setStyle(ButtonStyle.Link)
                    .setURL(`https://clicks.codes/nucleus/transcript/${code}?key=${key}&iv=${iv}`)
                    .setDisabled(!code)
            ])
        ]
    });
};

const check = async (_interaction: MessageContextMenuCommandInteraction) => {
    return true;
};

export { command, callback, check };
