/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 
    ps ax | grep EventNotfierDiscordBot-1.0
    kill $pid (number)

nohup screen -d -m -S nonroot java -Xmx6048M -jar  /home/javatests/ArtificialAutism-1.0.jar
nohup screen -d -m -S nonroot java -Xmx6800M -jar  /home/javatests/ArtificialAutism-1.0.jar

screen -ls (number1)
screen -X -S (number1) quit
 */
package PresentationLayer;

import FunctionLayer.Datahandler;
import FunctionLayer.DoStuff;
import FunctionLayer.PipelineJMWESingleton;
import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import DataLayer.settings;
import discord4j.common.util.Snowflake;
import discord4j.core.event.domain.message.MessageCreateEvent;
import java.math.BigInteger;


/**
 *
 * @author install1
 */
public class DiscordHandler {
    public static void main(String[] args) {
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "15");
        try {
            Datahandler.instance.initiateMYSQL();
            //nohup screen -d -m -S nonroot java -Xmx6900M -jar  /home/javatests/ArtificialAutism-1.0.jar
            //uncomment db fetch when ready, just keep the comment for future reference
            System.out.println("finished initiating MYSQL");
        } catch (SQLException | IOException ex) {
            Logger.getLogger(DiscordHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        PipelineJMWESingleton.getINSTANCE();
        Datahandler.instance.instantiateAnnotationMapJMWE();
        Datahandler.instance.shiftReduceParserInitiate();
        Datahandler.instance.instantiateAnnotationMap();
        System.out.println("FINISHED ALL ANNOTATIONS");
        Datahandler.instance.addHLstatsMessages();
        Datahandler.instance.updateStringCache();
        //String token = "NTI5NzAxNTk5NjAyMjc4NDAx.Dw0vDg.7-aMjVWdQMYPl8qVNyvTCPS5F_A";
        String token = new settings().getDiscordToken();
        final DiscordClient client = DiscordClient.create(token);
        final GatewayDiscordClient gateway = client.login().block();
        String usernameBot = gateway.getSelf().block().getUsername();
        new Thread(() -> {
            Datahandler.instance.update_autismo_socket_msg();
        }).start();
        gateway.on(MessageCreateEvent.class).subscribe(event -> {
            if (!FunctionLayer.DoStuff.isOccupied()) {
                FunctionLayer.DoStuff.doStuff(event, usernameBot);
            }
        });
        gateway.onDisconnect().block();
    }
}
