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

export const command = new SlashCommandSubcommandBuilder()
    .setName("autopublish")
    .setDescription("Automatically publish messages posted in announcement channels");

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

    let closed = false;
    let config = await client.database.guilds.read(interaction.guild!.id);
    let data = _.cloneDeep(config.autoPublish);
    do {
        const buttons = new ActionRowBuilder<ButtonBuilder>().addComponents(
            new ButtonBuilder()
                .setCustomId("switch")
                .setLabel(data.enabled ? "Enabled" : "Disabled")
                .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
                .setEmoji(
                    getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji
                ),
            new ButtonBuilder()
                .setCustomId("save")
                .setLabel("Save")
                .setStyle(ButtonStyle.Success)
                .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
                .setDisabled(_.isEqual(data, config.autoPublish))
        );

        const channelSelect = new ActionRowBuilder<ChannelSelectMenuBuilder>().addComponents(
            new ChannelSelectMenuBuilder()
                .setCustomId("channel")
                .setPlaceholder("Select a channel")
                .setMinValues(1)
                .setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread)
        );

        const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*";

        const embed = new EmojiEmbed()
            .setTitle("Auto Publish")
            .setDescription("Currently enabled in:\n" + current)
            .setStatus("Success")
            .setEmoji("ICONS.PUBLISH");

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

        let i: Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
        try {
            i = (await interaction.channel!.awaitMessageComponent({
                filter: (i: Discord.Interaction) => i.user.id === interaction.user.id,
                time: 300000
            })) as Discord.ButtonInteraction | Discord.ChannelSelectMenuInteraction;
        } catch (e) {
            closed = true;
            continue;
        }
        await i.deferUpdate();
        if (i.isButton()) {
            switch (i.customId) {
                case "switch": {
                    data.enabled = !data.enabled;
                    break;
                }
                case "save": {
                    await client.database.guilds.write(interaction.guild!.id, { autoPublish: data });
                    config = await client.database.guilds.read(interaction.guild!.id);
                    data = _.cloneDeep(config.autoPublish);
                    await client.memory.forceUpdate(interaction.guild!.id);
                    break;
                }
            }
        } else {
            await interaction.editReply({ embeds: LoadingEmbed, components: [] });
            for (const channel of i.values) {
                data.channels.includes(channel)
                    ? data.channels.splice(data.channels.indexOf(channel), 1)
                    : data.channels.push(channel);
            }
        }
    } while (!closed);

    await interaction.deleteReply();
};

export const check = (interaction: CommandInteraction, _partial: boolean = false) => {
    const member = interaction.member as Discord.GuildMember;
    const me = interaction.guild!.members.me!;
    if (!member.permissions.has("ManageMessages"))
        return "You must have the *Manage Messages* permission to use this command";
    if (_partial) return true;
    if (!me.permissions.has("ManageMessages")) return "I do not have the *Manage Messages* permission";
    return true;
};
