package io.github.hsyyid.spongeessentialcmds.cmdexecutors;

import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.option.OptionSubject;

import io.github.hsyyid.spongeessentialcmds.SpongeEssentialCmds;
import io.github.hsyyid.spongeessentialcmds.utils.Utils;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.util.command.args.CommandContext;
import org.spongepowered.api.util.command.spec.CommandExecutor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Optional;
import java.util.TimeZone;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class WhoisExecutor implements CommandExecutor
{
	public CommandResult execute(CommandSource src, CommandContext ctx) throws CommandException
	{
		Optional<Player> optPlayer = ctx.<Player> getOne("player");
		Optional<String> optPlayerName = ctx.<String> getOne("player name");

		if (optPlayer.isPresent())
		{
			Player player = optPlayer.get();
			src.sendMessage(Texts.of(TextColors.GOLD, "Real Name: ", TextColors.GRAY, player.getName()));
			src.sendMessage(Texts.of(TextColors.GOLD, "UUID: ", TextColors.GRAY, player.getUniqueId().toString()));
			if (SpongeEssentialCmds.game.getServer().getPlayer(player.getUniqueId()).isPresent())
				src.sendMessage(Texts.of(TextColors.GOLD, "Current Ontime: ", TextColors.GRAY, getCurrentOnTime(player.getUniqueId())));
			else
				src.sendMessage(Texts.of(TextColors.GOLD, "Last Time Online: ", TextColors.GRAY, Utils.getLastTimePlayerJoined(player.getUniqueId())));
			src.sendMessage(Texts.of(TextColors.GOLD, "Current World: ", TextColors.GRAY, player.getWorld().getName()));
		}
		else if (optPlayerName.isPresent())
		{
			Optional<Player> optionalPlayer = SpongeEssentialCmds.game.getServer().getPlayer(optPlayerName.get());

			if (optionalPlayer.isPresent())
			{
				Player player = optionalPlayer.get();
				src.sendMessage(Texts.of(TextColors.GOLD, "Real Name: ", TextColors.GRAY, player.getName()));
				src.sendMessage(Texts.of(TextColors.GOLD, "UUID: ", TextColors.GRAY, player.getUniqueId().toString()));
				if (SpongeEssentialCmds.game.getServer().getPlayer(player.getUniqueId()).isPresent())
					src.sendMessage(Texts.of(TextColors.GOLD, "Current Ontime: ", TextColors.GRAY, getCurrentOnTime(player.getUniqueId())));
				else
					src.sendMessage(Texts.of(TextColors.GOLD, "Last Time Online: ", TextColors.GRAY, Utils.getLastTimePlayerJoined(player.getUniqueId())));
				src.sendMessage(Texts.of(TextColors.GOLD, "Current World: ", TextColors.GRAY, player.getWorld().getName()));
			}
			else
			{
				Player foundPlayer = null;
				
				for (Player player : SpongeEssentialCmds.game.getServer().getOnlinePlayers())
				{
					Subject subject = player.getContainingCollection().get(player.getIdentifier());

					if (subject instanceof OptionSubject)
					{
						OptionSubject optionSubject = (OptionSubject) subject;
						if (optionSubject.getOption("nick").isPresent() && optionSubject.getOption("nick").get().equals(optPlayerName))
						{
							foundPlayer = player;
							break;
						}
					}
					else
					{
						continue;
					}
				}
				
				if(foundPlayer == null)
				{
					src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "Player not found."));
					return CommandResult.success();
				}
				else
				{
					Player player = foundPlayer;
					src.sendMessage(Texts.of(TextColors.GOLD, "Real Name: ", TextColors.GRAY, player.getName()));
					src.sendMessage(Texts.of(TextColors.GOLD, "UUID: ", TextColors.GRAY, player.getUniqueId().toString()));
					if (SpongeEssentialCmds.game.getServer().getPlayer(player.getUniqueId()).isPresent())
						src.sendMessage(Texts.of(TextColors.GOLD, "Current Ontime: ", TextColors.GRAY, getCurrentOnTime(player.getUniqueId())));
					else
						src.sendMessage(Texts.of(TextColors.GOLD, "Last Time Online: ", TextColors.GRAY, Utils.getLastTimePlayerJoined(player.getUniqueId())));
					src.sendMessage(Texts.of(TextColors.GOLD, "Current World: ", TextColors.GRAY, player.getWorld().getName()));
				}
			}
		}
		else
		{
			src.sendMessage(Texts.of(TextColors.DARK_RED, "Error! ", TextColors.RED, "You did not specify any arguments."));
		}

		return CommandResult.success();
	}

	public String getCurrentOnTime(UUID uuid)
	{
		String timeJoined = Utils.getLastTimePlayerJoined(uuid);

		if (!timeJoined.equals(""))
		{
			try
			{
				SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy HH:mm:ss");
				format.setTimeZone(TimeZone.getTimeZone("GMT"));
				Date date = format.parse(timeJoined);
				Calendar cal = Calendar.getInstance();
				Date currentDate = cal.getTime();
				long duration = currentDate.getTime() - date.getTime();
				long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
				long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
				long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);

				return ("In Hours: | " + diffInHours + " | In Minutes: | " + diffInMinutes + " | In Seconds: | " + diffInSeconds + " |");
			}
			catch (ParseException e)
			{
				return "NaN";
			}
		}
		else
		{
			return "NaN";
		}
	}
}

@@ -2993,20 +2993,16 @@
 = null;%0A
-%09%09%09%09
 %0A%09%09%09%09for
@@ -3279,16 +3279,23 @@
 ubject;%0A
+%09%09%09%09%09%09%0A
 %09%09%09%09%09%09if
@@ -3343,12 +3343,29 @@
 nt()
- && 
+)%0A%09%09%09%09%09%09%7B%0A%09%09%09%09%09%09%09if (
 opti
@@ -3418,16 +3418,22 @@
 ayerName
+.get()
 ))%0A%09%09%09%09%09
@@ -3429,26 +3429,28 @@
 t()))%0A%09%09%09%09%09%09
+%09
 %7B%0A
+%09
 %09%09%09%09%09%09%09found
@@ -3477,15 +3477,25 @@
 %09%09%09%09
+%09
 break;%0A
+%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -3555,19 +3555,16 @@
 %09%09%7D%0A
-%09%09%09%09
 %0A%09%09%09%09if
+ 
 (fou

clean up