package co.phoenixlab.discord;

import com.google.gson.Gson;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sx.blah.discord.DiscordClient;

import java.io.IOException;
import java.io.Reader;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class VahrhedralBot implements Runnable {

    public static final Logger LOGGER = LoggerFactory.getLogger("VahrhedralBot");

    public static void main(String[] args) {
        LOGGER.info("Starting Vahrhedral bot");
        try {
            VahrhedralBot bot = new VahrhedralBot();
            bot.run();
        } catch (Exception e) {
            LOGGER.error("Fatal error while starting bot", e);
        }
    }

    private DiscordClient discord;
    private Configuration config;

    public VahrhedralBot() {
        discord = DiscordClient.get();
    }

    @Override
    public void run() {
        //  TODO
        //  Load Config
        try {
            config = loadConfiguration(Paths.get("config/config.json"));
        } catch (IOException e) {
            LOGGER.error("Unable to load configuration", e);
            return;
        }
        LOGGER.info("Logging in using {}", config.getEmail());
        try {
            discord.login(config.getEmail(), config.getPassword());
        } catch (IOException | ParseException | URISyntaxException e) {
            LOGGER.error("Unable to log in", e);
            return;
        }
        if (!discord.isReady()) {
            LOGGER.error("Discord client is not ready");
            return;
        }
    }

    private Configuration loadConfiguration(Path path) throws IOException {
        Gson configGson = new Gson();
        try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
            return configGson.fromJson(reader, Configuration.class);
        }
    }



}

@@ -194,16 +194,261 @@
 dClient;
+%0Aimport sx.blah.discord.handle.IDispatcher;%0Aimport sx.blah.discord.handle.IListener;%0Aimport sx.blah.discord.handle.impl.events.MessageReceivedEvent;%0Aimport sx.blah.discord.handle.impl.events.ReadyEvent;%0Aimport sx.blah.discord.handle.obj.Message;
 %0A%0Aimport
@@ -1510,32 +1510,113 @@
 turn;%0A        %7D%0A
+        //  Register our event listeners first%0A        registerEventListeners();%0A
         LOGGER.i
@@ -2309,12 +2309,789 @@
 %0A    %7D%0A%0A
+    private void registerEventListeners() %7B%0A        IDispatcher dispatcher = discord.getDispatcher();%0A        dispatcher.registerListener((IListener%3CReadyEvent%3E)this::onReadyEvent);%0A        dispatcher.registerListener((IListener%3CMessageReceivedEvent%3E)this::onMessageRecievedEvent);%0A    %7D%0A%0A    private void onReadyEvent(ReadyEvent event) %7B%0A        LOGGER.info(%22Successfully connected as %7B%7D%22, discord.getOurUser().getName());%0A    %7D%0A%0A    private void onMessageRecievedEvent(MessageReceivedEvent event) %7B%0A        Message msg = event.getMessage();%0A        LOGGER.info(%22Message from %7B%7D #%7B%7D %7B%7D: %7B%7D%22,%0A                msg.getChannel().getParent().getName(),%0A                msg.getChannel().getName(),%0A                msg.getAuthor().getName(),%0A                msg.getContent());%0A    %7D%0A
 %0A%0A%7D%0A

Add basic event handling