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

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

import javax.annotation.Nonnull;
import com.hypixel.hytale.server.worldgen.biome.CustomBiomeGenerator;
import java.util.Iterator;
import java.util.Arrays;
import com.hypixel.hytale.server.worldgen.loader.biome.CustomBiomeJsonLoader;
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.BiomeFileContext;
import java.util.Map;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.biome.Biome;
import com.hypixel.hytale.server.worldgen.loader.context.ZoneFileContext;
import java.util.Comparator;
import com.hypixel.hytale.server.worldgen.biome.CustomBiome;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class ZoneCustomBiomesJsonLoader extends JsonLoader<SeedStringResource, CustomBiome[]>
{
    private static final Comparator<CustomBiome> PRIORITY_SORTER;
    protected final ZoneFileContext zoneContext;
    protected final Biome[] tileBiomes;
    
    public ZoneCustomBiomesJsonLoader(final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final ZoneFileContext zoneContext, final Biome[] tileBiomes) {
        super(seed, dataFolder, json);
        this.zoneContext = zoneContext;
        this.tileBiomes = tileBiomes;
    }
    
    @Nonnull
    @Override
    public CustomBiome[] load() {
        int index = 0;
        final CustomBiome[] biomes = new CustomBiome[this.zoneContext.getCustomBiomes().size()];
        for (final Map.Entry<String, BiomeFileContext> biomeEntry : this.zoneContext.getCustomBiomes()) {
            final BiomeFileContext biomeContext = biomeEntry.getValue();
            try (final JsonReader reader = new JsonReader(Files.newBufferedReader(biomeContext.getPath()))) {
                final JsonElement biomeJson = JsonParser.parseReader(reader);
                final CustomBiome biome = new CustomBiomeJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, biomeJson, biomeContext, this.tileBiomes).load();
                final CustomBiomeGenerator reference = biome.getCustomBiomeGenerator();
                if (reference == null) {
                    throw new NullPointerException(biomeContext.getPath().toAbsolutePath().toString());
                }
                biomes[index++] = biome;
            }
            catch (final Throwable e) {
                throw new Error(String.format("Error while loading custom biome \"%s\" from \"%s\"", biomeContext.getName(), biomeContext.getPath().toString()), e);
            }
        }
        Arrays.sort(biomes, ZoneCustomBiomesJsonLoader.PRIORITY_SORTER);
        return biomes;
    }
    
    static {
        PRIORITY_SORTER = ((o1, o2) -> Integer.compare(o2.getCustomBiomeGenerator().getPriority(), o1.getCustomBiomeGenerator().getPriority()));
    }
    
    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 custom biome \"%s\" from \"%s\"";
        public static final String ERROR_NO_CUSTOM_GENERATOR = "Could not find custom biome generator for custom biome \"%s\" at \"%s\"";
        public static final String FILE_CUSTOM_PREFIX = "Custom.";
        public static final String FILE_CUSTOM_SUFFIX = ".json";
    }
}
