package cicaprojekt;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public abstract class Tile {
    protected Map<Direction, Tile> adjacentTile;
    protected ZPM zpmOnTile;
    protected Stack<Box> boxStack;
    protected int y = -666;
    protected int x = -666;
    protected PlayerBase playerBaseOnTile = null;


    public Tile() {
        adjacentTile = new HashMap<Direction, Tile>();
        boxStack = new Stack<>();
    }

    public int getX() { return this.x; }

    public int getY() { return this.y; }

    public void setX(int x) {
        if (x >= 0)
            this.x = x;
    }

    public void setY(int y) {
        if (y >= 0)
            this.y = y;
    }

    public abstract boolean isSteppable();

    public Tile getAdjacentTile(Direction direction) {
        return adjacentTile.get(direction);
    }

    public void setAdajacentTile(Tile newTile, Direction direction) {
        adjacentTile.put(direction, newTile);
    }

    public void spawnStargate(Stargate stargate, Direction direction) {
        if (playerBaseOnTile != null && playerBaseOnTile.name.equals("Replicator"))
            playerBaseOnTile.destroy();

        adjacentTile.get(direction).spawnStargate(stargate, direction);
    }

    public void onEntry(PlayerBase playerBase) {
        playerBaseOnTile = playerBase;
    }

    public abstract void onExit(PlayerBase playerBase);

    public void setZPMOnTile(ZPM zpm) {
        zpmOnTile = zpm;
    }

    public ZPM getZPMFromTile() {
        ZPM zpm = zpmOnTile;
        zpmOnTile = null;
        return zpm;
    }

    public void putABox(Box box) {
        if (box == null)
            return;
        boxStack.push(box);
    }

    public Box getABox() {
        if (boxStack.isEmpty())
            return null;
        return boxStack.pop();
    }

    @Override
    public String toString() {
        return String.format("%d, %d", x, y);
    }
    
    public boolean hasZPM() {
        if(zpmOnTile == null)
            return false;
        else
            return true;
    }
    
    public boolean hasBox() {
        return !boxStack.isEmpty();
    }
}
