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

package com.hypixel.hytale.server.worldgen.loader;

import com.hypixel.hytale.server.core.prefab.selection.buffer.impl.PrefabBuffer;
import com.hypixel.hytale.server.core.prefab.PrefabRotation;
import com.hypixel.hytale.server.worldgen.util.bounds.IChunkBounds;
import com.hypixel.hytale.server.worldgen.chunk.ChunkGenerator;
import com.hypixel.hytale.server.core.prefab.selection.buffer.impl.IPrefabBuffer;
import com.hypixel.hytale.server.core.prefab.selection.buffer.PrefabLoader;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.worldgen.util.bounds.ChunkBounds;
import java.nio.file.Path;
import com.hypixel.hytale.server.core.prefab.selection.buffer.PrefabSupplier;

public class WorldGenPrefabSupplier implements PrefabSupplier
{
    public static final WorldGenPrefabSupplier[] EMPTY_ARRAY;
    private final WorldGenPrefabLoader loader;
    private final String prefabKey;
    private final Path path;
    private String prefabName;
    @Nullable
    private ChunkBounds bounds;
    
    public WorldGenPrefabSupplier(final WorldGenPrefabLoader loader, final String prefabKey, final Path path) {
        this.loader = loader;
        this.path = path;
        this.bounds = null;
        this.prefabKey = prefabKey;
    }
    
    public WorldGenPrefabLoader getLoader() {
        return this.loader;
    }
    
    @Nonnull
    public String getName() {
        return this.path.toString();
    }
    
    @Nonnull
    public String getPrefabName() {
        if (this.prefabName == null) {
            this.prefabName = PrefabLoader.resolveRelativeJsonPath(this.prefabKey, this.path, this.loader.getRootFolder());
        }
        return this.prefabName;
    }
    
    public Path getPath() {
        return this.path;
    }
    
    @Nullable
    @Override
    public IPrefabBuffer get() {
        return ChunkGenerator.getResource().prefabs.get(this);
    }
    
    @Nonnull
    public IChunkBounds getBounds(@Nonnull final IPrefabBuffer buffer) {
        if (this.bounds == null) {
            this.bounds = this.getBounds(0, 0, 0, buffer, PrefabRotation.ROTATION_0, new ChunkBounds());
        }
        return this.bounds;
    }
    
    @Override
    public boolean equals(@Nullable final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final WorldGenPrefabSupplier that = (WorldGenPrefabSupplier)o;
        return this.path.equals(that.path);
    }
    
    @Override
    public int hashCode() {
        return this.path.hashCode();
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "WorldGenPrefabSupplier{path=" + String.valueOf(this.path);
    }
    
    @Nonnull
    private ChunkBounds getBounds(final int depth, final int x, final int z, @Nonnull final IPrefabBuffer prefab, @Nonnull final PrefabRotation rotation, @Nonnull final ChunkBounds bounds) {
        if (depth >= 10) {
            return bounds;
        }
        final int minX = x + prefab.getMinX(rotation);
        final int minZ = z + prefab.getMinZ(rotation);
        final int maxX = x + prefab.getMaxX(rotation);
        final int maxZ = z + prefab.getMaxZ(rotation);
        bounds.include(minX, minZ, maxX, maxZ);
        for (final PrefabBuffer.ChildPrefab child : prefab.getChildPrefabs()) {
            final int childX = x + rotation.getX(child.getX(), child.getZ());
            final int childZ = z + rotation.getZ(child.getX(), child.getZ());
            for (final WorldGenPrefabSupplier supplier : this.loader.get(child.getPath())) {
                final IPrefabBuffer childPrefab = supplier.get();
                final PrefabRotation childRotation = rotation.add(child.getRotation());
                this.getBounds(depth + 1, childX, childZ, childPrefab, childRotation, bounds);
            }
        }
        return bounds;
    }
    
    static {
        EMPTY_ARRAY = new WorldGenPrefabSupplier[0];
    }
}
