package leigh.mud.engine.cmd;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mecha.util.StringAccumulator;
import leigh.mud.engine.MudSession;

import java.util.Arrays;

/**
 * Implementation of the WE ROOM command. This allows for manipulation of a room.
 *
 * @author Alex Leigh
 */
public class WorldEditRoomCommand extends CommandFrame {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(WorldEditRoomCommand.class);
    private final MudSession session;

    public WorldEditRoomCommand(MudSession session) {
        this.session = session;
        putCommand("title", cmd -> {
            logger.info("Called: {}", cmd);

            if (cmd.length == 1) {
                return "[title: " + session.getLocation().getTitle() + "]";
            } else {
                String[] args = Arrays.copyOfRange(cmd, 1, cmd.length);
                StringAccumulator sa = new StringAccumulator(" ");
                sa.appendAll(args);
                session.getLocation().setTitle(sa.toString());
                return "OK";
            }
        });

        putCommand("desc", cmd -> {
            logger.info("Called: {}", cmd);

            if (cmd.length == 1) {
                return "[desc: " + session.getLocation().getDesc() + "]";
            } else {
                String[] args = Arrays.copyOfRange(cmd, 1, cmd.length);
                StringAccumulator sa = new StringAccumulator(" ");
                sa.appendAll(args);
                session.getLocation().setDesc(sa.toString());
                return "OK";
            }
        });

        putCommand("exit", new WorldEditRoomExitCommand(session));
    }
}