import {
    ActionRowBuilder,
    ButtonBuilder,
    ButtonInteraction,
    ButtonStyle,
    CommandInteraction,
    ComponentType,
    Message,
    StringSelectMenuBuilder,
    StringSelectMenuInteraction
} from "discord.js";
import type { SlashCommandSubcommandBuilder } from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import client from "../../utils/client.js";
import { LoadingEmbed } from "../../utils/defaults.js";
import getEmojiByName from "../../utils/getEmojiByName.js";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder.setName("premium").setDescription("Information about Nucleus Premium");
//TODO: Allow User to remove Premium

const dmcallback = async (interaction: CommandInteraction, firstDescription: string, msg: Message): Promise<void> => {
    let closed = false;
    do {
        const dbUser = await client.database.premium.fetchUser(interaction.user.id);
        if (!dbUser) {
            await interaction.editReply({
                embeds: [
                    new EmojiEmbed()
                        .setTitle("Premium")
                        .setDescription(
                            `*You do not have premium! You can't activate premium on any servers.*` + firstDescription
                        )
                        .setEmoji("NUCLEUS.LOGO")
                        .setStatus("Danger")
                ]
            });
            return;
        }
        const premiumGuilds = dbUser.appliesTo.map((guildID) => {
            const guild = client.guilds.cache.get(guildID);
            if (!guild) return undefined;
            return guild.name;
        });

        const options = premiumGuilds.filter((guild) => guild !== undefined) as string[];

        const removeRow = new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
            new StringSelectMenuBuilder()
                .setCustomId("currentPremium")
                .setPlaceholder("Select a server to remove premium from")
                .setDisabled(premiumGuilds.length === 0)
                .addOptions(
                    options.slice(0, Math.min(options.length, 24)).map((guild) => {
                        return { label: guild, value: guild };
                    })
                )
        );
        const cancel = new ActionRowBuilder<ButtonBuilder>().addComponents(
            new ButtonBuilder().setCustomId("cancel").setLabel("Close").setStyle(ButtonStyle.Danger)
        );

        const components: ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>[] = [cancel];
        if (options.length > 0) components.unshift(removeRow);
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Premium")
                    .setDescription(
                        `*You have premium on the following servers:*\n\n` +
                            (options.length > 0 ? options.join(", ") : `You have not activated premium in any guilds`) +
                            firstDescription
                    )
                    .setEmoji("NUCLEUS.LOGO")
                    .setStatus("Success")
            ],
            components: components
        });

        let i: StringSelectMenuInteraction | ButtonInteraction;
        try {
            const filter = (i: StringSelectMenuInteraction | ButtonInteraction) => i.user.id === interaction.user.id;
            i = await msg.awaitMessageComponent<ComponentType.StringSelect | ComponentType.Button>({
                time: 300000,
                filter
            });
        } catch (e) {
            await interaction.deleteReply();
            closed = true;
            break;
        }
        await i.deferUpdate();
        if (i.isButton()) {
            closed = true;
        } else {
            await client.database.premium.removePremium(interaction.user.id, i.values[0]!);
        }
    } while (!closed);
    await interaction.deleteReply();
};

