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

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

import java.util.HashMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import com.google.gson.JsonArray;
import java.util.Map;
import com.hypixel.hytale.server.worldgen.util.condition.HashSetIntCondition;
import java.util.Objects;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import com.hypixel.hytale.procedurallib.condition.ConstantIntCondition;
import com.hypixel.hytale.procedurallib.condition.IIntCondition;
import com.hypixel.hytale.procedurallib.json.DoubleThresholdJsonLoader;
import com.hypixel.hytale.procedurallib.condition.IDoubleThreshold;
import javax.annotation.Nullable;
import com.hypixel.hytale.procedurallib.json.NoisePropertyJsonLoader;
import com.hypixel.hytale.procedurallib.property.NoiseProperty;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.biome.Biome;
import com.hypixel.hytale.server.worldgen.loader.context.BiomeFileContext;
import com.hypixel.hytale.server.worldgen.biome.CustomBiomeGenerator;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class CustomBiomeGeneratorJsonLoader extends JsonLoader<SeedStringResource, CustomBiomeGenerator>
{
    protected final BiomeFileContext biomeContext;
    protected final Biome[] tileBiomes;
    
    public CustomBiomeGeneratorJsonLoader(@Nonnull final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final BiomeFileContext biomeContext, final Biome[] tileBiomes) {
        super(seed.append(".CustomBiomeGenerator"), dataFolder, json);
        this.biomeContext = biomeContext;
        this.tileBiomes = tileBiomes;
    }
    
    @Nonnull
    @Override
    public CustomBiomeGenerator load() {
        return new CustomBiomeGenerator(this.loadNoiseProperty(), this.loadNoiseThreshold(), this.loadBiomeMask(), this.loadPriority());
    }
    
    @Nullable
    protected NoiseProperty loadNoiseProperty() {
        return new NoisePropertyJsonLoader(this.seed, this.dataFolder, this.get("NoiseMask")).load();
    }
    
    @Nonnull
    protected IDoubleThreshold loadNoiseThreshold() {
        return new DoubleThresholdJsonLoader(this.seed, this.dataFolder, this.get("NoiseMask").getAsJsonObject().get("Threshold")).load();
    }
    
    @Nonnull
    protected IIntCondition loadBiomeMask() {
        IIntCondition biomeMask = ConstantIntCondition.DEFAULT_TRUE;
        if (this.has("BiomeMask")) {
            final Map<String, Biome> nameBiomeMap = this.generateNameBiomeMapping();
            final JsonArray biomeMaskArray = this.get("BiomeMask").getAsJsonArray();
            final IntSet biomeSet = new IntOpenHashSet(biomeMaskArray.size());
            for (int i = 0; i < biomeMaskArray.size(); ++i) {
                final String biomeName = biomeMaskArray.get(i).getAsString();
                final Biome biome = nameBiomeMap.get(biomeName);
                Objects.requireNonNull(biome, biomeName);
                biomeSet.add(biome.getId());
            }
            biomeMask = new HashSetIntCondition(biomeSet);
        }
        return biomeMask;
    }
    
    @Nonnull
    protected Map<String, Biome> generateNameBiomeMapping() {
        final Map<String, Biome> map = new HashMap<String, Biome>();
        for (final Biome biome : this.tileBiomes) {
            map.put(biome.getName(), biome);
        }
        return map;
    }
    
    protected int loadPriority() {
        return this.has("Priority") ? this.get("Priority").getAsInt() : 0;
    }
    
    public interface Constants
    {
        public static final String KEY_NOISE_MASK = "NoiseMask";
        public static final String KEY_BIOME_MASK = "BiomeMask";
        public static final String KEY_PRIORITY = "Priority";
        public static final String ERROR_BIOME_ERROR_MASK = "Could not find tile biome \"%s\" for biome mask. Typo or disabled tile biome?";
    }
}
