import { LoadingEmbed } from "../../utils/defaults.js";
import type { HistorySchema, FlagColors } from "../../utils/database.js";
import Discord, {
    CommandInteraction,
    GuildMember,
    Message,
    ActionRowBuilder,
    ButtonBuilder,
    MessageComponentInteraction,
    ButtonStyle,
    TextInputStyle,
    APIMessageComponentEmoji,
    SlashCommandSubcommandBuilder,
    StringSelectMenuBuilder,
    StringSelectMenuOptionBuilder,
    ContextMenuCommandInteraction
} from "discord.js";
import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
import client from "../../utils/client.js";
import { modalInteractionCollector } from "../../utils/dualCollector.js";
import pageIndicator from "../../utils/createPageIndicator.js";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder
        .setName("about")
        // .setNameLocalizations({"ru": "info", "zh-CN": "history", "zh-TW": "notes", "pt-BR": "flags"})
        .setDescription("Shows moderator information about a user")
        .addUserOption((option) =>
            option.setName("user").setDescription("The user to get information about").setRequired(true)
        );

const types: Record<string, { emoji: string; text: string }> = {
    warn: { emoji: "PUNISH.WARN.YELLOW", text: "Warned" },
    mute: { emoji: "PUNISH.MUTE.YELLOW", text: "Muted" },
    unmute: { emoji: "PUNISH.MUTE.GREEN", text: "Unmuted" },
    join: { emoji: "MEMBER.JOIN", text: "Joined" },
    leave: { emoji: "MEMBER.LEAVE", text: "Left" },
    kick: { emoji: "MEMBER.KICK", text: "Kicked" },
    softban: { emoji: "PUNISH.SOFTBAN", text: "Softbanned" },
    ban: { emoji: "MEMBER.BAN", text: "Banned" },
    unban: { emoji: "MEMBER.UNBAN", text: "Unbanned" },
    purge: { emoji: "PUNISH.CLEARHISTORY", text: "Messages cleared" },
    nickname: { emoji: "PUNISH.NICKNAME.YELLOW", text: "Nickname changed" }
};

function historyToString(history: HistorySchema) {
    if (!Object.keys(types).includes(history.type)) throw new Error("Invalid history type");
    let s = `${getEmojiByName(types[history.type]!.emoji)} ${history.amount ? history.amount + " " : ""}${
        types[history.type]!.text
    } on <t:${Math.round(history.occurredAt.getTime() / 1000)}:F>`;
    if (history.moderator) {
        s += ` by <@${history.moderator}>`;
    }
    if (history.reason) {
        s += `\n**Reason:**\n> ${history.reason}`;
    }
    if (history.before) {
        s += `\n**Before:**\n> ${history.before}`;
    }
    if (history.after) {
        s += `\n**After:**\n> ${history.after}`;
    }
    return s + "\n";
}

class TimelineSection {
    name: string = "";
    content: { data: HistorySchema; rendered: string }[] = [];

    addContent = (content: { data: HistorySchema; rendered: string }) => {
        this.content.push(content);
        return this;
    };
    contentLength = () => {
        return this.content.reduce((acc, cur) => acc + cur.rendered.length, 0);
    };
    generateName = () => {
        const first = Math.round(this.content[0]!.data.occurredAt.getTime() / 1000);
        const last = Math.round(this.content[this.content.length - 1]!.data.occurredAt.getTime() / 1000);
        if (first === last) {
            return (this.name = `<t:${first}:F>`);
        }
        return (this.name = `<t:${first}:F> - <t:${last}:F>`);
    };
}

const monthNames = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
];

