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

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

import com.hypixel.hytale.server.worldgen.biome.BiomeInterpolation;
import com.hypixel.hytale.procedurallib.json.NoisePropertyJsonLoader;
import com.hypixel.hytale.procedurallib.property.NoiseProperty;
import com.hypixel.hytale.server.worldgen.loader.container.WaterContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.WaterContainer;
import com.hypixel.hytale.server.worldgen.loader.container.EnvironmentContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.EnvironmentContainer;
import com.hypixel.hytale.server.worldgen.loader.container.TintContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.TintContainer;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.worldgen.loader.container.PrefabContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.PrefabContainer;
import com.hypixel.hytale.server.worldgen.loader.container.LayerContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.LayerContainer;
import com.hypixel.hytale.server.worldgen.loader.container.FadeContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.FadeContainer;
import com.hypixel.hytale.server.worldgen.loader.container.CoverContainerJsonLoader;
import com.hypixel.hytale.server.worldgen.container.CoverContainer;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.json.HeightThresholdInterpreterJsonLoader;
import com.hypixel.hytale.procedurallib.condition.IHeightThresholdInterpreter;
import com.hypixel.hytale.server.worldgen.loader.context.ZoneFileContext;
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.FileLoadingContext;
import com.hypixel.hytale.server.worldgen.loader.context.BiomeFileContext;
import java.util.regex.Pattern;
import com.hypixel.hytale.server.worldgen.biome.Biome;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public abstract class BiomeJsonLoader extends JsonLoader<SeedStringResource, Biome>
{
    private static final Pattern COLOR_PREFIX_PATTERN;
    protected final BiomeFileContext biomeContext;
    protected final FileLoadingContext fileContext;
    
    public BiomeJsonLoader(final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final BiomeFileContext biomeContext) {
        super(seed, dataFolder, json);
        this.biomeContext = biomeContext;
        this.fileContext = biomeContext.getParentContext().getParentContext();
    }
    
    @Nonnull
    protected IHeightThresholdInterpreter loadTerrainHeightThreshold() {
        try {
            return new HeightThresholdInterpreterJsonLoader(this.seed, this.dataFolder, this.get("TerrainHeightThreshold"), 320).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load height threshold container", e);
        }
    }
    
    @Nonnull
    protected CoverContainer loadCoverContainer() {
        try {
            return new CoverContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Covers")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load cover container", e);
        }
    }
    
    @Nonnull
    protected FadeContainer loadFadeContainer() {
        try {
            return new FadeContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Fade")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load fade container", e);
        }
    }
    
    @Nonnull
    protected LayerContainer loadLayerContainers() {
        try {
            if (!this.has("Layers")) {
                throw new IllegalArgumentException("LayerContainer is not defined in Biome!");
            }
            return new LayerContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Layers")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load layer container", e);
        }
    }
    
    @Nullable
    protected PrefabContainer loadPrefabContainer() {
        try {
            PrefabContainer prefabContainer = null;
            if (this.has("Prefabs")) {
                prefabContainer = new PrefabContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Prefabs"), this.fileContext).load();
            }
            return prefabContainer;
        }
        catch (final Throwable e) {
            throw new Error("Failed to load prefab container", e);
        }
    }
    
    @Nonnull
    protected TintContainer loadTintContainer() {
        try {
            return new TintContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Tint")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load tint container", e);
        }
    }
    
    @Nonnull
    protected EnvironmentContainer loadEnvironmentContainer() {
        try {
            return new EnvironmentContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Environment")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load environment container", e);
        }
    }
    
    @Nonnull
    protected WaterContainer loadWaterContainer() {
        try {
            return new WaterContainerJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Water")).load();
        }
        catch (final Throwable e) {
            throw new Error("Failed to load water container", e);
        }
    }
    
    @Nullable
    protected NoiseProperty loadHeightmapNoise() {
        NoiseProperty heightmapNoise = null;
        if (this.has("HeightmapNoise")) {
            heightmapNoise = new NoisePropertyJsonLoader(this.seed, this.dataFolder, this.get("HeightmapNoise")).load();
        }
        return heightmapNoise;
    }
    
    protected int loadColor() {
        int rgb = 16711680;
        if (this.has("MapColor")) {
            rgb = getColor(this.get("MapColor").getAsString());
        }
        return rgb;
    }
    
    @Nullable
    protected BiomeInterpolation loadInterpolation() {
        BiomeInterpolation interpolation = BiomeInterpolation.DEFAULT;
        if (this.has("Interpolation")) {
            interpolation = new BiomeInterpolationJsonLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, this.get("Interpolation"), this.biomeContext.getParentContext()).load();
        }
        return interpolation;
    }
    
    protected static int getColor(@Nonnull final String string) {
        final String tintString = BiomeJsonLoader.COLOR_PREFIX_PATTERN.matcher(string).replaceFirst("");
        return Integer.parseInt(tintString, 16);
    }
    
    static {
        COLOR_PREFIX_PATTERN = Pattern.compile("0x|#");
    }
    
    public interface Constants
    {
        public static final String KEY_TERRAIN_HEIGHT_THRESHOLD = "TerrainHeightThreshold";
        public static final String KEY_COVERS = "Covers";
        public static final String KEY_LAYERS = "Layers";
        public static final String KEY_PREFABS = "Prefabs";
        public static final String KEY_FADE = "Fade";
        public static final String KEY_TINT = "Tint";
        public static final String KEY_ENVIRONMENT = "Environment";
        public static final String KEY_WATER = "Water";
        public static final String KEY_HEIGHTMAP_NOISE = "HeightmapNoise";
        public static final String KEY_INTERPOLATION = "Interpolation";
        public static final String KEY_MAP_COLOR = "MapColor";
        public static final String ERROR_NO_LAYER_CONTAINER = "LayerContainer is not defined in Biome!";
        public static final String ERROR_COVER_CONTAINER = "Failed to load cover container";
        public static final String ERROR_HEIGHT_CONTAINER = "Failed to load height threshold container";
        public static final String ERROR_LAYER_CONTAINER = "Failed to load layer container";
        public static final String ERROR_WATER_CONTAINER = "Failed to load water container";
        public static final String ERROR_TINT_CONTAINER = "Failed to load tint container";
        public static final String ERROR_FADE_CONTAINER = "Failed to load fade container";
        public static final String ERROR_ENVIRONMENT_CONTAINER = "Failed to load environment container";
        public static final String ERROR_PREFAB_CONTAINER = "Failed to load prefab container";
    }
}
