import {
    ActionRowBuilder,
    CommandInteraction,
    StringSelectMenuBuilder,
    ApplicationCommandOptionType,
    ApplicationCommandType,
    StringSelectMenuOptionBuilder,
    SlashCommandBuilder,
    StringSelectMenuInteraction,
    ComponentType,
    APIMessageComponentEmoji,
    ApplicationCommandSubGroup,
    PermissionsBitField,
    Interaction,
    ApplicationCommandOption,
    ApplicationCommandSubCommand
} from "discord.js";
import client from "../utils/client.js";
import EmojiEmbed from "../utils/generateEmojiEmbed.js";
import { LoadingEmbed } from "../utils/defaults.js";
import { capitalize } from "../utils/generateKeyValueList.js";
import { getCommandByName, getCommandMentionByName } from "../utils/getCommandDataByName.js";
import getEmojiByName from "../utils/getEmojiByName.js";

const command = new SlashCommandBuilder()
    .setName("help")
    .setDescription("Shows help for commands");

const styles: Record<string, {emoji: string}> = {
    "help": {emoji: "NUCLEUS.LOGO"},
    "mod": {emoji: "PUNISH.BAN.RED"},
    "nucleus": {emoji: "NUCLEUS.LOGO"},
    "privacy": {emoji: "NUCLEUS.LOGO"},
    "role": {emoji: "GUILD.ROLES.DELETE"},
    "rolemenu": {emoji: "GUILD.ROLES.DELETE"},
    "server": {emoji: "GUILD.RED"},
    "settings": {emoji: "GUILD.SETTINGS.RED"},
    "tag": {emoji: "PUNISH.NICKNAME.RED"},
    "tags": {emoji: "PUNISH.NICKNAME.RED"},
    "ticket": {emoji: "GUILD.TICKET.CLOSE"},
    "user": {emoji: "MEMBER.LEAVE"},
    "verify": {emoji: "CONTROL.REDTICK"}
}

