/*
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.NickSystem;
import server.Player;
import server.SyntaxException;
import server.Utils;
import server.token.TokenString;

public class IntroduceCommand extends Command {

    private static final TokenString TS_INTRODUCE_AUDIO = new TokenString("@00E%MN1 %v1/introduces/introduce/ %c21/%x2/%n2/ as %s0.");
    private static final TokenString TS_INTRODUCE_VISUAL = new TokenString("@00E%MN1 %v1/appears/appear/ to be introducing %Mc21/%x2/%n2/.");

    public IntroduceCommand() {
        super(CommandCategory.COMMUNICATION, "Introduces the specified player to those in the room with you.");
    }
    
    @Override
    public void run(Player player) throws SyntaxException {
        String who = CommandProcessor.nextToken();
        String as = CommandProcessor.nextToken();
        String name = CommandProcessor.nextToken();
        if (who == null || CommandProcessor.nextToken() != null) {
            throw new SyntaxException();
        }
        if (as != null) {
            if (!as.equalsIgnoreCase("as")) {
                throw new SyntaxException();
            }
            if (name == null) {
                throw new SyntaxException();
            }
            if (!Utils.isAlpha(name)) {
                player.sendText(true, "Invalid name - Must be alpha only.");
                return;
            }
            name = Utils.capitalize(name);
        }

        Player introducee = player.matchLocalPlayer(who);
        if (introducee == null) {
            player.sendText(true, "There's noone here by that name.");
            return;
        }
        
        if (as == null) {
            name = NickSystem.get(player.getId(), introducee.getId());
            if (name == null) {
                if (player == introducee) {
                    player.sendText(true, "You can set your name for introductions by using @10FNICKNAME MYSELF@ZZZ.");
                }
                else {
                    player.sendText(true, "You can't introduce this player without specifying a name.");
                }
                return;
            }
        }
        
        player.getRoom().sendText(null, TS_INTRODUCE_AUDIO, TS_INTRODUCE_VISUAL, TS_INTRODUCE_AUDIO, null, new String[] { name }, player, introducee);
    }

    @Override
    public String[] getSyntax(Player player) {
        return new String[] { "<player|MYSELF> [AS <name>]" };
    }

}