package com.alecgorge.minecraft.jsonapi.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;

public class CommandMethods {
	Server server;
	
	public CommandMethods(Server server) {
		this.server = server;
	}
	
	public Map<String, Map<String, Map<String, Object>>> listComplete() {
		Map<String, Map<String, Map<String, Object>>> allCmds = new HashMap<String, Map<String, Map<String, Object>>>();

		for (Plugin p : server.getPluginManager().getPlugins()) {
			final PluginDescriptionFile desc = p.getDescription();
			final Map<String, Map<String, Object>> cmds = desc.getCommands();

			allCmds.put(p.getDescription().getName(), cmds);
		}
		
		return allCmds;
	}
	
	public Map<String, Map<String, Object>> commandsForPluginName(String name) {
		Map<String, Map<String, Object>> allCmds = new HashMap<String, Map<String, Object>>();

		Plugin p = server.getPluginManager().getPlugin(name);
		if(p != null) {
			final PluginDescriptionFile desc = p.getDescription();
			allCmds = desc.getCommands();
		}
		
		return allCmds;
	}
	
	public List<String> listSimple() {
		List<String> allCmds = new ArrayList<String>();

		for (Plugin p : server.getPluginManager().getPlugins()) {
			final PluginDescriptionFile desc = p.getDescription();
			final Map<String, Map<String, Object>> cmds = desc.getCommands();
			
			allCmds.addAll(cmds.keySet());
		}
		
		return allCmds;
	}
	
	public Map<String, Object> commandForName(String name) {
		Map<String, Object> allCmds = new HashMap<String, Object>();

		for (Plugin p : server.getPluginManager().getPlugins()) {
			final PluginDescriptionFile desc = p.getDescription();
			final Map<String, Map<String, Object>> cmds = desc.getCommands();
			
			if(cmds.get(name) != null) {
				return cmds.get(name);
			}
		}
		
		return allCmds;

	}
}

@@ -739,16 +739,53 @@
 nds();%0A%0A
+%09%09%09if(desc.getCommands() != null) %7B%0A%09
 %09%09%09allCm
@@ -828,16 +828,126 @@
  cmds);%0A
+%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09allCmds.put(p.getDescription().getName(), new HashMap%3CString, Map%3CString,Object%3E%3E());%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09
@@ -1267,24 +1267,65 @@
 ption();%0A%09%09%09
+%0A%09%09%09if(desc.getCommands() != null) %7B%0A%09%09%09%09
 allCmds = de
@@ -1342,16 +1342,21 @@
 ands();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09

make plugin that don't have any commands in their plugin.yml have empty dictionaries, not null