package com.nincraft.ninbot.components.twitch;

import com.nincraft.ninbot.components.common.MessageUtils;
import com.nincraft.ninbot.components.config.ConfigConstants;
import com.nincraft.ninbot.components.config.ConfigService;
import lombok.val;
import net.dv8tion.jda.core.events.user.update.GenericUserPresenceEvent;
import net.dv8tion.jda.core.events.user.update.UserUpdateGameEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import org.springframework.stereotype.Component;

@Component
public class TwitchListener extends ListenerAdapter {

    private ConfigService configService;
    private MessageUtils messageUtils;

    public TwitchListener(ConfigService configService, MessageUtils messageUtils) {
        this.configService = configService;
        this.messageUtils = messageUtils;
    }

    @Override
    public void onGenericUserPresence(GenericUserPresenceEvent event) {
        if (event instanceof UserUpdateGameEvent) {
            val updateGameEvent = (UserUpdateGameEvent) event;
            if (updateGameEvent.getOldGame() != null && updateGameEvent.getNewGame() != null
                    && updateGameEvent.getOldGame().getUrl() == null && updateGameEvent.getNewGame().getUrl() != null) {
                val serverId = event.getGuild().getId();
                val streamingAnnounceUsers = configService.getValuesByName(serverId, ConfigConstants.STREAMING_ANNOUNCE_USERS);
                if (streamingAnnounceUsers.contains(event.getMember().getUser().getId())) {
                    val streamingAnnounceChannel = configService.getSingleValueByName(serverId, ConfigConstants.STREAMING_ANNOUNCE_CHANNEL);
                    if (streamingAnnounceChannel.isPresent()) {
                        val channel = event.getGuild().getTextChannelById(streamingAnnounceChannel.get());
                        val user = updateGameEvent.getUser().getName();
                        val url = updateGameEvent.getNewGame().getUrl();
                        messageUtils.sendMessage(channel, "%s is streaming! Check them out at %s", user, url);
                    }
                }
            }
        }
    }
}

@@ -222,16 +222,51 @@
 ervice;%0A
+import lombok.extern.log4j.Log4j2;%0A
 import l
@@ -529,16 +529,24 @@
 mponent%0A
+@Log4j2%0A
 public c
@@ -1051,16 +1051,211 @@
  event;%0A
+            log.debug(%22UserUpdateEvent %22 + updateGameEvent);%0A            log.debug(%22Old game %22 + updateGameEvent.getOldGame());%0A            log.debug(%22New game %22 + updateGameEvent.getNewGame());%0A
         

Add logging to Twitch listener