package xyz.rc24.bot.events;

import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.guild.GenericGuildEvent;
import net.dv8tion.jda.core.events.guild.GuildBanEvent;
import net.dv8tion.jda.core.events.guild.GuildUnbanEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import xyz.rc24.bot.mangers.LogManager;
import xyz.rc24.bot.mangers.LogManager.LogType;

import java.awt.*;
import java.time.Instant;
import java.time.format.DateTimeFormatter;

public class ServerLog extends ListenerAdapter {
    private LogManager manager;
    private static final Logger logger = LoggerFactory.getLogger(ServerLog.class);

    public ServerLog(JedisPool pool) {
        this.manager = new LogManager(pool);
        logger.info("Tracking :eyes:");
    }

    @Override
    public void onGuildBan(GuildBanEvent event) {
        EmbedBuilder builder = getEmbed("A user was banned from the server!",
                "#D32F2F",
                event.getUser());

        sendEmbed(new LogType[]{LogType.SERVER, LogType.MOD}, builder, event);
    }

    @Override
    public void onGuildUnban(GuildUnbanEvent event) {
        EmbedBuilder builder = getEmbed("A user was unbanned from the server!",
                "#4CAF50",
                event.getUser());

        sendEmbed(new LogType[]{LogType.SERVER, LogType.MOD}, builder, event);
    }

    @Override
    public void onGuildMemberLeave(GuildMemberLeaveEvent event) {
        if (!event.getGuild().getBans().complete().contains(event.getUser())) {
            EmbedBuilder builder = getEmbed("A user left the server!",
                    "#FFEB3B",
                    event.getUser());

            sendEmbed(new LogType[]{LogType.SERVER}, builder, event);
        }
        // Don't do anything if banned.
        // Bans send both a leave event + ban event.
    }

    @Override
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        EmbedBuilder builder = getEmbed("A user just joined the server!",
                "#00C853",
                event.getUser());

        sendEmbed(new LogType[]{LogType.SERVER}, builder, event);
    }

    private EmbedBuilder getEmbed(String title, String color, User user) {
        EmbedBuilder builder = new EmbedBuilder();
        builder.setTitle(title);
        builder.setDescription("User: " + user.getAsMention() + " | " + user.getName() + "#" + user.getDiscriminator() + "\n" +
                "Account creation date: `" + user.getCreationTime().format(DateTimeFormatter.RFC_1123_DATE_TIME) + "`");
        builder.setColor(Color.decode(color));
        builder.setFooter("Current time: ", null);
        builder.setTimestamp(Instant.now());
        return builder;
    }

    private void sendEmbed(LogType[] logTypes, EmbedBuilder embed, GenericGuildEvent guildEvent) {
        Long guildID = guildEvent.getGuild().getIdLong();

        for (LogType logType : logTypes) {
            if (manager.isLogEnabled(logType, guildID)) {
                guildEvent.getJDA()
                        .getTextChannelById(manager.getLog(logType, guildID))
                        .sendMessage(embed.build()).complete();
            }
        }
    }
}

@@ -419,24 +419,96 @@
 LeaveEvent;%0A
+import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;%0A
 import net.d
@@ -1773,32 +1773,50 @@
 eEvent event) %7B%0A
+        try %7B%0A    
         if (!eve
@@ -1883,32 +1883,36 @@
 ) %7B%0A            
+    
 EmbedBuilder bui
@@ -1974,16 +1974,20 @@
         
+    
 %22#FFEB3B
@@ -1989,16 +1989,20 @@
 FEB3B%22,%0A
+    
         
@@ -2024,32 +2024,548 @@
 nt.getUser());%0A%0A
+                sendEmbed(new LogType%5B%5D%7BLogType.SERVER%7D, builder, event);%0A            %7D%0A        %7D catch (InsufficientPermissionException e) %7B%0A            EmbedBuilder builder = new EmbedBuilder();%0A            builder.setTitle(%22Warning!%22);%0A            builder.setDescription(%22I don't have the Ban Members permission. I won't ban anyone, but without it I can't tell if anyone's leaving, or if it was a ban.%22);%0A            builder.setFooter(%22Error generated at%22, null);%0A            builder.setTimestamp(Instant.now());%0A
             send

Add warning for leave command