/
area/
classes/net/sourceforge/pain/logic/
classes/net/sourceforge/pain/logic/event/
classes/net/sourceforge/pain/logic/fn/util/
classes/net/sourceforge/pain/network/console/
classes/net/sourceforge/pain/plugin/
classes/net/sourceforge/pain/plugin/reset/
classes/net/sourceforge/pain/plugin/shutdown/
classes/net/sourceforge/pain/plugin/social/
classest/net/sourceforge/pain/db/data/
doc/
doc/paindb/resources/
src/net/sourceforge/pain/logic/
src/net/sourceforge/pain/logic/event/
src/net/sourceforge/pain/logic/fn/util/
src/net/sourceforge/pain/network/console/
src/net/sourceforge/pain/network/console/telnet/
src/net/sourceforge/pain/plugin/
src/net/sourceforge/pain/plugin/command/
src/net/sourceforge/pain/plugin/reset/
src/net/sourceforge/pain/plugin/shutdown/
src/net/sourceforge/pain/plugin/social/
src/net/sourceforge/pain/util/
tests/
tests/net/sourceforge/pain/db/data/
/*
 * Created by IntelliJ IDEA.
 * User: fmike
 * Date: 22.06.2003
 * Time: 2:11:51
 */
package net.sourceforge.pain.logic.event.console.command;

import net.sourceforge.pain.logic.event.console.*;
import net.sourceforge.pain.logic.fn.*;
import net.sourceforge.pain.*;
import net.sourceforge.pain.plugin.command.*;

import java.util.*;

public final class Help extends CommandHandler {

	public void processCommand() throws Exception {
		String commandName = commandParams;
		if (commandName == null || commandName.trim().length() == 0) {
			showHelp();
		} else {
			commandName = commandName.toLowerCase();
			CommandHandler commandHandler = ConsoleInputEvent.getCommandHandler(commandName, console);
			if (commandHandler == null || commandHandler.getClass() == ShowCommandTag.class) {
				MessageOutFn.outln(console, "Command not found:{c" + commandName + "{x");
				if (commandName.length() > 1) {
					showHint(commandName);
				}
			} else if (!commandHandler.isAccessible()) {
				MessageOutFn.outln(console, "Command not accessible:{c" + commandHandler.command.name + "{x");
			} else {
				MessageOutFn.outln(console, "HELP on command: {C" + commandHandler.command.name + "{x");
				commandHandler.showHelp();
				MessageOutFn.outln(console, "");
			}
		}
	}

	private void showHint(String commandName) {
		commandName = commandName.substring(0, 1);
		List commands = new ArrayList();
		for (Iterator it = ConsoleInputEvent.getCommandMapper().getCommands().iterator(); it.hasNext();) {
			TextCommand command = (TextCommand) it.next();
			if (!command.name.startsWith(commandName)) {
				continue;
			}
			if (command.commandClassName.endsWith("ShowCommandTag") || command.commandClassName.endsWith("SocialHandler")) {
				continue;
			}
			commands.add(command);
		}
		if (!commands.isEmpty()) {
			showCommandList(commandName, commands);
		}
	}

	private void showCommandList(String commandName, List commands) {
		MessageOutFn.outln(console, "Available commands (without socials) by mask:{c" + commandName + "*{x");
		for (Iterator it = commands.iterator(); it.hasNext();) {
			TextCommand textCommand = (TextCommand) it.next();
			MessageOutFn.outln(console, textCommand.name);
		}
	}

	public void showHelp() {
		MessageOutFn.outln(console, "This command shows a help for any console command.");
		MessageOutFn.outln(console, "Usage: help <command_name>");
		MessageOutFn.outln(console, "Available commands (without socials):");
		final CommandMapper commandMapper = ((CommandMapper) (Core.getPluginManager().getPlugin("command.CommandMapper")));
		ArrayList list = new ArrayList(200);
		for (Iterator it = commandMapper.getCommands().iterator(); it.hasNext();) {
			TextCommand command = (TextCommand) it.next();
			final String commandClassName = command.commandClassName;
			if (commandClassName.endsWith("ShowCommandTag")) {
				continue; // this is not command
			}
			if (commandClassName.endsWith("SocialHandler")) {
				continue; // too many socials to show
			}
//			if (rejectedCommands.contains(commandClassName)) {
//				continue; // command is rejected to user
//			}
			list.add(command.name);

		}
		Collections.sort(list);
		for (Iterator it = list.iterator(); it.hasNext();) {
			MessageOutFn.outln(console, (String) it.next());
		}
	}

}