package de.st_ddt.crazygeo;

import java.util.HashMap;

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;

import de.st_ddt.crazygeo.region.RealRoom;
import de.st_ddt.crazygeo.worldedit.WorldEditBridge;
import de.st_ddt.crazyplugin.CrazyPlugin;
import de.st_ddt.crazyplugin.exceptions.CrazyCommandCircumstanceException;
import de.st_ddt.crazyplugin.exceptions.CrazyCommandExecutorException;
import de.st_ddt.crazyplugin.exceptions.CrazyException;
import de.st_ddt.crazyutil.poly.room.FuncRoom;
import de.st_ddt.crazyutil.poly.room.Room;

/**
 * @author Daniel
 * 
 */
public class CrazyGeo extends CrazyPlugin
{

	private static CrazyGeo plugin;
	protected final HashMap<Player, RealRoom<? extends Room>> geos = new HashMap<Player, RealRoom<? extends Room>>();
	protected final HashMap<String, Class<Room>> type = new HashMap<String, Class<Room>>();
	protected WorldEditBridge weBridge;

	public static CrazyGeo getPlugin()
	{
		return plugin;
	}

	@Override
	protected String getShortPluginName()
	{
		return "geo";
	}

	@Override
	public void onEnable()
	{
		plugin = this;
		if (Bukkit.getServer().getPluginManager().getPlugin("WorldEdit") != null)
			this.weBridge = WorldEditBridge.getWorldEditBridge();
		super.onEnable();
	}

	@Override
	public boolean command(final CommandSender sender, final String commandLabel, final String[] args) throws CrazyException
	{
		if (sender instanceof ConsoleCommandSender)
			throw new CrazyCommandExecutorException(false);
		return super.command(sender, commandLabel, args);
	}

	public boolean command(final Player player, final String commandLabel, final String[] args) throws CrazyException
	{
		return false;
	}

	public void commandInfo(final Player player, final String[] newArgs)
	{
		// RealRoom<? extends Room> region = getPlayerSelection(player);
		// region.getBasis();
		// region.getRoom();
		// Rotated?
	}

	@Override
	public boolean commandMain(final CommandSender sender, final String commandLabel, final String[] args) throws CrazyException
	{
		if (sender instanceof ConsoleCommandSender)
			throw new CrazyCommandExecutorException(false);
		return super.commandMain(sender, commandLabel, args);
	}

	public boolean commandMain(final Player player, final String commandLabel, final String[] args) throws CrazyException
	{
		// if (commandLabel.equalsIgnoreCase("selType"))
		// {
		// commandMainSelect(player, args);
		// return true;
		// }
		if (commandLabel.equalsIgnoreCase("we") || commandLabel.equalsIgnoreCase("wei") || commandLabel.equalsIgnoreCase("weimport"))
		{
			commandMainWEImport(player, args);
			return true;
		}
		if (commandLabel.equalsIgnoreCase("wee") || commandLabel.equalsIgnoreCase("weexport"))
		{
			commandMainWEExport(player, args);
			return true;
		}
		return false;
	}

	// private void commandMainSelect(Player player, String[] args)
	// {
	// }
	private void commandMainWEImport(final Player player, final String[] args) throws CrazyException
	{
		if (weBridge == null)
			throw new CrazyCommandCircumstanceException("when WorldEdit is enabled!");
		final RealRoom<FuncRoom> region = weBridge.getSemiSavePlayerSelection(player);
		geos.put(player, region);
	}

	/**
	 * Import a region from WorldEdit
	 * 
	 * @param player
	 *            The Player whos selection should be imported
	 * @throws CrazyException
	 */
	public void importWE(final Player player) throws CrazyException
	{
		if (weBridge == null)
			return;
		final RealRoom<FuncRoom> region = weBridge.getSemiSavePlayerSelection(player);
		geos.put(player, region);
	}

	private void commandMainWEExport(final Player player, final String[] args) throws CrazyException
	{
		if (weBridge == null)
			throw new CrazyCommandCircumstanceException("when WorldEdit is enabled!");
		final RealRoom<? extends Room> region = getPlayerSelection(player);
		weBridge.setSemiSavePlayerSelection(player, region);
	}

	/**
	 * Export a region to WorldEdit
	 * 
	 * @param player
	 *            The Player whos selection should be exported
	 * @throws CrazyException
	 */
	public void exportWE(final Player player) throws CrazyException
	{
		if (weBridge == null)
			return;
		final RealRoom<? extends Room> region = getPlayerSelection(player);
		weBridge.setSemiSavePlayerSelection(player, region);
	}

	public RealRoom<? extends Room> getPlayerSelection(final Player player)
	{
		return geos.get(player);
	}

	public void setPlayerSelection(final Player player, final RealRoom<? extends Room> room)
	{
		geos.put(player, room);
	}

	public void clearPlayerSelection(final Player player)
	{
		geos.remove(player);
	}
}

@@ -651,42 +651,8 @@
 %0D%0A%0D%0A
-/**%0D%0A * @author Daniel%0D%0A * %0D%0A */%0D%0A
 publ
@@ -746,38 +746,38 @@
 d final HashMap%3C
-Player
+String
 , RealRoom%3C? ext
@@ -807,22 +807,22 @@
 HashMap%3C
-Player
+String
 , RealRo
@@ -3300,32 +3300,56 @@
 %09geos.put(player
+.getName().toLowerCase()
 , region);%0D%0A%09%7D%0D%0A
@@ -3714,24 +3714,48 @@
 s.put(player
+.getName().toLowerCase()
 , region);%0D%0A
@@ -4598,28 +4598,52 @@
 s.get(player
+.getName().toLowerCase()
 );%0D%0A
-
 %09%7D%0D%0A%0D%0A%09publi
@@ -4748,16 +4748,40 @@
 t(player
+.getName().toLowerCase()
 , room);
@@ -4844,24 +4844,24 @@
 layer)%0D%0A%09%7B%0D%0A
-
 %09%09geos.remov
@@ -4864,23 +4864,47 @@
 emove(player
+.getName().toLowerCase()
 );%0D%0A%09%7D%0D%0A%7D%0D%0A

use player names to save region selections