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

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

import com.hypixel.hytale.server.worldgen.loader.biome.TileBiomeJsonLoader;
import com.google.gson.JsonParser;
import java.io.Reader;
import com.google.gson.stream.JsonReader;
import java.nio.file.Files;
import javax.annotation.Nonnull;
import java.util.Iterator;
import com.hypixel.hytale.server.worldgen.loader.context.BiomeFileContext;
import java.util.Map;
import com.hypixel.hytale.common.map.WeightedMap;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.loader.context.ZoneFileContext;
import com.hypixel.hytale.server.worldgen.biome.TileBiome;
import com.hypixel.hytale.common.map.IWeightedMap;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class ZoneBiomesJsonLoader extends JsonLoader<SeedStringResource, IWeightedMap<TileBiome>>
{
    protected final ZoneFileContext zoneContext;
    
    public ZoneBiomesJsonLoader(final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final ZoneFileContext zone) {
        super(seed, dataFolder, json);
        this.zoneContext = zone;
    }
    
    @Override
    public IWeightedMap<TileBiome> load() {
        final WeightedMap.Builder<TileBiome> builder = WeightedMap.builder(TileBiome.EMPTY_ARRAY);
        for (final Map.Entry<String, BiomeFileContext> biomeEntry : this.zoneContext.getTileBiomes()) {
            final TileBiome biome = this.loadBiome(biomeEntry.getValue());
            builder.put(biome, biome.getWeight());
        }
        if (builder.size() <= 0) {
            throw new IllegalArgumentException("Could not find any tile biomes for this zone!");
        }
        return builder.build();
    }
    
    @Nonnull
    protected TileBiome loadBiome(@Nonnull final BiomeFileContext biomeContext) {
        try (final JsonReader reader = new JsonReader(Files.newBufferedReader(biomeContext.getPath()))) {
            final JsonElement biomeJson = JsonParser.parseReader(reader);
            return new TileBiomeJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, biomeJson, biomeContext).load();
        }
        catch (final Throwable e) {
            throw new Error(String.format("Error while loading tile biome \"%s\" from \"%s\"", biomeContext.getName(), biomeContext.getPath().toString()), e);
        }
    }
    
    public interface Constants
    {
        public static final String ERROR_BIOME_FILES_NULL = "Biome files error occured.";
        public static final String ERROR_BIOME_FAILED = "Error while loading tile biome \"%s\" from \"%s\"";
        public static final String ERROR_NO_TILE_BIOMES = "Could not find any tile biomes for this zone!";
        public static final String FILE_TILE_PREFIX = "Tile.";
        public static final String FILE_TILE_SUFFIX = ".json";
    }
}
