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

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

import com.google.gson.JsonElement;
import java.util.Iterator;
import com.hypixel.hytale.server.worldgen.loader.context.FileContext;
import com.hypixel.hytale.server.worldgen.loader.zone.ZoneJsonLoader;
import com.google.gson.JsonParser;
import java.io.Reader;
import com.google.gson.stream.JsonReader;
import java.nio.file.Files;
import com.hypixel.hytale.server.worldgen.loader.context.ZoneFileContext;
import java.util.Map;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.loader.context.FileLoadingContext;
import com.hypixel.hytale.server.worldgen.zone.Zone;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.Loader;

public class ZonesJsonLoader extends Loader<SeedStringResource, Zone[]>
{
    protected final FileLoadingContext loadingContext;
    
    public ZonesJsonLoader(@Nonnull final SeedString<SeedStringResource> seed, final Path dataFolder, final FileLoadingContext loadingContext) {
        super(seed.append(".Zones"), dataFolder);
        this.loadingContext = loadingContext;
    }
    
    @Nonnull
    @Override
    public Zone[] load() {
        final FileContext.Registry<ZoneFileContext> zoneRegistry = this.loadingContext.getZones();
        int index = 0;
        final Zone[] zones = new Zone[zoneRegistry.size()];
        for (final Map.Entry<String, ZoneFileContext> zoneEntry : zoneRegistry) {
            final ZoneFileContext zoneContext = zoneEntry.getValue();
            try (final JsonReader reader = new JsonReader(Files.newBufferedReader(zoneContext.getPath().resolve("Zone.json")))) {
                final JsonElement zoneJson = JsonParser.parseReader(reader);
                final Zone zone = new ZoneJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, zoneJson, zoneContext).load();
                zones[index++] = zone;
            }
            catch (final Throwable e) {
                throw new Error(String.format("Error while loading zone \"%s\" for world generator from file.", zoneContext.getPath().toString()), e);
            }
        }
        return zones;
    }
    
    public interface Constants
    {
        public static final String PATH_ZONES = "Zones";
        public static final String FILE_ZONE_MAIN_FILE = "Zone.json";
        public static final String ERROR_LOADING_ZONE = "Error while loading zone \"%s\" for world generator from file.";
    }
}