async function showHistory(member: Discord.GuildMember, interaction: CommandInteraction) {
    let currentYear = new Date().getFullYear();
    let pageIndex: number | null = null;
    let history, current: TimelineSection;
    history = await client.database.history.read(member.guild.id, member.id, currentYear);
    history = history
        .sort((a: { occurredAt: Date }, b: { occurredAt: Date }) => b.occurredAt.getTime() - a.occurredAt.getTime())
        .reverse();
    let m: Message;
    let refresh = false;
    let filteredTypes: string[] = [];
    let openFilterPane = false;
    let timedOut = false;
    let showHistorySelected = false;
    while (!timedOut && !showHistorySelected) {
        if (refresh) {
            history = await client.database.history.read(member.guild.id, member.id, currentYear);
            history = history
                .sort(
                    (a: { occurredAt: Date }, b: { occurredAt: Date }) =>
                        b.occurredAt.getTime() - a.occurredAt.getTime()
                )
                .reverse();
            if (openFilterPane) {
                let tempFilteredTypes = filteredTypes;
                if (filteredTypes.length === 0) {
                    tempFilteredTypes = Object.keys(types);
                }
                history = history.filter((h: { type: string }) => tempFilteredTypes.includes(h.type));
            }
            refresh = false;
        }
        const groups: TimelineSection[] = [];
        if (history.length > 0) {
            current = new TimelineSection();
            history.forEach((event: HistorySchema) => {
                if (current.contentLength() + historyToString(event).length > 2000 || current.content.length === 5) {
                    groups.push(current);
                    current.generateName();
                    current = new TimelineSection();
                }
                current.addContent({
                    data: event,
                    rendered: historyToString(event)
                });
            });
            current.generateName();
            groups.push(current);
            if (pageIndex === null) {
                pageIndex = groups.length - 1;
            }
        }
        if (pageIndex === null) pageIndex = 0;
        let components: (ActionRowBuilder<Discord.StringSelectMenuBuilder> | ActionRowBuilder<ButtonBuilder>)[] = [];
        if (openFilterPane)
            components = components.concat([
                new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents(
                    new Discord.StringSelectMenuBuilder()
                        .setMinValues(1)
                        .setMaxValues(Object.keys(types).length)
                        .setCustomId("filter")
                        .setPlaceholder("Select events to show")
                        .setOptions(
                            ...Object.entries(types).map(([key, value]) =>
                                new Discord.StringSelectMenuOptionBuilder()
                                    .setLabel(value.text)
                                    .setValue(key)
                                    .setDefault(filteredTypes.includes(key))
                                    .setEmoji(getEmojiByName(value.emoji, "id") as APIMessageComponentEmoji)
                            )
                        )
                )
            ]);
        components = components.concat([
            new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
                new ButtonBuilder()
                    .setCustomId("prevYear")
                    .setLabel((currentYear - 1).toString())
                    .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
                    .setStyle(ButtonStyle.Secondary),
                new ButtonBuilder().setCustomId("prevPage").setLabel("Previous page").setStyle(ButtonStyle.Primary),
                new ButtonBuilder().setCustomId("today").setLabel("Today").setStyle(ButtonStyle.Primary),
                new ButtonBuilder()
                    .setCustomId("nextPage")
                    .setLabel("Next page")
                    .setStyle(ButtonStyle.Primary)
                    .setDisabled(pageIndex >= groups.length - 1 && currentYear === new Date().getFullYear()),
                new ButtonBuilder()
                    .setCustomId("nextYear")
                    .setLabel((currentYear + 1).toString())
                    .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
                    .setStyle(ButtonStyle.Secondary)
                    .setDisabled(currentYear === new Date().getFullYear())
            ])
        ]);
        components = components.concat([
            new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
                new ButtonBuilder()
                    .setLabel("Mod notes")
                    .setCustomId("modNotes")
                    .setStyle(ButtonStyle.Primary)
                    .setEmoji(getEmojiByName("ICONS.EDIT", "id")),
                new ButtonBuilder()
                    .setLabel("Filter")
                    .setCustomId("openFilter")
                    .setStyle(openFilterPane ? ButtonStyle.Success : ButtonStyle.Primary)
                    .setEmoji(getEmojiByName("ICONS.FILTER", "id"))
            ])
        ]);

        const end =
            "\n\nJanuary " +
            currentYear.toString() +
            pageIndicator(Math.max(groups.length, 1), groups.length === 0 ? 1 : pageIndex) +
            (currentYear === new Date().getFullYear() ? monthNames[new Date().getMonth()] : "December") +
            " " +
            currentYear.toString();
        if (groups.length > 0) {
            const toRender = groups[Math.min(pageIndex, groups.length - 1)]!;
            m = await interaction.editReply({
                embeds: [
                    new EmojiEmbed()
                        .setEmoji("MEMBER.JOIN")
                        .setTitle("Moderation history for " + member.user.username)
                        .setDescription(
                            `**${toRender.name}**\n\n` + toRender.content.map((c) => c.rendered).join("\n") + end
                        )
                        .setStatus("Success")
                        .setFooter({
                            text:
                                openFilterPane && filteredTypes.length
                                    ? "Filters are currently enabled"
                                    : "No filters selected"
                        })
                ],
                components: components
            });
        } else {
            m = await interaction.editReply({
                embeds: [
                    new EmojiEmbed()
                        .setEmoji("MEMBER.JOIN")
                        .setTitle("Moderation history for " + member.user.username)
                        .setDescription(`**${currentYear}**\n\n*No events*`)
                        .setStatus("Success")
                        .setFooter({
                            text:
                                openFilterPane && filteredTypes.length
                                    ? "Filters are currently enabled"
                                    : "No filters selected"
                        })
                ],
                components: components
            });
        }
        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 (e) {
            await interaction.editReply({
                embeds: [
                    new EmojiEmbed()
                        .setEmoji("MEMBER.JOIN")
                        .setTitle("Moderation history for " + member.user.username)
                        .setDescription(m.embeds[0]!.description!)
                        .setStatus("Danger")
                        .setFooter({ text: "Message timed out" })
                ]
            });
            timedOut = true;
            continue;
        }
        await i.deferUpdate();
        if (i.customId === "filter" && i.isStringSelectMenu()) {
            filteredTypes = i.values;
            pageIndex = null;
            refresh = true;
        }
        if (i.customId === "prevYear") {
            currentYear--;
            pageIndex = null;
            refresh = true;
        }
        if (i.customId === "nextYear") {
            currentYear++;
            pageIndex = null;
            refresh = true;
        }
        if (i.customId === "prevPage") {
            pageIndex!--;
            if (pageIndex! < 0) {
                pageIndex = null;
                currentYear--;
                refresh = true;
            }
        }
        if (i.customId === "nextPage") {
            pageIndex!++;
            if (pageIndex! >= groups.length) {
                pageIndex = 0;
                currentYear++;
                refresh = true;
            }
        }
        if (i.customId === "today") {
            currentYear = new Date().getFullYear();
            pageIndex = null;
            refresh = true;
        }
        if (i.customId === "modNotes") {
            showHistorySelected = true;
        }
        if (i.customId === "openFilter") {
            openFilterPane = !openFilterPane;
            refresh = true;
        }
    }
    return timedOut ? 0 : 1;
}

