package org.academiadecodigo.carcrash.field;

public class Position {

    private int col;
    private int row;

    public Position() {
        setRandomPosition();
    }

    public int getCol() {
        return this.col;
    }

    public int getRow() {
        return this.row;
    }

    public void setRandomPosition() {
        this.col = (int) (Math.random() * Field.width);
        this.row = (int) (Math.random() * Field.height);
    }

    public void setPosition(int col, int row) {
        this.col = col;
        this.row = row;
    }

    public Direction move(Direction direction, int steps) {
        int newCol = this.col;
        int newRow = this.row;
        Direction newDirection = direction;

        switch (direction) {
            case NORTH:
                newCol -= steps;
                break;
            case SOUTH:
                newCol += steps;
                break;
            case WEST:
                newRow -= steps;
                break;
            case EAST:
                newRow += steps;
                break;
            default:
                break;
        }
        if (isEdge(newCol, newRow)) {
            return move(direction.getOpposite(), steps);
        }
        setPosition(newCol, newRow);
        return newDirection;
    }

    public boolean isEdge(int col, int row) {
        return (col <= 0 || col >= Field.width) ||
               (row <= 0 || row >= Field.height);
    }

    public boolean equals(Position other) {
        return this.col == other.col && this.row == other.row;
    }
}
