/*
 * 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.
 */
package FunctionLayer;

import PresentationLayer.DiscordHandler;
import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.object.entity.User;
import discord4j.core.object.entity.channel.TextChannel;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


/**
 *
 * @author install1
 */
public class DoStuff {

    public static boolean occupied = false;

    public static boolean isOccupied() {
        return occupied;
    }

    public static void doStuff(MessageCreateEvent event, String usernameBot) {
        String username = null;
        try {
            username = event.getMessage().getAuthor().get().getUsername();
        } catch (java.util.NoSuchElementException e) {
            username = null;
        }
        if (username != null && !username.equals(usernameBot)) {
            occupied = true;
            TextChannel block = event.getMessage().getChannel().cast(TextChannel.class).block();
            String name = block.getCategory().block().getName();
            name = name.toLowerCase();
            String channelName = block.getName().toLowerCase();
            boolean channelpermissionsDenied = false;
            switch (name) {
                case "public area": {
                    break;
                }
                case "information area": {
                    break;
                }
                default: {
                    channelpermissionsDenied = true;
                    break;
                }
            }
            List<User> blockLast = event.getMessage().getUserMentions().buffer().blockLast();
            String content = event.getMessage().getContent();
            if (!channelpermissionsDenied) {
                if (blockLast != null)
                {
                    for (User user : blockLast) {
                        content = content.replace(user.getId().asString(), "");
                    }
                }
                MessageResponseHandler.getMessage(content);
            }
            boolean mentionedBot = false;
            if (blockLast != null){
                for (User user : blockLast)
                {
                    if (user.getUsername().equals(usernameBot))
                    {
                        mentionedBot = true;
                        break;
                    }
                }
            }
            if (mentionedBot || channelName.contains("general-autism")) {
                try {
                    String ResponseStr;
                    ResponseStr = MessageResponseHandler.selectReponseMessage(content, username);
                    if (!ResponseStr.isEmpty()) {
                        System.out.print("\nResponseStr3: " + ResponseStr + "\n");
                        event.getMessage().getChannel().block().createMessage(ResponseStr).block();
                    }
                } catch (CustomError ex) {
                    Logger.getLogger(DoStuff.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            new Thread(() -> {
                try {
                    Datahandler.instance.checkIfUpdateStrings();
                } catch (CustomError ex) {
                    Logger.getLogger(DiscordHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }).start();
            occupied = false;
        }
    }
}
