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

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

import com.google.gson.JsonObject;
import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import javax.annotation.Nonnull;
import java.nio.file.Path;

public class ZoneFileContext extends FileContext<FileLoadingContext>
{
    private final Registry<BiomeFileContext> tileBiomes;
    private final Registry<BiomeFileContext> customBiomes;
    
    public ZoneFileContext(final int id, final String name, final Path filepath, final FileLoadingContext context) {
        super(id, name, filepath, context);
        this.tileBiomes = new Registry<BiomeFileContext>("TileBiome");
        this.customBiomes = new Registry<BiomeFileContext>("CustomBiome");
    }
    
    @Nonnull
    public Registry<BiomeFileContext> getTileBiomes() {
        return this.tileBiomes;
    }
    
    @Nonnull
    public Registry<BiomeFileContext> getCustomBiomes() {
        return this.customBiomes;
    }
    
    @Nonnull
    public Registry<BiomeFileContext> getBiomes(@Nonnull final BiomeFileContext.Type type) {
        return switch (type) {
            default -> throw new MatchException(null, null);
            case Tile -> this.getTileBiomes();
            case Custom -> this.getCustomBiomes();
        };
    }
    
    @Nonnull
    public ZoneFileContext matchContext(@Nullable final JsonElement json, final String key) {
        if (json == null || !json.isJsonObject()) {
            return this;
        }
        final JsonElement element = json.getAsJsonObject().get(key);
        if (element == null || !element.isJsonObject()) {
            return this;
        }
        final JsonObject object = element.getAsJsonObject();
        if (!object.has("File")) {
            return this;
        }
        final String filePath = object.get("File").getAsString();
        return this.matchContext(filePath);
    }
    
    @Nonnull
    public ZoneFileContext matchContext(@Nonnull final String filePath) {
        if (!filePath.startsWith("Zones.")) {
            return this;
        }
        final int nameStart = "Zones.".length();
        final int nameEnd = filePath.indexOf(46, nameStart);
        if (nameEnd < nameStart) {
            return this;
        }
        if (filePath.regionMatches(nameStart, this.getName(), 0, nameEnd - nameStart)) {
            return this;
        }
        final String zoneName = filePath.substring(nameStart, nameEnd);
        final Registry<ZoneFileContext> zoneRegistry = this.getParentContext().getZones();
        if (!zoneRegistry.contains(zoneName)) {
            return this;
        }
        return zoneRegistry.get(zoneName);
    }
    
    @Nonnull
    protected BiomeFileContext createBiome(final String name, final Path path, final BiomeFileContext.Type type) {
        return this.createBiome(this.getParentContext().nextBiomeId(), name, path, type);
    }
    
    @Nonnull
    protected BiomeFileContext createBiome(final int id, final String name, final Path path, final BiomeFileContext.Type type) {
        return new BiomeFileContext(this.getParentContext().updateBiomeId(id), name, path, type, this);
    }
    
    public interface Constants
    {
        public static final String ZONE_PREFIX = "Zones.";
    }
}
