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

import com.google.gson.JsonObject;

/**
 * Component: positional adjustment for data-driven items.
 * Allows Y offset adjustment via GUI slider.
 *
 * JSON config:
 * {@code "adjustable": {"default": 0.0, "min": -4.0, "max": 4.0, "step": 0.25}}
 */
public class AdjustableComponent implements IItemComponent {

    private final float defaultValue;
    private final float min;
    private final float max;
    private final float step;

    private AdjustableComponent(float defaultValue, float min, float max, float step) {
        this.defaultValue = defaultValue;
        this.min = min;
        this.max = max;
        this.step = step;
    }

    public static IItemComponent fromJson(JsonObject config) {
        float defaultVal = 0.0f;
        float min = -4.0f;
        float max = 4.0f;
        float step = 0.25f;
        if (config != null) {
            if (config.has("default")) defaultVal = config.get("default").getAsFloat();
            if (config.has("min")) min = config.get("min").getAsFloat();
            if (config.has("max")) max = config.get("max").getAsFloat();
            if (config.has("step")) step = config.get("step").getAsFloat();
        }
        // Ensure min <= max
        if (min > max) {
            float tmp = min;
            min = max;
            max = tmp;
        }
        step = Math.max(0.01f, step);
        defaultVal = Math.max(min, Math.min(max, defaultVal));
        return new AdjustableComponent(defaultVal, min, max, step);
    }

    /** Default Y offset value. */
    public float getDefaultValue() {
        return defaultValue;
    }

    /** Minimum Y offset. */
    public float getMin() {
        return min;
    }

    /** Maximum Y offset. */
    public float getMax() {
        return max;
    }

    /** Step increment for the adjustment slider. */
    public float getStep() {
        return step;
    }
}
