import { LoadingEmbed } from "./../../utils/defaultEmbeds.js";
import Discord, {
    CommandInteraction,
    GuildMember,
    Message,
    MessageActionRow,
    MessageActionRowComponent,
    MessageButton,
    MessageComponentInteraction,
    SelectMenuInteraction
} from "discord.js";
import { SelectMenuOption, SlashCommandSubcommandBuilder } from "@discordjs/builders";
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")
        );

class Embed {
    embed: Discord.MessageEmbed;
    title: string;
    description = "";
    pageId = 0;
    setEmbed(embed: Discord.MessageEmbed) {
        this.embed = embed;
        return this;
    }
    setTitle(title: string) {
        this.title = title;
        return this;
    }
    setDescription(description: string) {
        this.description = description;
        return this;
    }
    setPageId(pageId: number) {
        this.pageId = pageId;
        return this;
    }
}

const callback = async (interaction: CommandInteraction): Promise<void> => {
    if (!interaction.guild) return;
    const { renderUser, renderDelta } = client.logger;
    const member = (interaction.options.getMember("user") ?? interaction.member) as Discord.GuildMember;
    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");
    }
    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 = {
        NUCLEUSDEVELOPER: "**Nucleus Developer**",
        CLICKSDEVELOPER: "Clicks Developer",
        HOUSE_BRAVERY: "Hypesquad Bravery",
        HOUSE_BRILLIANCE: "Hypesquad Brilliance",
        HOUSE_BALANCE: "Hypesquad Balance",
        HYPESQUAD_EVENTS: "Hypesquad Events",
        EARLY_SUPPORTER: "Early Supporter",
        BUGHUNTER_LEVEL_1: "Bug Hunter Level 1",
        BUGHUNTER_LEVEL_2: "Bug Hunter Level 2",
        PARTNERED_SERVER_OWNER: "Partnered Server Owner",
        DISCORD_EMPLOYEE: "Discord Staff",
        EARLY_VERIFIED_BOT_DEVELOPER: "Verified Bot Developer",
        BOT: "Bot",
        BOOSTER: "Server Booster"
    };
    const members = await interaction.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 !== interaction.guild!.id).sort();
    let s = "";
    let count = 0;
    let ended = false;
    for (const roleId in roles) {
        const string = `<@&${roleId}>, `;
        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 = {
        ADMINISTRATOR: "Administrator",
        MANAGE_GUILD: "Manage Server",
        MANAGE_ROLES: "Manage Roles",
        MANAGE_CHANNELS: "Manage Channels",
        KICK_MEMBERS: "Kick Members",
        BAN_MEMBERS: "Ban Members",
        MODERATE_MEMBERS: "Moderate Members",
        MANAGE_NICKNAMES: "Manage Nicknames",
        MANAGE_WEBHOOKS: "Manage Webhooks",
        MANAGE_MESSAGES: "Manage Messages",
        VIEW_AUDIT_LOG: "View Audit Log",
        MENTION_EVERYONE: "Mention Everyone"
    };
    Object.keys(permsArray).map((perm) => {
        const hasPerm = member.permissions.has(perm as Discord.PermissionString);
        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]) {
                                    return getEmojiByName(`BADGES.${flag}`) + " " + nameReplacements[flag];
                                }
                            })
                            .join("\n") +
                            "\n\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({ dynamic: true }))
                    .setImage((await member.user.fetch()).bannerURL({ format: "gif" }))
            )
            .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"
                    )
                    .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
            )
            .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
                    )
                    .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
            )
            .setTitle("Key Permissions")
            .setDescription("Key permissions the user has")
            .setPageId(2)
    ];
    const m = (await interaction.reply({
        embeds: LoadingEmbed,
        fetchReply: true,
        ephemeral: true
    })) as Message;
    let page = 0;
    let breakReason = "";
    while (true) {
        const em = new Discord.MessageEmbed(embeds[page].embed);
        em.setDescription(em.description + "\n" + createPageIndicator(embeds.length, page));
        let selectPane = [];

        if (selectPaneOpen) {
            const options = [];
            embeds.forEach((embed) => {
                options.push(
                    new SelectMenuOption({
                        label: embed.title,
                        value: embed.pageId.toString(),
                        description: embed.description || ""
                    })
                );
            });
            selectPane = [
                new MessageActionRow().addComponents([
                    new Discord.MessageSelectMenu()
                        .addOptions(options)
                        .setCustomId("page")
                        .setMaxValues(1)
                        .setPlaceholder("Choose a page...")
                ])
            ];
        }
        await interaction.editReply({
            embeds: [em],
            components: selectPane.concat([
                new MessageActionRow().addComponents([
                    new MessageButton()
                        .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
                        .setStyle("SECONDARY")
                        .setCustomId("left")
                        .setDisabled(page === 0),
                    new MessageButton()
                        .setEmoji(getEmojiByName("CONTROL.MENU", "id"))
                        .setStyle(selectPaneOpen ? "PRIMARY" : "SECONDARY")
                        .setCustomId("select")
                        .setDisabled(false),
                    new MessageButton()
                        .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
                        .setCustomId("right")
                        .setStyle("SECONDARY")
                        .setDisabled(page === embeds.length - 1),
                    new MessageButton()
                        .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
                        .setCustomId("close")
                        .setStyle("DANGER")
                ])
            ])
        });
        let i: MessageComponentInteraction;
        try {
            i = await m.awaitMessageComponent({ time: 300000 });
        } catch {
            breakReason = "Message timed out";
            break;
        }
        i.deferUpdate();
        if ((i.component as MessageActionRowComponent).customId === "left") {
            if (page > 0) page--;
            selectPaneOpen = false;
        } else if ((i.component as MessageActionRowComponent).customId === "right") {
            if (page < embeds.length - 1) page++;
            selectPaneOpen = false;
        } else if ((i.component as MessageActionRowComponent).customId === "select") {
            selectPaneOpen = !selectPaneOpen;
        } else if ((i.component as MessageActionRowComponent).customId === "close") {
            breakReason = "Message closed";
            break;
        } else if ((i.component as MessageActionRowComponent).customId === "page") {
            page = parseInt((i as SelectMenuInteraction).values[0]);
            selectPaneOpen = false;
        } else {
            breakReason = "Message closed";
            break;
        }
    }
    const em = new Discord.MessageEmbed(embeds[page].embed);
    em.setDescription(em.description + "\n" + createPageIndicator(embeds.length, page) + " | " + breakReason);
    await interaction.editReply({
        embeds: [em],
        components: [
            new MessageActionRow().addComponents([
                new MessageButton()
                    .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
                    .setStyle("SECONDARY")
                    .setCustomId("left")
                    .setDisabled(true),
                new MessageButton()
                    .setEmoji(getEmojiByName("CONTROL.MENU", "id"))
                    .setStyle("SECONDARY")
                    .setCustomId("select")
                    .setDisabled(true),
                new MessageButton()
                    .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
                    .setCustomId("right")
                    .setStyle("SECONDARY")
                    .setDisabled(true),
                new MessageButton()
                    .setEmoji(getEmojiByName("CONTROL.CROSS", "id"))
                    .setCustomId("close")
                    .setStyle("DANGER")
                    .setDisabled(true)
            ])
        ]
    });
};

const check = () => {
    return true;
};

export { command };
export { callback };
export { check };
