package PresentationLayer;

import DataLayer.RunnerClient;
import DataLayer.ThreadClient;
import DataLayer.ThreadClientStoat;
import DataLayer.settings;
import FunctionLayer.Datahandler;
import FunctionLayer.PipelineJMWESingleton;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import discord4j.core.event.domain.lifecycle.ReadyEvent;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.Message;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.security.auth.login.LoginException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Executors;


/**
 * @author install1
 */
public class DiscordHandler extends ListenerAdapter {

    private static StanfordCoreNLP stanfordCoreNLP;
    private static Datahandler datahandler;
    private static StanfordCoreNLP stanfordCoreNLPSentiment;

    //TODO add python program that edits the java code. python program just adds test if statements on
    //variables until the tests pass
    public static void main(String[] args) throws LoginException {
        datahandler = new Datahandler();
        PipelineJMWESingleton.getINSTANCE();
        stanfordCoreNLP = datahandler.pipeLineSetUp();
        stanfordCoreNLPSentiment = datahandler.shiftReduceParserInitiate();

        System.out.println("FINISHED ALL ANNOTATIONS");
        String strF = datahandler.trimString("abcdef");
        datahandler.getResponseFutures(strF, stanfordCoreNLP, stanfordCoreNLPSentiment);
        Properties prop = new Properties();
        String fileName = "app.config";
        try (FileInputStream fis = new FileInputStream(fileName)) {
            prop.load(fis);
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
        String token = prop.getProperty("app.discordtoken");


        JDABuilder.createLight(token, GatewayIntent.GUILD_MESSAGES, GatewayIntent.MESSAGE_CONTENT, GatewayIntent.DIRECT_MESSAGES)
                .addEventListeners(new DiscordHandler())
                .setActivity(Activity.playing("Being the autism bot"))
                .build();

        Thread udpThread = new Thread(() -> {
            new ThreadClientStoat(datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
        }, "UDP-Socket-Thread");
        udpThread.start();

        new ThreadClient(datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment);
    }


    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        String content = event.getMessage().getContentRaw();
        String username = event.getMessage().getAuthor().getName();
        List<Member> mentionedMembers = event.getMessage().getMentions().getMembers();
        //List<Member> mentionedMembers = event.getMessage().getMentionedMembers();
        if (mentionedMembers != null) {
            for (Member member : mentionedMembers) {
                content = content.replace(member.getId(), "");
            }
        }
        if (username != null && !event.getAuthor().isBot() && !content.isEmpty()
                && event.getMessage().getCategory() != null) {
            String channelName = event.getMessage().getChannel().getName().toLowerCase();
            boolean channelpermissionsDenied = false;
            if (channelName.contains("suggestion-box")) {
                channelpermissionsDenied = true;
            }
            String categoryName = event.getMessage().getCategory().getName().toLowerCase();
            switch (categoryName) {
                case "public area":
                case "information area": {
                    break;
                }
                default: {
                    channelpermissionsDenied = true;
                    break;
                }
            }
            if (!channelpermissionsDenied) {
                boolean mentionedBot = false;
                if (mentionedMembers != null) {
                    for (Member member : mentionedMembers) {
                        if (member.getEffectiveName().equals(event.getJDA().getSelfUser().getName())) {
                            mentionedBot = true;
                            break;
                        }
                    }
                }
                final String contentF = content;
                try {
                    new RunnerClient(contentF, mentionedBot, channelName, datahandler, stanfordCoreNLP, stanfordCoreNLPSentiment,
                            event, username);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.exit(1);
                }
            }
        }
    }
}
