package net.craftstars.general.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import net.craftstars.general.General;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.config.Configuration;

public class AliasHandler {
	public static boolean setAliases = false;
	private static SimpleCommandMap commandMap = null;
	private static Method register = null;
	public static String[] compassAliases;
	public static String[] posAliases;
	
	public static void setup() {
		if(setAliases) return;
		if(commandMap == null && !getCommandMap()) return;
		if(register == null && !getRegisterMethod()) return;
		Configuration config = General.plugin.config;
		PluginDescriptionFile plug = General.plugin.getDescription();
		try {
			@SuppressWarnings({"unchecked", "rawtypes"})
			Map<String,Map> commands = (Map<String, Map>) plug.getCommands();
			for(String key : commands.keySet()) {
				if(!key.equals("general")) {
					String[] split = key.split(".");
					String cat = split[0], cmd = split[1];
					Command generalCommand = General.plugin.getCommand(key);
					register(cmd, generalCommand);
					for(String alias : config.getStringList("aliases." + key, null))
						register(alias, generalCommand);
				} else {
					
				}
			}
			Command posCommand = General.plugin.getCommand("info.getpos");
			// Compass
			List<String> ls = config.getStringList("aliases.info.compass", null);
			compassAliases = new String[ls.size() + 1];
			compassAliases[0] = "compass";
			register("compass", posCommand);
			for(int i = 1; i <= ls.size(); i++) {
				compassAliases[i] = ls.get(i-1);
				register(compassAliases[i], posCommand);
			}
			// Where
			ls = config.getStringList("aliases.info.where", null);
			posAliases = new String[ls.size() + 1];
			posAliases[0] = "where";
			register("where", posCommand);
			for(int i = 1; i <= ls.size(); i++) {
				posAliases[i] = ls.get(i-1);
				register(posAliases[i], posCommand);
			}
		} catch(NullPointerException e) {
			return;
		} catch(ClassCastException e) {
			General.logger.error("Commands are of wrong type!",e);
		}
	}
	
	public static boolean register(String label, Command command) {
		try {
			boolean success = (Boolean) register.invoke(commandMap, label, "General.dynalias", command, true);
			if(!success)
				General.logger.info("Command alias " + label + 
					" was not registered because another plugin claimed it.");
			return success;
		} catch(IllegalArgumentException e) {
			General.logger.warn(e.getMessage());
		} catch(IllegalAccessException e) {
			General.logger.warn(e.getMessage());
		} catch(InvocationTargetException e) {
			General.logger.warn(e.getMessage());
		}
		return false;
	}

	private static boolean getCommandMap() {
		CraftServer cs = (CraftServer) Bukkit.getServer();
		Field cm;
		try {
			cm = CraftServer.class.getDeclaredField("commandMap");
		} catch(SecurityException e) {
			General.logger.warn(e.getMessage());
			return false;
		} catch(NoSuchFieldException e) {
			General.logger.warn(e.getMessage());
			return false;
		}
		cm.setAccessible(true);
		try {
			commandMap = (SimpleCommandMap) cm.get(cs);
		} catch(IllegalArgumentException e) {
			General.logger.warn(e.getMessage());
			return false;
		} catch(IllegalAccessException e) {
			General.logger.warn(e.getMessage());
			return false;
		}
		return true;
	}
	
	private static boolean getRegisterMethod() {
		if(commandMap == null) return false;
		try {
			register = SimpleCommandMap.class.getDeclaredMethod("register", String.class, String.class, Command.class, boolean.class);
		} catch(SecurityException e) {
			General.logger.warn(e.getMessage());
			return false;
		} catch(NoSuchMethodException e) {
			General.logger.warn(e.getMessage());
			return false;
		}
		register.setAccessible(true);
		return true;
	}
}

@@ -1166,186 +1166,150 @@
 %09%09%09%09
-if(!key.equals(%22general%22)) %7B%0A%09%09%09%09%09String%5B%5D split = key.split
+Command generalCommand = General.plugin.getCommand(key);%0A%09%09%09%09if(key.contains
 (%22.%22)
-;
+)
 %0A%09%09%09%09%09
-String cat = split%5B0%5D, cmd = split%5B1%5D;%0A%09%09%09%09%09Command generalCommand = General.plugin.getCommand(key
+register(key.split(%22.%22)%5B1%5D, generalCommand
 );%0A%09%09%09%09
-%09
+else 
 regi
@@ -1317,11 +1317,11 @@
 ter(
-cmd
+key
 , ge
@@ -1339,17 +1339,16 @@
 d);%0A%09%09%09%09
-%09
 for(Stri
@@ -1405,25 +1405,24 @@
 null))%0A%09%09%09%09%09
-%09
 register(ali
@@ -1446,33 +1446,8 @@
 d);%0A
-%09%09%09%09%7D else %7B%0A%09%09%09%09%09%0A%09%09%09%09%7D%0A
 %09%09%09%7D

Fix index out of bounds bug in the alias handler