/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.tinylib.logic.event.console.command;

import net.sourceforge.pain.tinylib.data.type.*;
import net.sourceforge.pain.tinylib.logic.event.console.*;
import net.sourceforge.pain.tinylib.logic.fn.*;
import net.sourceforge.pain.tinylib.logic.fn.util.*;
import net.sourceforge.pain.tinylib.plugin.command.*;

import java.util.*;

public final class Grant extends GrantedCommand {

    public void processCommand() throws Exception {
        String text = commandParams == null ? "" : commandParams.trim();
        int spaceIndex = text.indexOf(' ');
        if (spaceIndex < 1) {
            MessageOutFn.outln(console, "ERROR:Grant whom what?");
            return;
        }
        String playerName = text.substring(0, spaceIndex);
        final String commandName = text.substring(spaceIndex).trim();
        if (text.length() == 0) {
            MessageOutFn.out(console, "ERROR:Grant what?");
            return;
        }
        Player target = ("self".equals(playerName)) ? player : GlobalFindFn.findPlayerByName(playerName);
        if (target == null) {
            MessageOutFn.outln(console, "No player found with name {c" + Utils.formatName(playerName) + "{x");
            return;
        }

        final boolean isGrant = isGrant();
        final boolean isUngrant = isUNGrant();
        final boolean isReject = isReject();
        final boolean isUnreject = isGrant || isUngrant || isReject ? false : true;

        final Set commandSet = (isGrant || isUNGrant()) ? target.getGrantedCommands() : target.getRejectedCommands();

        CommandMapper commandMapper = ConsoleInputEvent.getCommandMapper();
        TextCommand textCommand = commandMapper.findCommand(commandName);
        if (textCommand == null || ShowCommandTag.class.getName().endsWith(textCommand.commandClassName)) {
            MessageOutFn.outln(console, "Command not found:" + commandName);
            return;
        }
        if (textCommand.name.length() != commandName.length()) {
            MessageOutFn.outln(console, "You should specify full command name:" + commandName);
            return;
        }
        if (isUnreject) {
            removeReject(commandSet, textCommand);
            return;
        }
        final CommandHandler ch = ConsoleInputEvent.instantiateCommandHandler(textCommand.commandClassName);
        ch.player = target;
        if (isGrant) {
            if (!(ch instanceof GrantedCommand)) {
                MessageOutFn.outln(console, "ERROR:Not a grantable command:{c" + textCommand.name + "{x");
                return;
            }
            ((GrantedCommand) ch).onGrantAdded();
            commandSet.add(textCommand.commandClassName);
            MessageOutFn.outln(console, "Done.");
        } else if (isReject) {
            commandSet.add(textCommand.commandClassName);
            MessageOutFn.outln(console, "Done.");
        } else { //ungrant
            ungrant(ch, commandSet, textCommand);
        }


    }

    private void ungrant(CommandHandler ch, final Set commandSet, TextCommand textCommand) throws Exception {
        if (ch instanceof GrantedCommand) {
            if (commandSet.remove(textCommand.commandClassName)) {
                ((GrantedCommand) ch).onGrantRemoved();
                MessageOutFn.outln(console, "Grant removed: {c" + textCommand.name + "{x");
            } else {
                MessageOutFn.outln(console, "ERROR:No grant found: {c" + textCommand.name + "{x");
            }
        } else {
            MessageOutFn.outln(console, "ERROR:Not a grantable command: {c" + textCommand.name + "{x");
        }
    }

    private void removeReject(final Set commandSet, TextCommand textCommand) {
        boolean removed = commandSet.remove(textCommand.commandClassName);
        if (removed) {
            MessageOutFn.outln(console, "Done. Command is not more in rejection list:{c" + textCommand.name + "{x");
        } else {
            MessageOutFn.outln(console, "ERROR: Command is not in rejection list:{c" + textCommand.name + "{x");
        }
    }

    public void showHelp() {
        if (isGrant()) {
            MessageOutFn.outln(console, command.name + ": adds granted commands to player");
            MessageOutFn.outln(console, "Usage: " + command.name + " <player_name> <grantable_command>");
        } else if (isUNGrant()) {
            MessageOutFn.outln(console, command.name + ": removes granted commands from player");
            MessageOutFn.outln(console, "Usage: " + command.name + " <player_name> <grantable_command>");
        } else if (isReject()) {
            MessageOutFn.outln(console, command.name + ": rejects common command to be executed by player");
            MessageOutFn.outln(console, "Usage: " + command.name + " <player_name> <command>");
        } else { // UNREJECT
            MessageOutFn.outln(console, command.name + ": removes rejected command mark from player");
            MessageOutFn.outln(console, "Usage: " + command.name + " <player_name> <command>");
        }
    }

    private boolean isGrant() {
        return command.tag.equals("GRANT");
    }

    private boolean isUNGrant() {
        return command.tag.equals("UNGRANT");
    }

    private boolean isReject() {
        return command.tag.equals("REJECT");
    }

//	private boolean isUNReject() {
//		return command.tag.equals("UNREJECT");
//	}
//

}