package com.livingworld.modules.water;

/**
 * Per-region water state tracked by {@link WaterModule}.
 *
 * <p>All values are clamped to [0, 100].
 *
 * <ul>
 *   <li><b>waterAvailability</b> – how much fresh water exists in this region
 *   <li><b>purificationCapacity</b> – ecosystem's ability to filter water;
 *       driven by vegetation cover
 *   <li><b>droughtRisk</b> – likelihood of water shortage conditions
 *   <li><b>floodRisk</b> – likelihood of waterlogging or runoff damage
 * </ul>
 */
public final class WaterRegionData {

    private static final double MIN = 0.0;
    private static final double MAX = 100.0;

    private double waterAvailability;
    private double purificationCapacity;
    private double droughtRisk;
    private double floodRisk;

    public WaterRegionData(
            double waterAvailability,
            double purificationCapacity,
            double droughtRisk,
            double floodRisk) {
        this.waterAvailability   = clamp(waterAvailability);
        this.purificationCapacity = clamp(purificationCapacity);
        this.droughtRisk         = clamp(droughtRisk);
        this.floodRisk           = clamp(floodRisk);
    }

    /** Returns a healthy default water profile. */
    public static WaterRegionData defaults() {
        return new WaterRegionData(60.0, 50.0, 10.0, 10.0);
    }

    // ------------------------------------------------------------------
    // Getters
    // ------------------------------------------------------------------

    public double getWaterAvailability()   { return waterAvailability; }
    public double getPurificationCapacity() { return purificationCapacity; }
    public double getDroughtRisk()          { return droughtRisk; }
    public double getFloodRisk()            { return floodRisk; }

    // ------------------------------------------------------------------
    // Setters (clamp on write)
    // ------------------------------------------------------------------

    public void setWaterAvailability(double v)   { waterAvailability   = clamp(v); }
    public void setPurificationCapacity(double v) { purificationCapacity = clamp(v); }
    public void setDroughtRisk(double v)          { droughtRisk         = clamp(v); }
    public void setFloodRisk(double v)            { floodRisk           = clamp(v); }

    /** Clamps all fields to [0, 100]. */
    public void normalize() {
        waterAvailability   = clamp(waterAvailability);
        purificationCapacity = clamp(purificationCapacity);
        droughtRisk         = clamp(droughtRisk);
        floodRisk           = clamp(floodRisk);
    }

    /** Returns an independent copy. */
    public WaterRegionData copy() {
        return new WaterRegionData(waterAvailability, purificationCapacity, droughtRisk, floodRisk);
    }

    // ------------------------------------------------------------------

    private static double clamp(double v) {
        return Math.min(MAX, Math.max(MIN, v));
    }

    @Override
    public String toString() {
        return "WaterRegionData{"
                + "availability=" + waterAvailability
                + ", purification=" + purificationCapacity
                + ", drought=" + droughtRisk
                + ", flood=" + floodRisk + "}";
    }
}