export const noteMenu = async (
    member: GuildMember,
    interaction: CommandInteraction | ContextMenuCommandInteraction
): Promise<unknown> => {
    let m: Message;
    await interaction.reply({
        embeds: LoadingEmbed,
        ephemeral: true,
        fetchReply: true
    });
    let note;
    let timedOut = false;
    while (!timedOut) {
        note = await client.database.notes.read(member.guild.id, member.id);
        const colors: Record<string, Discord.ColorResolvable> = {
            none: "#424242",
            red: "#F27878",
            yellow: "#EDC575",
            green: "#68D49E",
            blue: "#6576CC",
            purple: "#D46899",
            gray: "#C4C4C4"
        };
        m = (await interaction.editReply({
            embeds: [
                new EmojiEmbed()
                    .setEmoji(`ICONS.FLAGS.${(note?.flag ?? "none").toUpperCase()}`)
                    .setTitle("Mod notes for " + member.user.username)
                    .setDescription(note?.note ? note.note : "*No note set*")
                    .setColor(colors[note?.flag ?? "none"]!)
            ],
            components: [
                new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
                    new ButtonBuilder()
                        .setLabel(`${note?.note ? "Modify" : "Create"} note`)
                        .setStyle(ButtonStyle.Primary)
                        .setCustomId("modify")
                        .setEmoji(getEmojiByName("ICONS.EDIT", "id")),
                    new ButtonBuilder()
                        .setLabel("Moderation history")
                        .setStyle(ButtonStyle.Primary)
                        .setCustomId("history")
                        .setEmoji(getEmojiByName("ICONS.HISTORY", "id"))
                ]),
                new ActionRowBuilder<Discord.StringSelectMenuBuilder>().addComponents([
                    new StringSelectMenuBuilder()
                        .setCustomId("flag")
                        .setPlaceholder("Select a flag")
                        .addOptions(
                            new StringSelectMenuOptionBuilder()
                                .setLabel("None")
                                .setDefault(!note?.flag)
                                .setValue("none")
                                .setDescription("Clear")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.NONE", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Red")
                                .setDefault(note?.flag === "red")
                                .setValue("red")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.RED", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Yellow")
                                .setDefault(note?.flag === "yellow")
                                .setValue("yellow")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.YELLOW", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Green")
                                .setDefault(note?.flag === "green")
                                .setValue("green")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.GREEN", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Blue")
                                .setDefault(note?.flag === "blue")
                                .setValue("blue")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.BLUE", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Purple")
                                .setDefault(note?.flag === "purple")
                                .setValue("purple")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.PURPLE", "id")),
                            new StringSelectMenuOptionBuilder()
                                .setLabel("Gray")
                                .setDefault(note?.flag === "gray")
                                .setValue("gray")
                                .setEmoji(getEmojiByName("ICONS.FLAGS.GRAY", "id"))
                        )
                ])
            ]
        })) as Message;
        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 (e) {
            timedOut = true;
            continue;
        }
        if (i.isButton()) {
            if (i.customId === "modify") {
                await i.showModal(
                    new Discord.ModalBuilder()
                        .setCustomId("modal")
                        .setTitle("Editing moderator note")
                        .addComponents(
                            new ActionRowBuilder<Discord.TextInputBuilder>().addComponents(
                                new Discord.TextInputBuilder()
                                    .setCustomId("note")
                                    .setLabel("Note")
                                    .setMaxLength(4000)
                                    .setRequired(false)
                                    .setStyle(TextInputStyle.Paragraph)
                                    .setValue(note?.note ? note.note : " ")
                            )
                        )
                );
                await interaction.editReply({
                    embeds: [
                        new EmojiEmbed()
                            .setTitle("Mod notes for " + member.user.username)
                            .setDescription("Modal opened. If you can't see it, click back and try again.")
                            .setStatus("Success")
                            .setEmoji("GUILD.TICKET.OPEN")
                    ],
                    components: [
                        new ActionRowBuilder<Discord.ButtonBuilder>().addComponents([
                            new ButtonBuilder()
                                .setLabel("Back")
                                .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
                                .setStyle(ButtonStyle.Primary)
                                .setCustomId("back")
                        ])
                    ]
                });
                let out;
                try {
                    out = await modalInteractionCollector(m, interaction.user);
                } catch (e) {
                    timedOut = true;
                    continue;
                }
                if (out === null || out.isButton()) {
                    continue;
                } else {
                    let toAdd = out.fields.getTextInputValue("note").trim() || null;
                    if (toAdd === "") toAdd = null;
                    await client.database.notes.create(member.guild.id, member.id, toAdd);
                }
            } else if (i.customId === "history") {
                await i.deferUpdate();
                if (!(await showHistory(member, interaction))) return;
            }
        } else if (i.isStringSelectMenu()) {
            await i.deferUpdate();
            let flag: string | null = i.values[0]!;
            if (flag === "none") flag = null;
            await client.database.notes.flag(member.guild.id, member.id, flag as FlagColors | null);
        }
    }
};

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

const check = (interaction: CommandInteraction) => {
    const member = interaction.member as GuildMember;
    if (!member.permissions.has("ManageMessages")) return "You do not have the *Manage Messages* permission";
    return true;
};

export { command, callback, check };
export const metadata = {
    longDescription:
        "Shows the moderation history (all previous bans, kicks, warns etc.), and moderator notes for a user.",
    premiumOnly: true
};
