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

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

import com.hypixel.hytale.procedurallib.json.NoiseMaskConditionJsonLoader;
import com.hypixel.hytale.procedurallib.condition.ICoordinateCondition;
import javax.annotation.Nullable;
import com.hypixel.hytale.procedurallib.json.NoisePropertyJsonLoader;
import com.hypixel.hytale.procedurallib.property.NoiseProperty;
import com.hypixel.hytale.server.core.asset.type.environment.config.Environment;
import com.hypixel.hytale.common.map.WeightedMap;
import com.hypixel.hytale.common.util.ArrayUtil;
import com.hypixel.hytale.common.map.IWeightedMap;
import com.hypixel.hytale.server.worldgen.util.ConstantNoiseProperty;
import com.google.gson.JsonArray;
import javax.annotation.Nonnull;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import com.hypixel.hytale.procedurallib.json.SeedString;
import com.hypixel.hytale.server.worldgen.container.EnvironmentContainer;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class EnvironmentContainerJsonLoader extends JsonLoader<SeedStringResource, EnvironmentContainer>
{
    public EnvironmentContainerJsonLoader(final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json) {
        super(seed, dataFolder, json);
    }
    
    @Nonnull
    @Override
    public EnvironmentContainer load() {
        return new EnvironmentContainer(this.loadDefault(), this.loadEntries());
    }
    
    @Nonnull
    protected EnvironmentContainer.DefaultEnvironmentContainerEntry loadDefault() {
        JsonElement element;
        if (this.json == null || this.json.isJsonNull() || this.json.isJsonArray()) {
            element = null;
        }
        else if (this.json.isJsonObject() && this.has("Default")) {
            element = this.get("Default");
        }
        else if (this.json.isJsonPrimitive() || this.json.isJsonObject()) {
            element = this.json;
        }
        else {
            element = null;
        }
        return new DefaultEnvironmentContainerEntryLoader((SeedString<SeedStringResource>)this.seed, this.dataFolder, element).load();
    }
    
    @Nonnull
    protected EnvironmentContainer.EnvironmentContainerEntry[] loadEntries() {
        if (this.json == null || !this.json.isJsonObject() || !this.has("Entries")) {
            return EnvironmentContainer.EnvironmentContainerEntry.EMPTY_ARRAY;
        }
        final JsonArray arr = this.get("Entries").getAsJsonArray();
        if (arr.isEmpty()) {
            return EnvironmentContainer.EnvironmentContainerEntry.EMPTY_ARRAY;
        }
        final EnvironmentContainer.EnvironmentContainerEntry[] entries = new EnvironmentContainer.EnvironmentContainerEntry[arr.size()];
        for (int i = 0; i < arr.size(); ++i) {
            try {
                entries[i] = new EnvironmentContainerEntryJsonLoader((SeedString<SeedStringResource>)this.seed.append(String.format("-%s", i)), this.dataFolder, arr.get(i)).load();
            }
            catch (final Throwable e) {
                throw new Error(String.format("Failed to load TintContainerEntry #%s", i), e);
            }
        }
        return entries;
    }
    
    protected static class EnvironmentContainerEntryJsonLoader extends JsonLoader<SeedStringResource, EnvironmentContainer.EnvironmentContainerEntry>
    {
        public EnvironmentContainerEntryJsonLoader(@Nonnull final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json) {
            super(seed.append(".EnvironmentContainer"), dataFolder, json);
        }
        
        @Nonnull
        @Override
        public EnvironmentContainer.EnvironmentContainerEntry load() {
            final IWeightedMap<Integer> colorMapping = this.loadIdMapping();
            return new EnvironmentContainer.EnvironmentContainerEntry(colorMapping, (colorMapping.size() > 1) ? this.loadValueNoise() : ConstantNoiseProperty.DEFAULT_ZERO, this.loadMapCondition());
        }
        
        @Nonnull
        protected IWeightedMap<Integer> loadIdMapping() {
            final WeightedMap.Builder<Integer> builder = WeightedMap.builder(ArrayUtil.EMPTY_INTEGER_ARRAY);
            if (this.json == null || this.json.isJsonNull()) {
                builder.put(0, 1.0);
            }
            else if (this.json.isJsonObject()) {
                if (!this.has("Names")) {
                    throw new IllegalArgumentException("Could not find names. Keyword: Names");
                }
                final JsonElement colorsElement = this.get("Names");
                if (colorsElement.isJsonArray()) {
                    final JsonArray names = colorsElement.getAsJsonArray();
                    final JsonArray weights = this.has("Weights") ? this.get("Weights").getAsJsonArray() : null;
                    if (weights != null && weights.size() != names.size()) {
                        throw new IllegalArgumentException("Tint weights array size does not fit color array size.");
                    }
                    for (int i = 0; i < names.size(); ++i) {
                        final String key = names.get(i).getAsString();
                        final int index = Environment.getAssetMap().getIndex(key);
                        if (index == Integer.MIN_VALUE) {
                            throw new IllegalArgumentException("Unknown key! " + key);
                        }
                        final double weight = (weights == null) ? 1.0 : weights.get(i).getAsDouble();
                        builder.put(index, weight);
                    }
                }
                else {
                    final String key2 = colorsElement.getAsString();
                    final int index2 = Environment.getAssetMap().getIndex(key2);
                    if (index2 == Integer.MIN_VALUE) {
                        throw new IllegalArgumentException("Unknown key! " + key2);
                    }
                    builder.put(index2, 1.0);
                }
            }
            else {
                final String key3 = this.json.getAsString();
                final int index3 = Environment.getAssetMap().getIndex(key3);
                if (index3 == Integer.MIN_VALUE) {
                    throw new IllegalArgumentException("Unknown key! " + key3);
                }
                builder.put(index3, 1.0);
            }
            return builder.build();
        }
        
        @Nullable
        protected NoiseProperty loadValueNoise() {
            if (!this.has("Noise")) {
                throw new IllegalArgumentException("Could not find value noise. Keyword: Noise");
            }
            return new NoisePropertyJsonLoader(this.seed, this.dataFolder, this.get("Noise")).load();
        }
        
        @Nonnull
        protected ICoordinateCondition loadMapCondition() {
            return new NoiseMaskConditionJsonLoader(this.seed, this.dataFolder, this.get("NoiseMask")).load();
        }
    }
    
    protected static class DefaultEnvironmentContainerEntryLoader extends EnvironmentContainerEntryJsonLoader
    {
        public DefaultEnvironmentContainerEntryLoader(@Nonnull final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json) {
            super(seed, dataFolder, json);
        }
        
        @Nonnull
        @Override
        public EnvironmentContainer.DefaultEnvironmentContainerEntry load() {
            IWeightedMap<Integer> colorMapping;
            if (this.json == null || this.json.isJsonNull()) {
                final WeightedMap.Builder<Integer> builder = WeightedMap.builder(ArrayUtil.EMPTY_INTEGER_ARRAY);
                builder.put(0, 1.0);
                colorMapping = builder.build();
            }
            else if (this.json.isJsonPrimitive()) {
                final WeightedMap.Builder<Integer> builder = WeightedMap.builder(ArrayUtil.EMPTY_INTEGER_ARRAY);
                final String key = this.json.getAsString();
                final int index = Environment.getAssetMap().getIndex(key);
                if (index == Integer.MIN_VALUE) {
                    throw new IllegalArgumentException("Unknown key! " + key);
                }
                builder.put(index, 1.0);
                colorMapping = builder.build();
            }
            else {
                colorMapping = this.loadIdMapping();
            }
            return new EnvironmentContainer.DefaultEnvironmentContainerEntry(colorMapping, (colorMapping.size() > 1) ? this.loadValueNoise() : ConstantNoiseProperty.DEFAULT_ZERO);
        }
    }
    
    public interface Constants
    {
        public static final String KEY_DEFAULT = "Default";
        public static final String KEY_ENTRIES = "Entries";
        public static final String KEY_NAMES = "Names";
        public static final String KEY_WEIGHTS = "Weights";
        public static final String KEY_ENTRY_NOISE = "Noise";
        public static final String KEY_NOISE_MASK = "NoiseMask";
        public static final String ERROR_NAMES_NOT_FOUND = "Could not find names. Keyword: Names";
        public static final String ERROR_WEIGHT_SIZE = "Tint weights array size does not fit color array size.";
        public static final String ERROR_NO_VALUE_NOISE = "Could not find value noise. Keyword: Noise";
        public static final String ERROR_LOADING_ENTRY = "Failed to load TintContainerEntry #%s";
        public static final String SEED_INDEX_SUFFIX = "-%s";
    }
}
