package h04.movement;

import fopbot.Direction;
import fopbot.Robot;

public class WalkingMoveStrategy implements MoveStrategy {

    @Override
    public void move(Robot robot, int dx, int dy) {
        if (dx < 0) {
            turnFacing(robot, Direction.LEFT);
        }
        if (dx > 0) {
            turnFacing(robot, Direction.RIGHT);
        }
        dx = Math.abs(dx);
        while (dx --> 0) {
            robot.move();
        }

        if (dy < 0) {
            turnFacing(robot, Direction.DOWN);
        }
        if (dy > 0) {
            turnFacing(robot, Direction.UP);
        }
        dy = Math.abs(dy);
        while (dy --> 0) {
            robot.move();
        }

        turnFacing(robot, Direction.UP);
    }

    private void turnFacing(Robot robot, Direction dir) {
        while (robot.getDirection() != dir) {
            robot.turnLeft();
        }
    }
}
