package com.nincraft.ninbot.components.fun;

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.entities.MessageChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class Dadbot extends ListenerAdapter {

    private Random random;
    private ConfigService configService;
    private MessageUtils messageUtils;

    @Autowired
    public Dadbot(ConfigService configService, MessageUtils messageUtils) {
        random = new Random();
        this.configService = configService;
        this.messageUtils = messageUtils;
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        if (!event.getAuthor().isBot()) {
            parseMessage(event);
        }
    }

    private void parseMessage(MessageReceivedEvent event) {
        if (!event.getChannelType().isGuild()
                || channelIsBlacklisted(event.getGuild().getId(), event.getChannel().getId())) {
            return;
        }
        val message = event.getMessage().getContentStripped();
        if (StringUtils.isNotBlank(message) && message.split("\\s+").length >= 1) {
            String first = message.split("\\s+")[0];
            if ((first.equalsIgnoreCase("I'm") || first.equalsIgnoreCase("im")) && checkChance()) {
                hiImDad(message, event.getChannel());
            }
        }
    }

    private boolean channelIsBlacklisted(String serverId, String channelId) {
        val channelConfigList = configService.getConfigByName(serverId, ConfigConstants.DADBOT_BLACKLIST_CHANNEL);
        return channelConfigList.stream().anyMatch(config -> config.getValue().equals(channelId));
    }

    private void hiImDad(String message, MessageChannel channel) {
        String stringBuilder = "Hi" +
                message.substring(message.indexOf(' ')) +
                ", I'm Dad!";
        messageUtils.sendMessage(channel, stringBuilder);
    }

    private boolean checkChance() {
        return random.nextInt(100) < 10;
    }
}

@@ -242,61 +242,8 @@
 al;%0A
-import net.dv8tion.jda.core.entities.MessageChannel;%0A
 impo
@@ -1161,45 +1161,62 @@
     
-if (!event.getChannelType().isGuil
+val message = event.getMessage().getContentStrippe
 d()
+;
 %0A   
@@ -1224,66 +1224,128 @@
     
-        %7C%7C channelIsBlacklisted(event.getGuild().getId(), 
+val first = message.split(%22%5C%5Cs+%22)%5B0%5D;%0A        if (!(first.equalsIgnoreCase(%22I'm%22) %7C%7C first.equalsIgnoreCase(%22im%22)) %7C%7C (!
 even
@@ -1360,15 +1360,21 @@
 nnel
-().getI
+Type().isGuil
 d())
@@ -1411,71 +1411,8 @@
   %7D%0A
-        val message = event.getMessage().getContentStripped();%0A
     
@@ -1491,144 +1491,8 @@
 %3E= 1
-) %7B%0A            String first = message.split(%22%5C%5Cs+%22)%5B0%5D;%0A            if ((first.equalsIgnoreCase(%22I'm%22) %7C%7C first.equalsIgnoreCase(%22im%22))
  && 
@@ -1504,28 +1504,24 @@
 Chance()) %7B%0A
-    
             
@@ -1546,37 +1546,10 @@
 vent
-.getChannel());%0A            %7D
+);
 %0A   
@@ -1913,26 +1913,151 @@
 sage
-Channel channel) %7B
+ReceivedEvent event) %7B%0A        if (!channelIsBlacklisted(event.getGuild().getId(), event.getChannel().getId())) %7B%0A            return;%0A        %7D
 %0A   
@@ -2212,23 +2212,34 @@
 Message(
-c
+event.getC
 hannel
+()
 , string

Fix too many SQL calls due to bad code