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

package com.hypixel.hytale.builtin.buildertools.scriptedbrushes;

import com.hypixel.hytale.math.vector.Vector3i;
import javax.annotation.Nonnull;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.accessor.ChunkAccessor;
import com.hypixel.hytale.server.core.universe.world.accessor.LocalCachedChunkAccessor;

public class BrushConfigChunkAccessor extends LocalCachedChunkAccessor
{
    private final BrushConfigEditStore editOperation;
    
    @Nonnull
    public static BrushConfigChunkAccessor atWorldCoords(final BrushConfigEditStore editOperation, final ChunkAccessor<WorldChunk> delegate, final int centerX, final int centerZ, final int blockRadius) {
        final int chunkRadius = ChunkUtil.chunkCoordinate(blockRadius) + 1;
        return atChunkCoords(editOperation, delegate, ChunkUtil.chunkCoordinate(centerX), ChunkUtil.chunkCoordinate(centerZ), chunkRadius);
    }
    
    @Nonnull
    public static BrushConfigChunkAccessor atChunkCoords(final BrushConfigEditStore editOperation, final ChunkAccessor<WorldChunk> delegate, final int centerX, final int centerZ, final int chunkRadius) {
        return new BrushConfigChunkAccessor(editOperation, delegate, centerX, centerZ, chunkRadius);
    }
    
    protected BrushConfigChunkAccessor(final BrushConfigEditStore editOperation, final ChunkAccessor<WorldChunk> delegate, final int centerX, final int centerZ, final int radius) {
        super(delegate, centerX, centerZ, radius);
        this.editOperation = editOperation;
    }
    
    @Override
    public int getBlock(@Nonnull final Vector3i pos) {
        if (this.editOperation.getAfter().hasBlockAtWorldPos(pos.x, pos.y, pos.z)) {
            return this.editOperation.getAfter().getBlockAtWorldPos(pos.x, pos.y, pos.z);
        }
        return this.getBlockIgnoringHistory(pos);
    }
    
    @Override
    public int getBlock(final int x, final int y, final int z) {
        if (this.editOperation.getAfter().hasBlockAtWorldPos(x, y, z)) {
            return this.editOperation.getAfter().getBlockAtWorldPos(x, y, z);
        }
        return this.getBlockIgnoringHistory(x, y, z);
    }
    
    public int getBlockIgnoringHistory(@Nonnull final Vector3i pos) {
        return this.getBlockIgnoringHistory(pos.x, pos.y, pos.z);
    }
    
    public int getBlockIgnoringHistory(final int x, final int y, final int z) {
        if (this.editOperation.getBefore().hasBlockAtWorldPos(x, y, z)) {
            return this.editOperation.getBefore().getBlockAtWorldPos(x, y, z);
        }
        return this.getChunk(ChunkUtil.indexChunkFromBlock(x, z)).getBlock(x, y, z);
    }
    
    @Override
    public int getFluidId(final int x, final int y, final int z) {
        return this.editOperation.getFluid(x, y, z);
    }
}
