package leigh.mud.engine.cmd;

import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.mud.engine.Direction;
import leigh.mud.engine.Exit;
import leigh.mud.engine.MudSession;
import leigh.mud.engine.Room;

import java.util.Arrays;
import java.util.Map;

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

    public WorldEditRoomExitCommand(MudSession session) {
        putCommand("create", cmd -> {
            logger.info("exit create called: {}", Arrays.toString(cmd));

            if (cmd.length == 2) {
                Direction srcDir = Direction.getDirection(cmd[1]);
                Direction dstDir = Direction.opposite(srcDir);

                Room src = session.getLocation();
                Room dst = session.getEngine().createVoidRoom();

                // TODO: Implement setting exit description
                Exit srcExit = new Exit(dst.getId(), null);
                Exit dstExit = new Exit(src.getId(), null);

                src.getExits().put(srcDir, srcExit);
                dst.getExits().put(dstDir, dstExit);

                return "OK";
            }

            return session.getUnknownResponse();
        });

        putCommand("list", cmd -> {
            if (cmd.length == 1) {
                StringBuilder sb = new StringBuilder();
                sb.append("=====\r\n");
                for (Map.Entry<Direction, Exit> entry : session.getLocation().getExits().entrySet()) {
                    sb.append(entry.getKey());
                    sb.append(" ");
                    sb.append(entry.getValue());
                    sb.append("\r\n");
                }
                sb.append("=====\r\n");
                return sb.toString();
            }

            return session.getUnknownResponse();
        });
    }
}
