package dev.nincodedo.ninbot.components.announcements;

import dev.nincodedo.ninbot.common.LocaleService;
import dev.nincodedo.ninbot.common.StatAwareListenerAdapter;
import dev.nincodedo.ninbot.components.config.ConfigConstants;
import dev.nincodedo.ninbot.components.config.ConfigService;
import dev.nincodedo.ninbot.components.config.component.ComponentService;
import dev.nincodedo.ninbot.components.config.component.ComponentType;
import dev.nincodedo.ninbot.components.stats.StatManager;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.emote.EmoteAddedEvent;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

import java.util.ResourceBundle;

@Component
public class EmoteCreationAnnouncement extends StatAwareListenerAdapter {

    private ComponentService componentService;
    private ConfigService configService;
    private String componentName;

    public EmoteCreationAnnouncement(StatManager statManager, ConfigService configService,
            ComponentService componentService) {
        super(statManager);
        this.componentService = componentService;
        this.configService = configService;
        componentName = "emote-added-announcement";
        componentService.registerComponent(componentName, ComponentType.LISTENER);
    }

    @Override
    public void onEmoteAdded(EmoteAddedEvent event) {
        if (componentService.isDisabled(componentName, event.getGuild().getId())) {
            return;
        }
        var optionalChannelId = configService.getSingleValueByName(event.getGuild()
                .getId(), ConfigConstants.EMOTE_ADDED_ANNOUNCEMENT_CHANNEL_ID);
        if (optionalChannelId.isPresent()) {
            var emoteAddedChannelId = optionalChannelId.get();
            var channel = event.getJDA().getTextChannelById(emoteAddedChannelId);
            if (channel != null) {
                var emote = event.getEmote();
                countOneStat(componentName, event.getGuild().getId());
                Member member = null;
                if (event.getGuild()
                        .getMember(event.getJDA().getSelfUser())
                        .getPermissions(channel)
                        .contains(Permission.VIEW_AUDIT_LOGS)) {
                    member = event.getGuild()
                            .getMember(event.getGuild().retrieveAuditLogs().complete().get(0).getUser());
                }
                channel.sendMessage(buildAnnouncementMessage(emote, event.getGuild(), member))
                        .queue(message -> message.addReaction(emote).queue());
            }
        }
    }

    @NotNull
    private Message buildAnnouncementMessage(Emote emote, Guild guild, Member member) {
        ResourceBundle resourceBundle = LocaleService.getResourceBundleOrDefault(guild);
        MessageBuilder messageBuilder = new MessageBuilder();
        messageBuilder.append(resourceBundle.getString("listener.emote.announce.message"));
        if (member != null) {
            messageBuilder.append(" ");
            messageBuilder.append(resourceBundle.getString("listener.emote.announce.message.member"));
            messageBuilder.append(member);
        }
        messageBuilder.append("\n");
        messageBuilder.append(emote);
        return messageBuilder.build();
    }
}

@@ -483,24 +483,58 @@
 tatManager;%0A
+import lombok.extern.slf4j.Slf4j;%0A
 import net.d
@@ -560,24 +560,24 @@
 ageBuilder;%0A
-
 import net.d
@@ -961,16 +961,23 @@
 undle;%0A%0A
+@Slf4j%0A
 @Compone
@@ -1755,32 +1755,190 @@
 turn;%0A        %7D%0A
+        log.trace(%22Event Response %7B%7D: Running EmoteCreationAnnouncement for server %7B%7D%22, event.getResponseNumber(),%0A                event.getGuild().getId());%0A
         var opti
@@ -2189,24 +2189,170 @@
 elId.get();%0A
+            log.trace(%22Event Response %7B%7D: Emote announcement channel id %7B%7D%22, event.getResponseNumber(),%0A                    emoteAddedChannelId);%0A
             
@@ -2448,32 +2448,134 @@
 nnel != null) %7B%0A
+                log.trace(%22Event Response %7B%7D: Found channel %7B%7D%22, event.getResponseNumber(), channel);%0A
                 
@@ -3235,16 +3235,242 @@
 ssage -%3E
+ %7B%0A                            log.trace(%22Event Response %7B%7D: Sending message for %7B%7D in %7B%7D%22, event.getResponseNumber(),%0A                                    emote.getName(), event.getGuild().getId());%0A                           
  message
@@ -3496,16 +3496,43 @@
 .queue()
+;%0A                        %7D
 );%0A     

Add logging to emote announcements