package eu.thog92.dramagen;


import com.sun.net.httpserver.HttpServer;
import eu.thog92.generator.api.BotGenerator;
import eu.thog92.generator.api.Dictionary;
import eu.thog92.generator.api.annotations.Module;
import eu.thog92.generator.api.annotations.SubscribeEvent;
import eu.thog92.generator.api.config.Configuration;
import eu.thog92.generator.api.events.HttpStartEvent;
import eu.thog92.generator.api.events.InitEvent;
import eu.thog92.generator.api.tasks.GeneratorTask;
import eu.thog92.generator.api.tasks.ScheduledTask;
import eu.thog92.generator.twitter.TwitterConfiguration;
import eu.thog92.generator.twitter.TwitterModule;
import eu.thog92.generator.twitter.TwitterTask;

import java.io.File;
import java.io.IOException;

@Module(name = "Drama", version = "1.1", dependencies = "after:twitter;")
public class DramaGenerator
{
    private BotGenerator generator;
    private GeneratorTask generatorTask;
    private ScheduledTask twitterTask;
    private HttpServer server;
    private Dictionary dictionary;

    @SubscribeEvent
    public void init(InitEvent event)
    {
        File moduleDir = new File(event.getConfigDir(), "Drama");
        moduleDir.mkdirs();
        generator = event.getBotGenerator();
        Configuration globalSettings = new Configuration(event.getConfigDir(), "Drama", "drama");
        DramaConfiguration dramaConfiguration = globalSettings.readFromFile(DramaConfiguration.class);

        Configuration twitterSettings = new Configuration(event.getConfigDir(), "Drama", "twitter");

        if (dramaConfiguration == null)
        {
            dramaConfiguration = new DramaConfiguration(3600, 8080);
            globalSettings.saveToDisk(dramaConfiguration);
        }

        TwitterConfiguration twitterConfiguration = twitterSettings.readFromFile(TwitterConfiguration.class);
        if (twitterConfiguration == null)
        {
            System.err.println("[Drama Generator] A new Twitter config have been created! Complete it before restart.");
            twitterSettings.saveToDisk(new TwitterConfiguration("", "", "", "", ""));
            System.exit(666);
        }

        this.dictionary = new Dictionary();
        this.dictionary.setDir(new File(moduleDir, "dictionary"));
        try
        {
            this.dictionary.loadCombinations();
            this.dictionary.loadBlackList();
        } catch (IOException e)
        {
            e.printStackTrace();
            System.exit(49);
        }

        this.generatorTask = new GeneratorTask(this.dictionary);
        this.twitterTask = new TwitterTask(TwitterModule.getInstance().createTwitterInstance(twitterConfiguration.debug, twitterConfiguration.consumerKey, twitterConfiguration.consumerSecret, twitterConfiguration.accessToken, twitterConfiguration.accessTokenSecret), this.generatorTask, twitterConfiguration.sendTweetOnStartup, null);
        this.twitterTask.setDelay(dramaConfiguration.tweetDelay);

        generator.getTasksManager().scheduleTask(this.twitterTask);
        try
        {
            this.server = generator.getHttpManager().createHTTPServer(dramaConfiguration.httpPort);
        } catch (IOException e)
        {
            e.printStackTrace();
        }

        System.out.println("Drama is ready.");

    }

    @SubscribeEvent
    public void onHTTPServerInit(HttpStartEvent event)
    {
        System.out.println("Loading HTTP Server...");
        HttpServer server = event.getServer();
        server.createContext("/drama", new DramaHandler(generatorTask, false));
        server.createContext("/api/drama", new DramaHandler(generatorTask, true));
    }
}

@@ -411,32 +411,244 @@
 ents.InitEvent;%0A
+import eu.thog92.generator.api.events.irc.IRCChannelMessage;%0Aimport eu.thog92.generator.api.irc.IRCClient;%0Aimport eu.thog92.generator.api.events.irc.IRCReady;%0Aimport eu.thog92.generator.api.irc.IRCConfiguration;%0A
 import eu.thog92
@@ -679,24 +679,24 @@
 eratorTask;%0A
-
 import eu.th
@@ -1226,16 +1226,63 @@
 tionary;
+%0A    private IRCConfiguration ircConfiguration;
 %0A%0A    @S
@@ -2373,32 +2373,536 @@
 66);%0A        %7D%0A%0A
+        Configuration ircSettings = new Configuration(event.getConfigDir(), %22Drama%22, %22irc%22);%0A        IRCConfiguration ircConfiguration = ircSettings.readFromFile(IRCConfiguration.class);%0A%0A        if (ircConfiguration == null)%0A        %7B%0A            System.err.println(%22%5BDrama Generator%5D A new IRC config have been created! Complete it before restart.%22);%0A            ircSettings.saveToDisk(new IRCConfiguration(%22%22, 6666, %22DramaGenerator%22, new String%5B%5D %7B%22#WAMM%22%7D));%0A            System.exit(333);%0A        %7D%0A%0A
         this.dic
@@ -3883,16 +3883,226 @@
 pPort);%0A
+            IRCClient ircClient = IRCClient.createIRCClient(ircConfiguration.hostname, ircConfiguration.port, ircConfiguration.username).addChannels(ircConfiguration.channels);%0A            ircClient.connect();%0A
         
@@ -4223,24 +4223,26 @@
  ready.%22);%0A%0A
+%0A%0A
     %7D%0A%0A    @
@@ -4494,24 +4494,24 @@
 k, false));%0A
-
         serv
@@ -4586,11 +4586,349 @@
 );%0A    %7D
+%0A%0A    @SubscribeEvent%0A    public void onReady(IRCReady event)%0A    %7B%0A%0A    %7D%0A%0A    @SubscribeEvent%0A    public void onMesssage(IRCChannelMessage event)%0A    %7B%0A        if(event.getMessage().contains(%22.drama%22))%0A        %7B%0A            event.getIRCClient().sendToChat(event.getChannel(), this.generatorTask.generateSentence(false));%0A        %7D%0A    %7D
 %0A%7D%0A

Add IRC Support