package meew0.mewtwo.thread;

import meew0.mewtwo.MewtwoMain;
import meew0.mewtwo.irc.ConsoleUser;
import org.pircbotx.Channel;
import org.pircbotx.Configuration;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.hooks.events.MessageEvent;

/**
 * Created by meew0 on 31.08.14.
 */
public class BotWrapperThread implements Runnable {
    private final Configuration.Builder<PircBotX> configuration;
    private final String threadName;

    public BotWrapperThread(Configuration.Builder<PircBotX> configuration, String nick, String serverHostName, int serverPort, String threadName) {
        this.configuration = configuration;

        this.configuration.setName(nick);
        this.configuration.setServer(serverHostName, serverPort);

        this.threadName = threadName;
    }

    @Override
    public void run() {
        final PircBotX mewtwo = new PircBotX(configuration.buildConfiguration());

        MewtwoMain.mewtwoLogger.info("Bot created, starting up...");

        MewtwoMain.mewtwoLogger.info("Starting up processing thread...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    if(InputWatchThread.hasNextFor(threadName)) {
                        MewtwoMain.mewtwoLogger.info("Hey! There's something for me to do!");
                        InputWatchThread.InputEntry entry = InputWatchThread.getNext();
                        Channel channel = mewtwo.getUserChannelDao().getChannel(entry.getChannel());
                        User user = new ConsoleUser(mewtwo);
                        MessageEvent<PircBotX> event = new MessageEvent<PircBotX>(mewtwo, channel, user, entry.getMessage());
                        try {
                            MewtwoMain.listener.onMessage(event);
                        } catch(Throwable t) {
                            MewtwoMain.mewtwoLogger.error("Error while triggering console event!", t);
                        }
                    }
                }
            }
        }, threadName + "-InputProcessingThread").start();
        MewtwoMain.mewtwoLogger.info("Successfully started input processing thread");

        try {
            mewtwo.startBot();
        } catch(Throwable t) {
            MewtwoMain.mewtwoLogger.error("Error while starting/running bot! This is most likely fatal and you should restart Mewtwo now", t);
        }
    }
}

@@ -1091,76 +1091,13 @@
 ead(
-new Runnable() %7B%0A            @Override%0A            public void run()
+() -%3E
  %7B%0A 
@@ -1107,20 +1107,16 @@
         
-  
-  
 while(tr
@@ -1117,28 +1117,24 @@
 ile(true) %7B%0A
-    
             
@@ -1195,36 +1195,32 @@
                 
-   
- 
 MewtwoMain.mewtw
@@ -1293,20 +1293,16 @@
         
-    
 InputWat
@@ -1377,20 +1377,16 @@
         
-   
- 
 Channel 
@@ -1474,20 +1474,16 @@
         
-    
 User use
@@ -1511,20 +1511,16 @@
 ewtwo);%0A
-    
         
@@ -1649,38 +1649,30 @@
             
-    
 try %7B%0A
-    
             
@@ -1737,28 +1737,24 @@
             
-    
 %7D catch(Thro
@@ -1756,36 +1756,32 @@
 (Throwable t) %7B%0A
-    
                 
@@ -1867,34 +1867,8 @@
 t);%0A
-                        %7D%0A
     

Use lambda instead of anonymous class for input processing thread