package org.academiadecodigo.carcrash.cars;

import org.academiadecodigo.carcrash.field.Direction;
import org.academiadecodigo.carcrash.field.Position;

public abstract class Car {

    /** The position of the car on the grid */
    private Position pos;
    private Direction orientation;
    private boolean crashed;
    private char letter;
    private Brand brand;

    public Car(Brand brand) {
        this.pos = new Position();
        this.orientation = Direction.values()[(int) (Math.random() * Direction.values().length)];
        this.brand = brand;
    }

    public Position getPos() {
        return pos;
    }

    public Direction getOrientation() {
        return orientation;
    }

    public void setDirection(Direction orientation) {
        this.orientation = orientation;
    }

    public boolean isCrashed() {
        return this.crashed;
    }

    public Direction getNewOrientation() {
        Direction newDirection = Direction.values()[(int) (Math.random() * Direction.values().length)];

        while (orientation.isOpposite(newDirection)) {
            newDirection = Direction.values()[(int) (Math.random() * Direction.values().length)];
        }

        return newDirection;
    }

    public void crash() {
        this.crashed = true;
    }

    public abstract void move();

    @Override
    public String toString() {
        return (isCrashed()) ? "C" : Character.toString(this.brand.getSymbol());
    }
}
