import { LoadingEmbed, Embed } from "../../utils/defaults.js";
import Discord, {
    CommandInteraction,
    GuildMember,
    ActionRowBuilder,
    ButtonBuilder,
    MessageComponentInteraction,
    ButtonStyle,
    PermissionResolvable,
    APISelectMenuOption,
    StringSelectMenuBuilder
} from "discord.js";
import type { SlashCommandSubcommandBuilder } from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
import generateKeyValueList from "../../utils/generateKeyValueList.js";
import createPageIndicator from "../../utils/createPageIndicator.js";
import client from "../../utils/client.js";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder
        .setName("about")
        .setDescription("Shows info about a user")
        .addUserOption((option) =>
            option.setName("user").setDescription("The user to get info about | Default: Yourself")
        );

const callback = async (interaction: CommandInteraction): Promise<void> => {
    const guild = interaction.guild!;
    const member = (interaction.options.getMember("user") ?? interaction.member) as Discord.GuildMember;
    await userAbout(guild, member, interaction);
};

async function userAbout(
    guild: Discord.Guild,
    member: Discord.GuildMember,
    interaction: Discord.CommandInteraction | Discord.ContextMenuCommandInteraction
) {
    await member.user.fetch();
    await member.fetch();
    await interaction.reply({
        embeds: LoadingEmbed,
        fetchReply: true,
        ephemeral: true
    });
    const { renderUser, renderDelta } = client.logger;
    const flags: string[] = [];
    if (
        [
            "438733159748599813", // Pinea
            "317731855317336067", // Mini
            "261900651230003201", // Coded
            "511655498676699136" // Zan
        ].includes(member.user.id)
    ) {
        flags.push("NUCLEUSDEVELOPER");
    }
    if (
        (await client.guilds.cache.get("684492926528651336")?.members.fetch())
            ?.filter((m: GuildMember) => m.roles.cache.has("760896837866749972"))
            ?.map((m: GuildMember) => m.id)
            .includes(member.user.id)
    ) {
        flags.push("CLICKSDEVELOPER");
    }
    if (member.user.flags) {
        member.user.flags.toArray().map((flag) => {
            flags.push(flag.toString());
        });
    }
    if (member.user.bot) {
        flags.push("BOT");
    }
    // Check if they are boosting the server
    if (member.premiumSince) {
        flags.push("BOOSTER");
    }
    const nameReplacements: Record<string, string> = {
        NUCLEUSDEVELOPER: "**Nucleus Developer**",
        CLICKSDEVELOPER: "Clicks Developer",
        BOT: "Bot",
        BOOSTER: "Server Booster",
        HypeSquadOnlineHouse1: "Hypesquad Bravery",
        HypeSquadOnlineHouse2: "Hypesquad Brilliance",
        HypeSquadOnlineHouse3: "Hypesquad Balance",
        Hypesquad: "Hypesquad Events",
        PremiumEarlySupporter: "Early Supporter",
        BugHunterLevel1: "Bug Hunter Level 1",
        BugHunterLevel2: "Bug Hunter Level 2",
        Partner: "Partnered Server Owner",
        Staff: "Discord Staff",
        VerifiedDeveloper: "Verified Bot Developer",
        ActiveDeveloper: "Active Developer",
        Quarantined:
            "Quarantined [[What does this mean?]](https://support.discord.com/hc/en-us/articles/6461420677527)",
        Spammer: "Likely Spammer"
        // CertifiedModerator
        // VerifiedBot
    };
    const members = await guild.members.fetch();
    const membersArray = [...members.values()];
    membersArray.sort((a, b) => {
        if (a.joinedTimestamp === null) return 1;
        if (b.joinedTimestamp === null) return -1;
        return a.joinedTimestamp - b.joinedTimestamp;
    });
    const joinPos = membersArray.findIndex((m) => m.id === member.user.id);

    const roles = member.roles.cache.filter((r) => r.id !== guild.id).sort();
    let s = "";
    let count = 0;
    let ended = false;
    for (const roleId of roles) {
        const string = `<@&${roleId[1].id}>, `;
        if (s.length + string.length > 1000) {
            ended = true;
            s += `and ${roles.size - count} more`;
            break;
        }
        count++;
        s += string;
    }
    if (s.length > 0 && !ended) s = s.slice(0, -2);

    let perms = "";
    const permsArray: Record<string, string> = {
        Administrator: "Administrator",
        ManageGuild: "Manage Server",
        ManageRoles: "Manage Roles",
        ManageChannels: "Manage Channels",
        KickMembers: "Kick Members",
        BanMembers: "Ban Members",
        ModerateMembers: "Moderate Members",
        ManageNicknames: "Manage Nicknames",
        ManageWebhooks: "Manage Webhooks",
        ManageMessages: "Manage Messages",
        ViewAuditLog: "View Audit Log",
        MentionEveryone: "Mention Everyone"
    };
    Object.keys(permsArray).map((perm) => {
        const hasPerm = member.permissions.has(perm as PermissionResolvable);
        perms += `${getEmojiByName("CONTROL." + (hasPerm ? "TICK" : "CROSS"))} ${permsArray[perm]}\n`;
    });

    let selectPaneOpen = false;

    const embeds = [
        new Embed()
            .setEmbed(
                new EmojiEmbed()
                    .setTitle("User Info: General")
                    .setStatus("Success")
                    .setEmoji("MEMBER.JOIN")
                    .setDescription(
                        flags
                            .map((flag) => {
                                if (nameReplacements[flag]) {
                                    const emoji = getEmojiByName(`BADGES.${flag}`);
                                    if (emoji) return emoji + " " + nameReplacements[flag] + "\n";
                                    else return nameReplacements[flag] + "\n";
                                }
                            })
                            .join("") +
                            "\n" +
                            generateKeyValueList({
                                member: renderUser(member.user),
                                nickname: member.nickname ?? "*None set*",
                                id: `\`${member.id}\``,
                                "joined the server": renderDelta(member.joinedTimestamp!),
                                "joined discord": renderDelta(member.user.createdTimestamp),
                                "boost status": member.premiumSince
                                    ? `Started boosting ${renderDelta(member.premiumSinceTimestamp!)}`
                                    : "*Not boosting*",
                                "join position": `${joinPos + 1}`
                            })
                    )
                    .setThumbnail(member.user.displayAvatarURL())
            )
            .setTitle("General")
            .setDescription("General information about the user")
            .setPageId(0),
        new Embed()
            .setEmbed(
                new EmojiEmbed()
                    .setTitle("User Info: Roles")
                    .setStatus("Success")
                    .setEmoji("GUILD.ROLES.CREATE")
                    .setDescription(
                        generateKeyValueList({
                            member: renderUser(member.user),
                            id: `\`${member.id}\``,
                            roles: `${member.roles.cache.size - 1}`
                        }) +
                            "\n" +
                            (s.length > 0 ? s : "*None*") +
                            "\n"
                    )
            )
            .setTitle("Roles")
            .setDescription("Roles the user has")
            .setPageId(1),
        new Embed()
            .setEmbed(
                new EmojiEmbed()
                    .setTitle("User Info: Key Permissions")
                    .setStatus("Success")
                    .setEmoji("GUILD.ROLES.CREATE")
                    .setDescription(
                        generateKeyValueList({
                            member: renderUser(member.user),
                            id: `\`${member.id}\``
                        }) +
                            "\n" +
                            perms
                    )
            )
            .setTitle("Key Permissions")
            .setDescription("Key permissions the user has")
            .setPageId(2)
    ];
    if (member.user.bannerURL()) {
        embeds[0]!.embed.setImage(member.user.bannerURL()!);
    }
    let page = 0;
    let timedOut = false;
    for (const embed of embeds) {
        embed.embed.setDescription(embed.embed.description + "\n" + createPageIndicator(embeds.length, embed.pageId));
    }
    while (!timedOut) {
        const em = embeds[page]!.embed;

        let selectPane: ActionRowBuilder<ButtonBuilder | StringSelectMenuBuilder>[] = [];

        if (selectPaneOpen) {
            const options: APISelectMenuOption[] = [];
            embeds.forEach((embed) => {
                options.push({
                    label: embed.title,
                    value: embed.pageId.toString(),
                    description: embed.description || ""
                });
            });
            selectPane = [
                new ActionRowBuilder<StringSelectMenuBuilder>().addComponents(
                    new Discord.StringSelectMenuBuilder()
                        .addOptions(options)
                        .setCustomId("page")
                        .setMaxValues(1)
                        .setPlaceholder("Choose a page...")
                )
            ];
        }
        const m = await interaction.editReply({
            embeds: [em],
            components: selectPane.concat([
                new ActionRowBuilder<ButtonBuilder>().addComponents([
                    new ButtonBuilder()
                        .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
                        .setStyle(ButtonStyle.Secondary)
                        .setCustomId("left")
                        .setDisabled(page === 0),
                    new ButtonBuilder()
                        .setEmoji(getEmojiByName("CONTROL.MENU", "id"))
                        .setStyle(selectPaneOpen ? ButtonStyle.Primary : ButtonStyle.Secondary)
                        .setCustomId("select")
                        .setDisabled(false),
                    new ButtonBuilder()
                        .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
                        .setCustomId("right")
                        .setStyle(ButtonStyle.Secondary)
                        .setDisabled(page === embeds.length - 1)
                ])
            ])
        });
        let i: MessageComponentInteraction;
        try {
            i = await m.awaitMessageComponent({
                time: 300000,
                filter: (i) => {
                    return (
                        i.user.id === interaction.user.id &&
                        i.channel!.id === interaction.channel!.id &&
                        i.message.id === m.id
                    );
                }
            });
        } catch {
            timedOut = true;
            continue;
        }
        await i.deferUpdate();
        if (i.customId === "left") {
            if (page > 0) page--;
            selectPaneOpen = false;
        } else if (i.customId === "right") {
            if (page < embeds.length - 1) page++;
            selectPaneOpen = false;
        } else if (i.customId === "select") {
            selectPaneOpen = !selectPaneOpen;
        } else if (i.customId === "page" && i.isStringSelectMenu()) {
            page = parseInt(i.values[0]!);
            selectPaneOpen = false;
        }
    }
    const em = embeds[page]!.embed;
    em.setDescription(em.description + " | Message closed");
    await interaction.editReply({
        embeds: [em],
        components: []
    });
}

export { command };
export { callback };
export { userAbout };
