package me.despawningbone.discordbot.command.info;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

import me.despawningbone.discordbot.command.Command;
import me.despawningbone.discordbot.command.CommandResult;
import me.despawningbone.discordbot.command.CommandResult.CommandResultType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;

public class Ping extends Command {
	public Ping() {
		this.desc = "Check the round trip latency between the server and the bot!";
		this.usage = "[debug]";
		this.remarks = Arrays.asList("Putting the debug parameter will output the WebSocket latency too.");
	}

	@Override
	public CommandResult execute(TextChannel channel, User author, Message msg, String[] args) {   //DONE USE RESTACTION INSTEAD
		OffsetDateTime timesent = OffsetDateTime.now();
		if (args.length > 0 && args[0].equals("debug")) {
			channel.sendMessage(":ping_pong: Pong! Response latency: ").queue(m -> {
				long ms = Math.abs(timesent.until(OffsetDateTime.now(), ChronoUnit.MILLIS));
				// System.out.println(String.valueOf(ms));
				channel.editMessageById(m.getId(), ":ping_pong: Pong! Response latency: " + ms + "ms" + (ms < 250 ? "\nNot bad! :D" : ms > 500 ? "\nWow. God damn Internet issues!" : "")).queue();
				channel.sendMessage("WebSocket response latency: " + channel.getJDA().getGatewayPing() + "ms").queue();
			});
		} else {
			channel.getJDA().getRestPing().queue(ms -> channel.sendMessage(":ping_pong: Pong! Response latency: " + ms + "ms" + (ms < 250 ? "\nNot bad! :D" : ms > 500 ? "\nWow. God damn Internet issues!" : "")).queue());
		}

		return new CommandResult(CommandResultType.SUCCESS);
	}
}