const callback = async (interaction: CommandInteraction): Promise<void> => {
    if (interaction.guild) client.database.premium.hasPremium(interaction.guild.id).finally(() => {});
    const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true });
    const member = await (await interaction.client.guilds.fetch("684492926528651336")).members
        .fetch(interaction.user.id)
        .catch(() => {
            void interaction.editReply({
                embeds: [
                    new EmojiEmbed()
                        .setTitle("Premium")
                        .setDescription(
                            `*You are not currently in the Clicks Server. To gain access to premium please join.*` +
                                firstDescription
                        )
                        .setEmoji("NUCLEUS.LOGO")
                        .setStatus("Danger")
                ],
                components: [
                    new ActionRowBuilder<ButtonBuilder>().addComponents(
                        new ButtonBuilder()
                            .setStyle(ButtonStyle.Link)
                            .setLabel("Join")
                            .setURL("https://discord.gg/bPaNnxe")
                    )
                ]
            });
        });
    if (!member) return;
    const firstDescription =
        "\n\nPremium allows servers of your choice to get access to extra features for a fixed price per month.\nThis includes:\n" +
        `${getEmojiByName(
            "MOD.IMAGES.TOOSMALL"
        )}  Attachment logs - Stores attachments so they can be viewed after a message is deleted.\n` +
        `${getEmojiByName(
            "GUILD.TICKET.ARCHIVED"
        )}  Ticket Transcripts - Gives a link to view the history of a ticket after it has been closed.\n`;
    const dbMember = await client.database.premium.fetchUser(interaction.user.id);
    let premium = `You do not have premium! You can't activate premium on any servers.`;
    let count = 0;
    const { level, appliesTo } = dbMember ?? { level: 0, appliesTo: [] };
    if (level === 99) {
        premium = `You have Infinite Premium! You have been gifted this by the developers as a thank you. You can give premium to any and all servers you are in.`;
        count = 200;
    } else if (level === 1) {
        premium = `You have Premium tier 1! You can give premium to ${1 - appliesTo.length} more server(s).`;
        count = 1;
    } else if (level === 2) {
        premium = `You have Premium tier 2! You can give premium to ${3 - appliesTo.length} more server(s).`;
        count = 3;
    } else if (level === 3) {
        premium = `You have Premium Mod! You can give premium to ${
            3 - appliesTo.length
        } more server(s), as well as automatically giving premium to all servers you have a "manage" permission in.`;
        count = 3;
    }
    if (dbMember?.expiresAt) {
        premium = `**You can't give servers premium anymore because your subscription ended or was cancelled.** To get premium again please subscribe in the Clicks server`;
        count = 0;
    }
    if (!interaction.guild) return await dmcallback(interaction, firstDescription, m);
    const hasPremium = await client.database.premium.hasPremium(interaction.guild!.id);
    let premiumGuild = "";
    if (hasPremium) {
        const gaveUser = await client.users.fetch(hasPremium[1]);
        premiumGuild = `**This server has premium! It was ${
            hasPremium[2] === 3 && hasPremium[3]
                ? `automatically applied by ` +
                  (gaveUser.discriminator !== "0"
                      ? `${gaveUser.username}#${gaveUser.discriminator}`
                      : `@${gaveUser.username}`)
                : `given by <@${hasPremium[1]}>`
        }**\n\n`;
    }

    const components: ActionRowBuilder<ButtonBuilder>[] = [];
    if (level === 0 || dbMember?.expiresAt) {
        components.push(
            new ActionRowBuilder<ButtonBuilder>().addComponents(
                new ButtonBuilder()
                    .setStyle(ButtonStyle.Link)
                    .setLabel("Join Clicks")
                    .setURL("https://discord.gg/bPaNnxe")
            )
        );
    } else {
        components.push(
            new ActionRowBuilder<ButtonBuilder>().addComponents(
                new ButtonBuilder()
                    .setStyle(premiumGuild.length > 0 ? ButtonStyle.Secondary : ButtonStyle.Success)
                    .setLabel(premiumGuild.length > 0 ? "This server has premium" : "Activate premium here")
                    .setCustomId("premiumActivate")
                    .setDisabled(count <= 0 || (hasPremium ? hasPremium[0] : false))
            )
        );
    }

    let userPremiumServers = "";
    if ((dbMember?.appliesTo.length ?? 0) > 0)
        userPremiumServers = "\nIf you want to remove premium from a server, run this command in your DMs with me.";

    await interaction.editReply({
        embeds: [
            new EmojiEmbed()
                .setTitle("Premium")
                .setDescription(premiumGuild + premium + firstDescription)
                .setEmoji("NUCLEUS.LOGO")
                .setStatus("Danger")
                .setImage("https://assets.clicks.codes/ads/ads/nucleus-premium.png")
        ],
        components: components
    });

    const filter = (i: ButtonInteraction) => i.customId === "premiumActivate" && i.user.id === interaction.user.id;
    let i;
    try {
        i = await interaction.channel!.awaitMessageComponent<2>({ filter, time: 60000 });
    } catch (e) {
        return;
    }
    await i.deferUpdate();
    const guild = i.guild!;
    if (count - appliesTo.length <= 0) {
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Premium")
                    .setDescription(
                        `You have already activated premium on the maximum amount of servers!` +
                            userPremiumServers +
                            firstDescription
                    )
                    .setEmoji("NUCLEUS.PREMIUMACTIVATE")
                    .setStatus("Danger")
            ],
            components: []
        });
    } else {
        await client.database.premium.addPremium(interaction.user.id, guild.id);
        await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setTitle("Premium")
                    .setDescription(
                        `You have activated premium on this server!` + userPremiumServers + firstDescription
                    )
                    .setEmoji("NUCLEUS.LOGO")
                    .setStatus("Danger")
            ],
            components: []
        });
    }
};

export { command };
export { callback };
