package me.lenajenichen.voicebot.main;

import me.lenajenichen.voicebot.CommandManager;
import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.api.CreateDescription;
import me.lenajenichen.voicebot.listener.*;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.security.auth.login.LoginException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BotMain {

    public static BotMain INSTATNCE;

    public JDA shardManager;
    public JDABuilder jdaBuilder;

    private CommandManager cmdMan;

    public static void main(String[] args) {
        try {
            new BotMain();
        } catch (LoginException | IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    public BotMain() throws LoginException, IllegalArgumentException {

        LoggerInit.initLogger();

        INSTATNCE = this;

        MySQL.createTable();

        jdaBuilder = jdaBuilder.createDefault("ODQzNzQxNDgwNzU3NjI0ODMy.YKIRfA.mNJkqZd015aNAJhzZ4qzphhfRQg");
        jdaBuilder.setStatus(OnlineStatus.ONLINE);
        jdaBuilder.setActivity(Activity.watching("nach uwu's"));
        jdaBuilder.enableIntents(GatewayIntent.GUILD_MEMBERS);

        this.cmdMan = new CommandManager();

        System.out.println("1");
        jdaBuilder.addEventListeners(new CommandListener());
        System.out.println("1");
        jdaBuilder.addEventListeners(new VoiceListener());
        System.out.println("1");
        jdaBuilder.addEventListeners(new MessageDelete());
        System.out.println("1");
        jdaBuilder.addEventListeners(new MessageEvent());
        System.out.println("1");
        jdaBuilder.addEventListeners(new AvatarUpdate());
        System.out.println("1");
        jdaBuilder.addEventListeners(new CaptchaPrivateMessageEvent());
        System.out.println("1");
        jdaBuilder.addEventListeners(new GuildMemberJoin());
        System.out.println("1");
        jdaBuilder.addEventListeners(new NameUpdate());

        shardManager = jdaBuilder.build();

        shutdown();

    }

    public void shutdown() {
        new Thread(() -> {
            String line = "";
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                while ((line = reader.readLine()) != null) {
                    if(line.equalsIgnoreCase("exit")) {
                        if(shardManager != null) {
                            jdaBuilder.setStatus(OnlineStatus.OFFLINE);
                            shardManager.shutdown();
                        }
                        reader.close();
                        break;
                    } else {
                        System.out.println("Use 'exit' to shutdown the bot");
                    }
                }
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }).start();
    }

    public CommandManager getCmdMan() {
        return cmdMan;
    }

    public static void sendErrorMessage(TextChannel channel) {
        CreateDescription.createDescriptionMessage("Es ist ein Fehler aufgetreten! Kontaktieren Sie bitte den Support.", "#FF0000", channel);
    }


}
