/
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.builder;

import net.sourceforge.pain.data.*;
import net.sourceforge.pain.tinylib.*;
import net.sourceforge.pain.tinylib.data.*;
import net.sourceforge.pain.tinylib.data.type.*;
import net.sourceforge.pain.tinylib.logic.fn.*;

/**
 * PAiN  Date: 05.06.2003  Time: 1:40:02
 */
public final class BC_Room extends BuilderCommand {

    private Room room;
    private int state = STATE_INITIAL;

    private static final int STATE_INITIAL = 0;
    private static final int STATE_ASK_CREATE_NEW = 1;
    private static final int STATE_ENTER_NAME = 2;
    private static final int STATE_ENTER_DESC = 3;
    private static final int STATE_ENTER_CAPACITY = 4;

    private String space_id;
    private String newName;
    private String newDesc;
    private int newCapcacity;


    public void processBuilderCommand(BuilderShell p, String args) throws Exception {
        if (state > STATE_INITIAL) {
            processReply(p, args);
        } else if (args == null || args.length() == 0) {
            showUsage(p.console);
        } else {
            String id = args;
            RoomsRegistry reg = Mudlib.getWorld().getRoomsRegistry();
            room = reg.findRoom(id);

            p.activeCommand = this;
            space_id = id;
            if (room == null) {
                askToCreateNew(p);
            } else {
                MessageOutFn.outln(p.console, "Space found!");
                MessageOutFn.outln(p.console, "{W" + room.getName() + "{x");
                MessageOutFn.outln(p.console, room.getDesc());
                MessageOutFn.outln(p.console, "Capaciy :" + room.getCapacity());
                askName(p);
            }
        }
    }

    private void processReply(BuilderShell p, String reply) throws Exception {
        switch (state) {
            case STATE_ASK_CREATE_NEW:
                if ("Y".equalsIgnoreCase(reply.substring(0, 1))) {
                    askName(p);
                } else {
                    cancelCommand(p);
                }
                break;
            case STATE_ENTER_NAME:
                reply = reply.trim();
                if (reply.length() == 0) {
                    if (room == null) {
                        MessageOutFn.outln(p.console, "Name is too short!");
                        askName(p);
                    } else {
                        newName = room.getName();
                        MessageOutFn.outln(p.console, "Reusing old name:" + newName);
                        askSpaceDesc(p);
                    }
                } else if (reply.equalsIgnoreCase("x")) {
                    cancelCommand(p);
                } else if (reply.length() == 1) {
                    MessageOutFn.outln(p.console, "Name is too short!");
                    askName(p);
                } else {
                    newName = reply;
                    askSpaceDesc(p);
                }
                break;
            case STATE_ENTER_DESC:
                reply = reply.trim();
                if (reply.length() == 0) {
                    if (room == null) {
                        MessageOutFn.outln(p.console, "Description is too short!");
                        askSpaceDesc(p);
                    } else {
                        newDesc = room.getDesc();
                        MessageOutFn.outln(p.console, "Reusing old desc.");
                        askSpaceCapacity(p);
                    }
                } else if (reply.equalsIgnoreCase("x")) {
                    cancelCommand(p);
                } else if (reply.length() < 10) {
                    MessageOutFn.outln(p.console, "Description is too short!");
                    askSpaceDesc(p);
                } else {
                    newDesc = reply;
                    askSpaceCapacity(p);
                }
                break;
            case STATE_ENTER_CAPACITY:
                reply = reply.trim();
                if (reply.length() == 0) {
                    if (room == null) {
                        askSpaceCapacity(p);
                    } else {
                        newCapcacity = room.getCapacity();
                        MessageOutFn.outln(p.console, "Reusing old capacity.");
                        finish(p);
                    }
                } else if (reply.equalsIgnoreCase("x")) {
                    cancelCommand(p);
                } else {
                    try {
                        newCapcacity = Integer.parseInt(reply);
                        if (newCapcacity < 0) {
                            throw new IllegalArgumentException("specified capacity < 0 :" + newCapcacity);
                        }
                    } catch (Exception e) {
                        MessageOutFn.outln(p.console, "{R" + e.getClass().getName() + ":" + e.getMessage() + "{x");
                        newCapcacity = 0;
                        askSpaceCapacity(p);
                    }
                    if (newCapcacity > 0) {
                        finish(p);
                    }
                }
                break;
        }

    }

    private void finish(BuilderShell p) throws Exception {
        if (room == null) {
            room = (Room) ObjectFactory.createRaw(Room.class);
            room.setRoomUniqueId(space_id);
            fillSpace();
            Mudlib.getWorld().getRoomsRegistry().registerRoom(room);
        } else {
            fillSpace();
        }
        MessageOutFn.outln(p.console, "Done.");
        p.activeCommand = null;
    }

    private void fillSpace() {
        Space space = room.asSpace();
        space.setName(newName);
        space.setDesc(newDesc);
        space.setCapacity(newCapcacity);
    }

    private void cancelCommand(BuilderShell p) {
        p.activeCommand = null;
    }

    private void askName(BuilderShell p) {
        MessageOutFn.out(p.console, "Enter new space name (" + (room == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
        state = STATE_ENTER_NAME;
    }

    private void askSpaceDesc(BuilderShell p) {
        MessageOutFn.out(p.console, "Enter new space desc (" + (room == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
        state = STATE_ENTER_DESC;
    }

    private void askSpaceCapacity(BuilderShell p) {
        MessageOutFn.out(p.console, "Enter new space capacity (" + (room == null ? "" : "ENTER - leave old, ") + "'X' to stop command) :");
        state = STATE_ENTER_CAPACITY;
    }

    private void askToCreateNew(BuilderShell p) {
        MessageOutFn.outln(p.console, "Space not found:" + space_id);
        MessageOutFn.out(p.console, "Would you like to craete new? (Y/N):");
        state = STATE_ASK_CREATE_NEW;

    }


    public void showUsage(Console console) {
        MessageOutFn.outln(console, "Space:Command syntax <SPACE_ID>\n");
    }

    public void showHelp(Console console) {
        MessageOutFn.outln(console, "Builder command SPACE is used to create or modify indexed space");
        MessageOutFn.outln(console, "Usage: space <SPACE_ID>");
    }

    public String getName() {
        return "Space";
    }
}