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

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

import com.google.gson.JsonArray;
import com.hypixel.hytale.server.worldgen.loader.WorldGenPrefabLoader;
import com.hypixel.hytale.common.map.WeightedMap;
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.loader.WorldGenPrefabSupplier;
import com.hypixel.hytale.common.map.IWeightedMap;
import com.hypixel.hytale.server.worldgen.SeedStringResource;
import com.hypixel.hytale.procedurallib.json.JsonLoader;

public class WeightedPrefabMapJsonLoader extends JsonLoader<SeedStringResource, IWeightedMap<WorldGenPrefabSupplier>>
{
    protected final String prefabsKey;
    protected final String weightsKey;
    
    public WeightedPrefabMapJsonLoader(@Nonnull final SeedString<SeedStringResource> seed, final Path dataFolder, final JsonElement json, final String prefabsKey, final String weightsKey) {
        super(seed.append(".WeightedPrefabMap"), dataFolder, json);
        this.prefabsKey = prefabsKey;
        this.weightsKey = weightsKey;
    }
    
    @Override
    public IWeightedMap<WorldGenPrefabSupplier> load() {
        final WorldGenPrefabLoader prefabLoader = ((SeedStringResource)this.seed.get()).getLoader();
        final WeightedMap.Builder<WorldGenPrefabSupplier> builder = WeightedMap.builder(WorldGenPrefabSupplier.EMPTY_ARRAY);
        if (!this.has(this.prefabsKey)) {
            throw new IllegalArgumentException(this.prefabsKey);
        }
        final JsonElement prefabElement = this.get(this.prefabsKey);
        if (prefabElement.isJsonArray()) {
            final JsonArray prefabArray = prefabElement.getAsJsonArray();
            final JsonArray weightArray = this.has(this.weightsKey) ? this.get(this.weightsKey).getAsJsonArray() : null;
            if (weightArray != null && prefabArray.size() != weightArray.size()) {
                throw new IllegalArgumentException("Weight array size is different from prefab name array.");
            }
            for (int i = 0; i < prefabArray.size(); ++i) {
                final JsonElement prefabArrayElement = prefabArray.get(i);
                final String prefabString = prefabArrayElement.getAsString();
                final WorldGenPrefabSupplier[] suppliers = prefabLoader.get(prefabString);
                final double weight = (weightArray != null) ? (weightArray.get(i).getAsDouble() / suppliers.length) : 1.0;
                for (final WorldGenPrefabSupplier supplier : suppliers) {
                    builder.put(supplier, weight);
                }
            }
        }
        else {
            final String prefabString2 = prefabElement.getAsString();
            final WorldGenPrefabSupplier[] value;
            final WorldGenPrefabSupplier[] suppliers2 = value = prefabLoader.get(prefabString2);
            for (final WorldGenPrefabSupplier supplier2 : value) {
                builder.put(supplier2, 1.0);
            }
        }
        if (builder.size() <= 0) {
            throw new IllegalArgumentException("Prefabs are defined but could not find a valid entry!");
        }
        return builder.build();
    }
    
    public interface Constants
    {
        public static final String ERROR_ENTRY_NO_PREFAB = "Could not find prefab names. Keyword: %s";
        public static final String ERROR_ENTRY_WEIGHT_SIZE = "Weight array size is different from prefab name array.";
        public static final String ERROR_NO_PREFABS = "Prefabs are defined but could not find a valid entry!";
    }
}
