/*
Copyright (C) 2005 David Green <green@couchpotato.net>
All Rights Reserved.

This file is part of Aelfengard.

Aelfengard is proprietary software. You may not redistribute it without
prior written permission from the copyright holder.
*/

package server.command;

import server.Area;
import server.Player;
import server.Room;
import server.SyntaxException;
import server.Utils;

public class AreaCommand extends Command {

    private final boolean canModify;
    
    public AreaCommand(boolean canModify) {
        super(CommandCategory.ADMIN, "Allows " + (canModify ? "creation/destruction/" : "") + "listing/goto of areas.");
        this.canModify = canModify;
    }
    
    @Override
    public void run(Player player) throws SyntaxException {
        String subcmd = CommandProcessor.nextToken();
        if (subcmd == null) {
            throw new SyntaxException();
        }
        if (subcmd.equals("list")) {
            if (CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            StringBuffer sb = new StringBuffer();
            for (Area area : Area.getAreas()) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append("@10F");
                sb.append(area.getName());
                sb.append("@ZZZ");
            }
            player.sendText(true, "The following areas are available: " + sb.toString());
        }
        else if (subcmd.equals("goto")) {
            String areaName = CommandProcessor.nextToken();
            if (areaName == null || CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            Area area = Area.findByName(areaName); 
            if (area == null) {
                player.sendText(true, "Sorry, that area doesn't exist.");
                return;
            }
            Room room = area.getDefaultRoom();
            if (room == null) {
                player.sendText(true, "Sorry, I can't find a room to put you in.");
                return;
            }
            player.moveTo(room);
        }
        else if (!canModify) {
            throw new SyntaxException();
        }
        else if (subcmd.equals("create")) {
            String areaName = CommandProcessor.nextToken();
            if (areaName == null || CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            if (Area.findByName(areaName) != null) {
                player.sendText(true, "Sorry, that area already exists.");
                return;
            }
            if (!Utils.isAlpha(areaName)) {
                player.sendText(true, "Invalid area name: " + areaName);
                return;
            }
            new Area(areaName);
            player.sendText(true, "Area '" + areaName + "' has been created.");
        }
        else if (subcmd.equals("destroy")) {
            String areaName = CommandProcessor.nextToken();
            if (areaName == null || CommandProcessor.nextToken() != null) {
                throw new SyntaxException();
            }
            Area area = Area.findByName(areaName);
            if (area == null) {
                player.sendText(true, "Sorry, that area doesn't exist.");
                return;
            }
            if (areaName.equals("main")) {
                player.sendText(true, "The 'main' area is special and cannot be destroyed.");
                return;
            }
            if (area.getSize() != 1) {
                player.sendText(true, "An area must contain only a single room " +
                        "before it can be destroyed.");
                return;
            }
            area.destroy();
            player.sendText(true, "Area '" + areaName + "' has been destroyed.");
        }
        else {
            throw new SyntaxException();
        }
    }

    @Override
    public String[] getSyntax(Player player) {
        if (canModify) {
            return new String[] {
                    "CREATE <name>",
                    "DESTROY <name>",
                    "LIST",
                    "GOTO <name>",
            };
        }
        else {
            return new String[] {
                    "LIST",
                    "GOTO <name>",
            };
        }
    }

}