package cicaprojekt;

public class PlayerBase implements Destroyable {
    protected Tile currentTile;
    protected Direction facingDirection;
    protected String name;
    protected boolean destroyed;

    public PlayerBase(String name, Tile startTile, Direction startDirection) {
        this.name = name;
        currentTile = startTile;
        facingDirection = startDirection;
        destroyed = false;
    }

    @Override
    public String toString() {
        return String.format("%s: %s", name, currentTile);
    }

    @Override
    public void destroy() {
        destroyed = true;
        Game.instance.playerBaseDestroyed(this);
    }

    public Tile getCurrentTile() {
        return currentTile;
    }

    public void setCurrentTile(Tile newCurrentTile) {
        currentTile = newCurrentTile;
    }

    public void move(Direction direction) {
        if(!isDestroyed()) {
            this.setFacingDirection(direction);
            Tile adjtile = this.getCurrentTile().getAdjacentTile(direction);

            if (adjtile != null) {
                if (adjtile.isSteppable()) {
                    currentTile.onExit(this);
                    setCurrentTile(adjtile);
                    adjtile.onEntry(this);
                }
            }
        }
    }

    public void pickZPM(Tile tile) { /* PlayerBase does not collect ZPM modules */ }

    public void rotateLeft() {
        switch (facingDirection) {
            case NORTH:
                facingDirection = Direction.WEST;
                break;
            case WEST:
                facingDirection = Direction.SOUTH;
                break;
            case SOUTH:
                facingDirection = Direction.EAST;
                break;
            case EAST:
                facingDirection = Direction.NORTH;
                break;
        }
    }

    public void rotateRight() {
        switch (facingDirection) {
            case NORTH:
                facingDirection = Direction.EAST;
                break;
            case EAST:
                facingDirection = Direction.SOUTH;
                break;
            case SOUTH:
                facingDirection = Direction.WEST;
                break;
            case WEST:
                facingDirection = Direction.NORTH;
                break;
        }
    }

    public Direction getFacingDirection() {
        return facingDirection;
    }

    public void setFacingDirection(Direction direction) {
        facingDirection = direction;
    }
    
    public boolean isDestroyed(){
        return destroyed;
    }
}
