package de.steamwar.bausystem.world;

@SuppressWarnings({"unused", "UnusedReturnValue"})
public class SizedStack<T> {

    private final int maxSize;
    private final T[] data;
    private int size;
    private int head;

    public SizedStack(int size) {
        this.maxSize = size;
        this.data = (T[]) new Object[this.maxSize];
        this.head = 0;
        this.size = 0;
    }

    public T push(final T element) {
        this.data[this.head] = element;
        this.increaseHead();
        this.increaseSize();
        return element;
    }

    public T pop() {
        this.decreaseHead();
        this.decreaseSize();
        final T result = this.data[this.head];
        this.data[this.head] = null;
        return result;
    }

    public T peek() {
        return this.data[this.head];
    }

    public boolean empty() {
        return this.size == 0;
    }

    protected boolean canEqual(final Object other) {
        return other instanceof SizedStack;
    }

    private void increaseHead() {
        this.head++;
        while (this.head > this.maxSize - 1) {
            this.head -= this.maxSize;
        }
    }

    private void decreaseHead() {
        this.head--;
        while (this.head < 0) {
            this.head += this.maxSize;
        }
    }

    private void increaseSize() {
        if (this.size < this.maxSize) {
            this.size++;
        }
    }

    private void decreaseSize() {
        if (this.size > 0) {
            this.size--;
        }
    }

    @Override
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        result = result * PRIME + this.maxSize;
        result = result * PRIME + this.toString().hashCode();
        result = result * PRIME + this.size;
        return result;
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof SizedStack)) {
            return false;
        }
        final SizedStack<?> other = (SizedStack<?>) o;
        if (!other.canEqual(this)) {
            return false;
        }
        if (this.maxSize != other.maxSize) {
            return false;
        }
        if (this.size != other.size) {
            return false;
        }
        if (!this.data.getClass().equals(other.data.getClass())) {
            return false;
        }
        return this.toString().equals(other.toString());
    }

    public int getMaxSize() {
        return maxSize;
    }

    public int getSize() {
        return size;
    }

    @Override
    public String toString() {
        final StringBuilder result = new StringBuilder("[");
        for (int i = 0; i < this.size - 1; i++) {
            result.append(this.data[(this.head - i - 1 < 0) ? (this.head - i - 1 + this.maxSize) : (this.head - i - 1)]).append(",");
        }
        result.append(this.data[(this.head - this.size < 0) ? (this.head - this.size + this.maxSize) : (this.head - this.size)]);
        result.append("]");
        return result.toString();
    }
}