import { LoadingEmbed } from "../../../utils/defaults.js";
import Discord, {
    CommandInteraction,
    ActionRowBuilder,
    ButtonBuilder,
    ButtonStyle,
    ChannelSelectMenuBuilder,
    ChannelType,
    ComponentType
} from "discord.js";
import EmojiEmbed from "../../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../../utils/getEmojiByName.js";
import type { SlashCommandSubcommandBuilder } from "discord.js";
import client from "../../../utils/client.js";
import _ from "lodash";

const command = (builder: SlashCommandSubcommandBuilder) =>
    builder.setName("warnings").setDescription("Settings for the staff notifications channel");

const callback = async (interaction: CommandInteraction): Promise<unknown> => {
    if (!interaction.guild) return;
    const m = await interaction.reply({
        embeds: LoadingEmbed,
        ephemeral: true,
        fetchReply: true
    });

    let data = await client.database.guilds.read(interaction.guild.id);
    let channel = _.clone(data.logging.staff.channel);
    let closed = false;
    do {
        const channelMenu = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
            new ChannelSelectMenuBuilder()
                .setCustomId("channel")
                .setPlaceholder("Select a channel")
                .setChannelTypes(ChannelType.GuildText)
        );
        const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
            new ButtonBuilder()
                .setCustomId("clear")
                .setLabel("Clear")
                .setStyle(ButtonStyle.Danger)
                .setEmoji(getEmojiByName("CONTROL.CROSS", "id") as Discord.APIMessageComponentEmoji)
                .setDisabled(!channel),
            new ButtonBuilder()
                .setCustomId("save")
                .setLabel("Save")
                .setStyle(ButtonStyle.Success)
                .setEmoji(getEmojiByName("ICONS.SAVE", "id") as Discord.APIMessageComponentEmoji)
                .setDisabled(_.isEqual(channel, data.logging.staff.channel))
        );

        const embed = new EmojiEmbed()
            .setTitle("Staff Notifications Channel")
            .setStatus("Success")
            .setEmoji("CHANNEL.TEXT.CREATE")
            .setDescription(
                `Logs which require an action from a moderator or administrator will be sent to this channel.\n` +
                    `**Channel:** ${channel ? `<#${channel}>` : "*None*"}\n`
            );

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

        let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
        try {
            i = await interaction.channel!.awaitMessageComponent<ComponentType.Button | ComponentType.ChannelSelect>({
                filter: (i: Discord.Interaction) =>
                    i.user.id === interaction.user.id && i.isMessageComponent() && i.message.id === m.id,
                time: 300000
            });
        } catch (e) {
            closed = true;
            continue;
        }
        await i.deferUpdate();
        if (i.isButton()) {
            switch (i.customId) {
                case "clear": {
                    channel = null;
                    break;
                }
                case "save": {
                    await client.database.guilds.write(interaction.guild!.id, {
                        "logging.staff.channel": channel
                    });
                    data = await client.database.guilds.read(interaction.guild!.id);
                    await client.memory.forceUpdate(interaction.guild!.id);
                    break;
                }
            }
        } else {
            channel = i.values[0]!;
        }
    } while (!closed);

    await interaction.deleteReply();
};

const check = (interaction: CommandInteraction, _partial: boolean = false) => {
    const member = interaction.member as Discord.GuildMember;
    if (!member.permissions.has("ManageGuild"))
        return "You must have the *Manage Server* permission to use this command";
    return true;
};

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