package org.academiadecodigo.carcrash.cars;

import org.academiadecodigo.carcrash.field.Position;

abstract public class Car {

    /** The position of the car on the grid */
    private Position pos;
    private boolean crashed;
    private CarType brand;
    private double maxSpeed;
    private Position finalDestination;

    public Car(CarType brand, Position initPos) {
        this.pos = initPos;
        this.crashed = false;
        this.brand = brand;
        setNewFinalDestination();
    }

    public Position getPos() {
        return pos;
    }

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

    public void setNewFinalDestination() {
        this.finalDestination = new Position();
    }

    public Position getFinalDestination() {
        return this.finalDestination;
    }

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

    public double getMaxSpeed() {
        return this.maxSpeed;
    }

    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public void move() {
        if (isCrashed()) { return; }

        if (getPos().equals(getFinalDestination())) {
            setNewFinalDestination();
        }

        this.getPos().moveTowards(getFinalDestination(), getMaxSpeed());
    }

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