package com.tiedup.remake.v2.bondage.component;

import com.google.gson.JsonObject;

/**
 * Component: gagging behavior for data-driven items.
 *
 * JSON config: {@code "gagging": {"comprehension": 0.2, "range": 10.0}}
 */
public class GaggingComponent implements IItemComponent {

    private final double comprehension;
    private final double range;

    private GaggingComponent(double comprehension, double range) {
        this.comprehension = comprehension;
        this.range = range;
    }

    public static IItemComponent fromJson(JsonObject config) {
        double comprehension = 0.2;
        double range = 10.0;
        if (config != null) {
            if (config.has("comprehension")) {
                comprehension = config.get("comprehension").getAsDouble();
            }
            if (config.has("range")) {
                range = config.get("range").getAsDouble();
            }
        }
        comprehension = Math.max(0.0, Math.min(1.0, comprehension));
        range = Math.max(0.0, range);
        return new GaggingComponent(comprehension, range);
    }

    /** How much of the gagged speech is comprehensible (0.0 = nothing, 1.0 = full). */
    public double getComprehension() {
        return comprehension;
    }

    /** Maximum range in blocks where muffled speech can be heard. */
    public double getRange() {
        return range;
    }
}