const callback = async (interaction: CommandInteraction): Promise<void> => {
    const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true });
    const commands = client.fetchedCommands;

    let closed = false;
    let currentPath: [string, string, string] = ["", "", ""]
    do {
        const commandRow = new ActionRowBuilder<StringSelectMenuBuilder>()
            .addComponents(
                new StringSelectMenuBuilder()
                    .setCustomId("commandRow")
                    .setPlaceholder("Select a command")
                    .addOptions(
                        ...commands.filter(command => command.type === ApplicationCommandType.ChatInput).map((command) => {
                            const builder = new StringSelectMenuOptionBuilder()
                                .setLabel(capitalize(command.name))
                                .setValue(command.name)
                                .setDescription(command.description)
                                .setDefault(currentPath[0] === command.name)
                            if (styles[command.name]) builder.setEmoji(getEmojiByName(styles[command.name]!.emoji, "id") as APIMessageComponentEmoji)
                            return builder
                        })
                    )
        );
        const subcommandGroupRow = new ActionRowBuilder<StringSelectMenuBuilder>()
            .addComponents(
                new StringSelectMenuBuilder()
                    .setCustomId("subcommandGroupRow")
            );
        const subcommandRow = new ActionRowBuilder<StringSelectMenuBuilder>()
            .addComponents(
                new StringSelectMenuBuilder()
                    .setCustomId("subcommandRow")
            );
        const embed = new EmojiEmbed()
            .setTitle("Help")
            .setStatus("Danger")
            .setEmoji("NUCLEUS.LOGO")

        if(currentPath[0] === "" || currentPath[0] === "help") {
            embed.setDescription(
                `Welcome to Nucleus\n\n` +
                `Select a command to get started${
                    (interaction.member?.permissions as PermissionsBitField).has("ManageGuild") ?
                        `, or run ${getCommandMentionByName("nucleus/guide")} for commands to set up your server` : ``
                    }\n\n\n` +
                `Nucleus is fully [open source](https://github.com/clicksminuteper/Nucleus), and all currently free features will remain free forever.\n\n` +
                `You can invite Nucleus to your server using ${getCommandMentionByName("nucleus/invite")}`
            )
        } else {
            const currentData = getCommandByName(currentPath.filter(value => value !== "" && value !== "none").join('/'));
            const current = commands.find((command) => command.name === currentPath[0])!;

            let optionString = ``
            let options: (ApplicationCommandOption & {
                nameLocalized?: string;
                descriptionLocalized?: string;
            })[] = [];
            //o ptions
            if(currentPath[1] !== "" && currentPath[1] !== "none" && currentPath[2] !== "" && currentPath[2] !== "none") {
                const Op = current.options.find(option => option.name === currentPath[1])! as ApplicationCommandSubGroup
                const Op2 = Op.options!.find(option => option.name === currentPath[2])!
                options = Op2.options ?? []
            } else if(currentPath[1] !== "" && currentPath[1] !== "none") {
                let Op = current.options.find(option => option.name === currentPath[1])!
                if(Op.type === ApplicationCommandOptionType.SubcommandGroup) {
                    options = []
                } else {
                    Op = Op as ApplicationCommandSubCommand
                    options = Op.options ?? []
                }
            } else {
                options = current.options.filter(option => option.type !== ApplicationCommandOptionType.SubcommandGroup && option.type !== ApplicationCommandOptionType.Subcommand) || [];
            }
            for(const option of options) {
                optionString += `> ${option.name} (${ApplicationCommandOptionType[option.type]})- ${option.description}\n`
            }
            const APICommand = client.commands["commands/" + currentPath.filter(value => value !== "" && value !== "none").join("/")]![0]
            let allowedToRun = true;
            if(APICommand?.check) {
                allowedToRun = await APICommand.check(interaction as Interaction, true)
            }
            embed.setDescription(
                `${getEmojiByName(styles[currentPath[0]]!.emoji)} **${capitalize(currentData.name)}**\n> ${currentData.mention}\n\n` +
                `> ${currentData.description}\n\n` +
                (APICommand ? (`${getEmojiByName(allowedToRun ? "CONTROL.TICK" : "CONTROL.CROSS")} You ${allowedToRun ? "" : "don't "}` +
                `have permission to use this command\n\n`) : "") +
                ((optionString.length > 0) ? "**Options:**\n" + optionString : "")
            )
            const subcommands = current.options.filter((option) => option.type === ApplicationCommandOptionType.Subcommand);
            const subcommandGroups = current.options.filter((option) => option.type === ApplicationCommandOptionType.SubcommandGroup);

            if(subcommandGroups.length > 0) {
                subcommandGroupRow.components[0]!
                    .addOptions(
                        new StringSelectMenuOptionBuilder().setLabel("Select a subcommand").setValue("none").setDefault(currentPath[1] === "none"),
                        ...subcommandGroups.map((option) => new StringSelectMenuOptionBuilder().setLabel(capitalize(option.name)).setValue(option.name).setDefault(currentPath[1] === option.name))
                    )
                if(subcommandGroupRow.components[0]!.options.find((option) => option.data.default && option.data.value !== "none")) {
                    const subsubcommands = (subcommandGroups.find((option) => option.name === currentPath[1])! as ApplicationCommandSubGroup).options?.filter((option) => option.type === ApplicationCommandOptionType.Subcommand) ?? [];
                    subcommandRow.components[0]!
                        .addOptions(
                            new StringSelectMenuOptionBuilder().setLabel("Select a subcommand").setValue("none").setDefault(currentPath[2] === "none"),
                            ...subsubcommands.map((option) => new StringSelectMenuOptionBuilder().setLabel(capitalize(option.name)).setValue(option.name).setDefault(currentPath[2] === option.name))
                        )
                }
            }
            if(subcommands.length > 0) {
                subcommandGroupRow.components[0]!
                    .addOptions(
                        ...subcommands.map((option) => new StringSelectMenuOptionBuilder().setLabel(capitalize(option.name)).setValue(option.name).setDefault(currentPath[1] === option.name))
                    )
            }
        }

        const cmps = [commandRow];
        if(subcommandGroupRow.components[0]!.options.length > 0) cmps.push(subcommandGroupRow);
        if(subcommandRow.components[0]!.options.length > 0) cmps.push(subcommandRow);

        await interaction.editReply({ embeds: [embed], components: cmps });

        let i: StringSelectMenuInteraction;
        try {
            i = await m.awaitMessageComponent<ComponentType.StringSelect>({filter: (newInteraction) => interaction.user.id === newInteraction.user.id,time: 300000})
        } catch (e) {
            closed = true;
            continue;
        }
        await i.deferUpdate();
        const value = i.values[0]!;
        switch(i.customId) {
            case "commandRow": {
                currentPath = [value, "", ""];
                break;
            }
            case "subcommandGroupRow": {
                currentPath = [currentPath[0], value , ""];
                break;
            }
            case "subcommandRow": {
                currentPath[2] = value;
                break;
            }
        }

    } while (!closed);
};


export { command as command };
export { callback };
