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

package com.hypixel.hytale.procedurallib.json;

import com.hypixel.hytale.procedurallib.condition.DoubleThresholdCondition;
import com.hypixel.hytale.procedurallib.condition.SingleDoubleCondition;
import com.hypixel.hytale.procedurallib.condition.DefaultDoubleCondition;
import java.util.Objects;
import com.google.gson.JsonElement;
import java.nio.file.Path;
import javax.annotation.Nonnull;
import com.hypixel.hytale.procedurallib.condition.IDoubleCondition;

public class DoubleConditionJsonLoader<K extends SeedResource> extends JsonLoader<K, IDoubleCondition>
{
    protected final Boolean defaultValue;
    
    public DoubleConditionJsonLoader(@Nonnull final SeedString<K> seed, final Path dataFolder, final JsonElement json) {
        this(seed.append(".DoubleCondition"), dataFolder, json, true);
    }
    
    public DoubleConditionJsonLoader(final SeedString<K> seed, final Path dataFolder, final JsonElement json, final Boolean defaultValue) {
        super(seed, dataFolder, json);
        this.defaultValue = defaultValue;
    }
    
    @Nonnull
    @Override
    public IDoubleCondition load() {
        if (this.json == null || this.json.isJsonNull()) {
            Objects.requireNonNull(this.defaultValue, "Default value is not set and condition is not defined.");
            return this.defaultValue ? DefaultDoubleCondition.DEFAULT_TRUE : DefaultDoubleCondition.DEFAULT_FALSE;
        }
        if (this.json.isJsonPrimitive() || (this.json.isJsonArray() && this.json.getAsJsonArray().size() == 1 && this.json.getAsJsonArray().get(0).isJsonPrimitive())) {
            final double limit = this.json.getAsDouble();
            return new SingleDoubleCondition(limit);
        }
        if (this.json.isJsonArray()) {
            return new DoubleThresholdCondition(new DoubleThresholdJsonLoader(this.seed, this.dataFolder, this.json).load());
        }
        throw new Error(String.format("Failed to load \"%s\" as DoubleCondition", this.json));
    }
    
    public interface Constants
    {
        public static final String ERROR_NO_DEFAULT = "Default value is not set and condition is not defined.";
        public static final String ERROR_FAILED = "Failed to load \"%s\" as DoubleCondition";
    }
}
