package vg.civcraft.mc.bettershards.manager;

import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.bukkit.Bukkit;

import vg.civcraft.mc.bettershards.BetterShardsPlugin;
import vg.civcraft.mc.bettershards.events.PlayerEnsuredToTransitEvent;
import vg.civcraft.mc.bettershards.events.PlayerFailedToTransitEvent;

public class TransitManager {

	private Map<UUID, String> exitTransit;
	private Map<UUID, String> arrivalTransit;

	public TransitManager() {
		exitTransit = new ConcurrentHashMap<UUID, String>();
		arrivalTransit = new ConcurrentHashMap<UUID, String>();
	}

	/**
	 * Checks if a player is in any transit.
	 */
	public boolean isPlayerInTransit(UUID uuid) {
		return isPlayerInExitTransit(uuid) || isPlayerInArrivalTransit(uuid);
	}
	
	public boolean isPlayerInExitTransit(UUID uuid) {
		return exitTransit.containsKey(uuid);
	}
	
	public boolean isPlayerInArrivalTransit(UUID uuid) {
		return arrivalTransit.containsKey(uuid);
	}

	/**
	 * This adds a player to a list that can be checked to see if a player is in
	 * transit.
	 */
	public void addPlayerToExitTransit(final UUID uuid, final String server) {
		exitTransit.put(uuid, server);
		Bukkit.getScheduler().runTaskLater(BetterShardsPlugin.getInstance(), new Runnable() {

			@Override
			public void run() {
				if (isPlayerInExitTransit(uuid)) {
					BetterShardsPlugin.getInstance().info(
							uuid + " failed to transit to " + server + ", was removed by timeout");
					PlayerFailedToTransitEvent event = new PlayerFailedToTransitEvent(uuid, server);
					Bukkit.getPluginManager().callEvent(event);
					exitTransit.remove(uuid);
				}
			}

		}, 600); // 30 seconds timeout
	}
	
	public void addPlayerToArrivalTransit(final UUID uuid, final String server) {
		arrivalTransit.put(uuid, server);
		Bukkit.getScheduler().runTaskLater(BetterShardsPlugin.getInstance(), new Runnable() {

			@Override
			public void run() {
				if (isPlayerInArrivalTransit(uuid)) {
					BetterShardsPlugin.getInstance().info(
							uuid + " failed to transit from " + server + ", was removed by timeout");
					PlayerFailedToTransitEvent event = new PlayerFailedToTransitEvent(uuid, server);
					Bukkit.getPluginManager().callEvent(event);
					arrivalTransit.remove(uuid);
				}
			}

		}, 600); // 30 seconds timeout
	}

	/**
	 * Called by the MercuryListener when the target server of a transfer
	 * indicates that the player arrived
	 * 
	 * @param player UUID of the transferred player
	 */
	public void notifySuccessfullExit(UUID player) {
		if (isPlayerInExitTransit(player)) {
			PlayerEnsuredToTransitEvent e = new PlayerEnsuredToTransitEvent(player, exitTransit.get(player));
			Bukkit.getPluginManager().callEvent(e);
			exitTransit.remove(player);
		}
	}
	
	/**
	 * Called when a player's data is fully loaded
	 * 
	 * @param player UUID of the transferred player
	 */
	public void notifySuccessfullArrival(UUID player) {
		if (isPlayerInArrivalTransit(player)) {
			//TODO Add appropriate event here
			arrivalTransit.remove(player);
		}
	}
	
	
}

@@ -1161,24 +1161,128 @@
 g server) %7B%0A
+%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + uuid + %22 was added to exit transit for %22 + server);%0A
 %09%09exitTransi
@@ -1294,32 +1294,32 @@
 (uuid, server);%0A
-
 %09%09Bukkit.getSche
@@ -1753,32 +1753,154 @@
 ve(uuid);%0A%09%09%09%09%7D%0A
+%09%09%09%09else %7B%0A%09%09%09%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + uuid + %22 was removed from exit transit cleanly%22);%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%0A%09%09%7D, 600);
@@ -2006,16 +2006,123 @@
 rver) %7B%0A
+%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + uuid + %22 was added to arrival transit for %22 + server);%0A
 %09%09arriva
@@ -2608,32 +2608,157 @@
 ve(uuid);%0A%09%09%09%09%7D%0A
+%09%09%09%09else %7B%0A%09%09%09%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + uuid + %22 was removed from arrival transit cleanly%22);%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%0A%09%09%7D, 600);
@@ -3221,24 +3221,172 @@
 layer);%0A%09%09%7D%0A
+%09%09else %7B%0A%09%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + player + %22 was attempted to be removed from exit transit, but was already gone%22);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09/**%0A%09 
@@ -3622,16 +3622,16 @@
 nt here%0A
-
 %09%09%09arriv
@@ -3656,21 +3656,172 @@
 layer);%0A%09%09%7D%0A
+%09%09else %7B%0A%09%09%09BetterShardsPlugin.getInstance().info(%22Player %22 + player + %22 was attempted to be removed from arrival transit, but was already gone%22);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09%0A%7D%0A

Add logging