// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.modules.collision;

import java.util.Arrays;
import javax.annotation.Nonnull;
import com.hypixel.hytale.math.vector.Vector3i;

public class BlockTracker implements IBlockTracker
{
    public static final int NOT_FOUND = -1;
    protected static final int ALLOC_SIZE = 4;
    @Nonnull
    protected Vector3i[] positions;
    protected int count;
    
    public BlockTracker() {
        this.positions = new Vector3i[4];
        for (int i = 0; i < this.positions.length; ++i) {
            this.positions[i] = new Vector3i();
        }
    }
    
    @Override
    public Vector3i getPosition(final int index) {
        return this.positions[index];
    }
    
    @Override
    public int getCount() {
        return this.count;
    }
    
    public void reset() {
        this.count = 0;
    }
    
    @Override
    public boolean track(final int x, final int y, final int z) {
        if (this.isTracked(x, y, z)) {
            return true;
        }
        this.trackNew(x, y, z);
        return false;
    }
    
    @Override
    public void trackNew(final int x, final int y, final int z) {
        if (this.count >= this.positions.length) {
            this.alloc();
        }
        final Vector3i v = this.positions[this.count++];
        v.x = x;
        v.y = y;
        v.z = z;
    }
    
    @Override
    public boolean isTracked(final int x, final int y, final int z) {
        return this.getIndex(x, y, z) >= 0;
    }
    
    @Override
    public void untrack(final int x, final int y, final int z) {
        final int index = this.getIndex(x, y, z);
        if (index >= 0) {
            this.untrack(index);
        }
    }
    
    public void untrack(final int index) {
        if (this.count <= 0) {
            throw new IllegalStateException("Calling untrack on empty tracker");
        }
        --this.count;
        if (this.count == 0) {
            return;
        }
        final Vector3i v = this.positions[index];
        System.arraycopy(this.positions, index + 1, this.positions, index, this.count - index);
        this.positions[this.count] = v;
    }
    
    public int getIndex(final int x, final int y, final int z) {
        for (int i = this.count - 1; i >= 0; --i) {
            final Vector3i v = this.positions[i];
            if (v.x == x && v.y == y && v.z == z) {
                return i;
            }
        }
        return -1;
    }
    
    protected void alloc() {
        this.positions = Arrays.copyOf(this.positions, this.positions.length + 4);
        for (int i = this.count; i < this.positions.length; ++i) {
            this.positions[i] = new Vector3i();
        }
    }
}
