package me.minidigger.voxelgameslib.bukkit.command;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Singleton;

import me.minidigger.voxelgameslib.api.command.CommandHandler;
import me.minidigger.voxelgameslib.api.command.CommandInfo;
import me.minidigger.voxelgameslib.bukkit.VoxelGamesLibBukkit;

import org.bukkit.command.CommandMap;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.SimplePluginManager;

import lombok.extern.java.Log;

@Log
@Singleton
public class BukkitCommandHandler extends CommandHandler {
    
    @Inject
    private VoxelGamesLibBukkit plugin;
    
    private CommandMap bukkitCommandMap;
    private Map<String, org.bukkit.command.Command> knownCommands;
    
    @Override
    public void start() {
        super.start();
        
        // hook bukkits shit
        if (plugin.getServer().getPluginManager() instanceof SimplePluginManager) {
            SimplePluginManager manager = (SimplePluginManager) plugin.getServer().getPluginManager();
            try {
                Field field = SimplePluginManager.class.getDeclaredField("commandMap");
                field.setAccessible(true);
                bukkitCommandMap = (CommandMap) field.get(manager);
            } catch (@Nonnull IllegalAccessException | NoSuchFieldException e) {
                log.severe("Could not get commandMap from the SimplePluginManager, can't register any commands!");
                e.printStackTrace();
                return;
            }
            
            try {
                Field knownCommandField = SimpleCommandMap.class.getDeclaredField("knownCommands");
                knownCommandField.setAccessible(true);
                Object obj = knownCommandField.get(bukkitCommandMap);
                if (obj.getClass().isAssignableFrom(Map.class)) {
                    knownCommands = (Map<String, org.bukkit.command.Command>) obj;
                } else {
                    log.severe("Could not get knownCommands from the SimpleCommandMap (returned unexpected object), can't unregister any commands!");
                }
            } catch (@Nonnull NoSuchFieldException | IllegalAccessException e) {
                log.warning("Could get knownCommands from the SimpleCommandMap, can't unregister any commands!");
                e.printStackTrace();
            }
            
        } else {
            // should never occur
            throw new IllegalArgumentException("Specified plugin has no SimplePluginManager?!");
        }
    }
    
    @Override
    protected void registerCommand(@Nonnull String commandLabel, @Nonnull CommandInfo info, @Nonnull Method method, @Nonnull Object object) {
        super.registerCommand(commandLabel, info, method, object);
        
        //TODO register command
    }
    
    @Override
    protected void unregisterCommand(@Nonnull String commandLabel, @Nonnull CommandInfo info, @Nonnull Method method, @Nonnull Object object, boolean remove) {
        super.unregisterCommand(commandLabel, info, method, object, remove);
        
        //TODO unregister command
    }
}

@@ -46,16 +46,236 @@
 mmand;%0A%0A
+import org.bukkit.command.CommandMap;%0Aimport org.bukkit.command.CommandSender;%0Aimport org.bukkit.command.SimpleCommandMap;%0Aimport org.bukkit.command.defaults.BukkitCommand;%0Aimport org.bukkit.plugin.SimplePluginManager;%0A%0A
 import j
@@ -331,16 +331,42 @@
 Method;%0A
+import java.util.HashMap;%0A
 import j
@@ -375,24 +375,25 @@
 a.util.Map;%0A
+%0A
 import javax
@@ -473,16 +473,47 @@
 leton;%0A%0A
+import lombok.extern.java.Log;%0A
 import m
@@ -695,169 +695,8 @@
 t;%0A%0A
-import org.bukkit.command.CommandMap;%0Aimport org.bukkit.command.SimpleCommandMap;%0Aimport org.bukkit.plugin.SimplePluginManager;%0A%0Aimport lombok.extern.java.Log;%0A%0A
 @Log
@@ -1973,16 +1973,78 @@
 ndMap);%0A
+                System.out.println(obj.getClass().getName());%0A
         
@@ -2087,16 +2087,20 @@
 bleFrom(
+Hash
 Map.clas
@@ -2187,16 +2187,447 @@
 d%3E) obj;
+%0A%0A                    // DEBUG%0A                    knownCommands.put(%22testcommandtest%22, new BukkitCommand(%22testcommandtest%22) %7B%0A                        @Override%0A                        public boolean execute(CommandSender commandSender, String s, String%5B%5D strings) %7B%0A                            commandSender.sendMessage(%22testcommandtest%22);%0A                            return true;%0A                        %7D%0A                    %7D);
 %0A       
@@ -3463,24 +3463,16 @@
 bject);%0A
-        
 %0A       

fix getting of the knownCommands map