package cicaprojekt;

import java.util.Stack;

public class Scale extends Field {
    private Gate gateConnected;
    private int threshold;
    private int weight;


    public Scale(Gate gate, int threshold) {
        gateConnected = gate;
        this.threshold = threshold;
        boxStack = new Stack<Box>();
    }

    @Override
    public boolean isSteppable()
    {
        if (boxStack.isEmpty())
            return true;

        return false;
    }

    @Override
    public void onEntry(PlayerBase playerBase) {
        super.onEntry(playerBase);
        gateConnected.setOpen(true);
    }

    @Override
    public void onExit(PlayerBase playerBase) {
        gateConnected.setOpen(false);
    }

    @Override
    public Box getABox() {
        if (boxStack.isEmpty())
            return null;
        weight -= boxStack.peek().weight();
        stackChanged();
        return boxStack.pop();
    }

    @Override
    public void putABox(Box box) {
        if (box == null)
            return;
        boxStack.push(box);
        weight += box.weight();
        stackChanged();
    }

    private void stackChanged() {
        if (weight >= threshold)
            gateConnected.setOpen(true);
        else
            gateConnected.setOpen(false);
    }
    
    public Gate getGateConnected() {
        return gateConnected;
    }
}
