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

package com.hypixel.hytale.procedurallib.json;

import com.google.gson.JsonArray;
import com.hypixel.hytale.procedurallib.condition.DoubleThreshold;
import com.hypixel.hytale.procedurallib.condition.DefaultDoubleThresholdCondition;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.condition.IDoubleThreshold;

public class DoubleThresholdJsonLoader<K extends SeedResource> extends JsonLoader<K, IDoubleThreshold>
{
    protected final boolean defaultValue;
    
    public DoubleThresholdJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json) {
        this(seed, dataFolder, json, true);
    }
    
    public DoubleThresholdJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json, final boolean defaultValue) {
        super(seed.append(".DoubleThreshold"), dataFolder, json);
        this.defaultValue = defaultValue;
    }
    
    @Nonnull
    @Override
    public IDoubleThreshold load() {
        if (this.json == null || this.json.isJsonNull()) {
            return this.defaultValue ? DefaultDoubleThresholdCondition.DEFAULT_TRUE : DefaultDoubleThresholdCondition.DEFAULT_FALSE;
        }
        if (this.json.isJsonPrimitive()) {
            final double value = this.json.getAsDouble();
            return new DoubleThreshold.Single(0.0, value);
        }
        final JsonArray jsonArray = this.json.getAsJsonArray();
        if (jsonArray.size() <= 0) {
            throw new IllegalArgumentException("Threshold array must contain at least one entry!");
        }
        if (jsonArray.get(0).isJsonArray()) {
            final DoubleThreshold.Single[] entries = new DoubleThreshold.Single[jsonArray.size()];
            for (int i = 0; i < entries.length; ++i) {
                final JsonArray jsonArrayEntry = jsonArray.get(i).getAsJsonArray();
                if (jsonArrayEntry.size() != 2) {
                    throw new IllegalArgumentException("Threshold array entries must have 2 numbers for lower/upper limit!");
                }
                entries[i] = new DoubleThreshold.Single(jsonArrayEntry.get(0).getAsDouble(), jsonArrayEntry.get(1).getAsDouble());
            }
            return new DoubleThreshold.Multiple(entries);
        }
        if (jsonArray.size() != 2) {
            throw new IllegalArgumentException("Threshold array entries must have 2 numbers for lower/upper limit!");
        }
        return new DoubleThreshold.Single(jsonArray.get(0).getAsDouble(), jsonArray.get(1).getAsDouble());
    }
    
    public interface Constants
    {
        public static final String ERROR_NO_ENTRY = "Threshold array must contain at least one entry!";
        public static final String ERROR_THRESHOLD_SIZE = "Threshold array entries must have 2 numbers for lower/upper limit!";
    }
}
