package fr.Skydust.JdrBot;

import java.time.LocalDateTime;
import java.util.Timer;

import fr.Skydust.JdrBot.tasks.HeartbeatTask;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import fr.Skydust.JdrBot.bases.CommandManager.CommandsManager;

public class JdrBot {
	public static String Version = JdrBot.class.getPackage().getImplementationVersion();

	public static JDA jda;
	public static LocalDateTime basedate;

	public static boolean debugMode = false;

	public static void main(String[] args) {
		parseArgs(args);

		try {
			basedate = LocalDateTime.now();

			// Prod Bot
			String token = "MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM";
			// Test Bot
			//String token = "ODk0ODc0Nzk5Nzg3MTE0NTA2.YVwXGg.JfIsIIQHIXbVsjoGGv2SfejnT9s";

			jda = JDABuilder.createDefault(token)
					.setActivity(Activity.playing("un jeu de rôle"))
					.enableIntents(GatewayIntent.MESSAGE_CONTENT)
					.enableCache(CacheFlag.VOICE_STATE).build();

			jda.awaitReady();

			CommandsManager.registerCommands(jda, "!");

			jda.addEventListener(new JdrBotListener());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	private static void parseArgs(String[] args) {
		for (int i=0;i<args.length;i++) {
			String currentArgument = args[i].toLowerCase();

			switch (currentArgument) {
				case "--heartbeat" -> {
					if (i == args.length - 1) { // Check for an argument after heartbeat
						System.out.println("USAGE: --heartbeat [Command({ping})]");
						return; // Quits the program
					}
					//Example: "zabbix_sender -z localhost -s \"Lustricru\" -k discordBots.potatos.ping -o {ping}"
					String command = args[i + 1];

					//Skip next arg
					i++;

					new Timer().scheduleAtFixedRate(new HeartbeatTask(command), 1000, 60000);
					System.out.println("Registered Heartbeat Command: " + command);
				}
				case "--debug" -> {
					debugMode = true;
					System.out.println("Warning: the bot is in debug mode");
				}
			}
		}
	}
}
