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

public class GiveCommand extends Command {
    
    private final boolean toss;
    
    private static final TokenString TS_GIVE = new TokenString("%N1 %v1/gives/give/ %n2 to %n3.");
    private static final TokenString TS_TOSS = new TokenString("%N1 %v1/tosses/toss/ %n2 to %n3.");

    public GiveCommand(boolean toss) {
        super(CommandCategory.INTERACTION, (toss ? "Toss" : "Give") + " an item to someone.");
        this.toss = toss;
    }

    @Override
    public void run(Player player) throws SyntaxException {
        String itemName = CommandProcessor.nextToken();
        String targetName = CommandProcessor.nextToken();
        if ("to".equals(targetName)) {
            targetName = CommandProcessor.nextToken();
        }
        if (itemName == null || targetName == null || CommandProcessor.nextToken() != null) {
            throw new SyntaxException();
        }
        Item item = player.matchInventoryItem(itemName);
        if (item == null) {
            player.sendText(true, "You're not holding anything by that name.");
            return;
        }
        Living target = player.matchLocalLiving(targetName);
        if (target == null) {
            player.sendText(true, "There's noone here by that name.");
            return;
        }
        if (target == player) {
            player.sendText(true, "You can't " + (toss ? "toss" : "give") + " something to yourself.");
            return;
        }
        item.moveTo(target.getInventory());
        player.getRoom().sendText(null, null, toss ? TS_TOSS : TS_GIVE, null, null, null, player, item, target);
    }

    @Override
    public String[] getSyntax(Player player) {
        return new String[] { "<item> [to] <who>" };
    }

}