package com.alttd.webinterface.appeals;

import com.alttd.webinterface.bot.DiscordBotInstance;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Guild;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class DiscordAppealDiscord {
    private static final DiscordAppealDiscord INSTANCE = new DiscordAppealDiscord();

    public static DiscordAppealDiscord getInstance() {
        return INSTANCE;
    }

    public CompletableFuture<Optional<BannedUser>> getBannedUser(long discordId) {
        Guild guildById = DiscordBotInstance.getInstance()
                .getJda()
                .getGuildById(141644560005595136L);
        if (guildById == null) {
            throw new IllegalStateException("Guild not found");
        }
        CompletableFuture<Optional<BannedUser>> completableFuture = new CompletableFuture<>();
        log.info("Retrieving ban for user {}", discordId);
        DiscordBotInstance.getInstance().getJda().retrieveUserById(discordId)
                .queue(user -> {
                    log.info("Found user {}", user.getEffectiveName());
                    guildById.retrieveBan(user).queue(ban -> {
                        if (ban == null) {
                            completableFuture.complete(Optional.empty());
                            log.info("User {} is not banned", user.getEffectiveName());
                            return;
                        }
                        log.info("User {} is banned", user.getEffectiveName());
                        completableFuture.complete(Optional.of(BanToBannedUser.map(ban)));
                    });

                });
        return completableFuture;
    }
}
