package de.frajul.particlelab.entities.particles.attributes.attributeValues;

import java.util.Random;

/**
 * Created by Julian on 02.08.2016.
 */
public class Range extends ParticleAttributeValue {

    private float lowMin;
    private float lowMax;
    private float highMin;
    private float highMax;

    public Range() {
        super(ParticleAttributeValueType.RANGE);
    }

    public Range createNormalizedInstance(Random random) {
        Range range = new Range();
        float high = createHighValue(random);
        range.setHighMax(high);
        range.setHighMin(high);
        float low = createLowValue(random);
        range.setLowMax(low);
        range.setLowMin(low);
        return range;
    }

    public float createValue(Random random, float mix) {
        if (mix == 1)
            return createHighValue(random);
        else if (mix == 0)
            return createLowValue(random);
        else {
            float highValue = createHighValue(random);
            float lowValue = createLowValue(random);
            return mix * (highValue - lowValue) + lowValue;
        }
    }

    private float createHighValue(Random random) {
        float min = highMin;
        float max = highMax;
        if (min == max)
            return min;
        return random.nextFloat() * (max - min) + min;
    }

    private float createLowValue(Random random) {
        float min = lowMin;
        float max = lowMax;
        if (min == max)
            return min;
        return random.nextFloat() * (max - min) + min;
    }

    public void setLowMin(float lowMin) {
        this.lowMin = lowMin;
    }

    public void setLowMax(float lowMax) {
        this.lowMax = lowMax;
    }

    public void setHighMin(float highMin) {
        this.highMin = highMin;
    }

    public void setHighMax(float highMax) {
        this.highMax = highMax;
    }

    private boolean isLowDifference() {
        return lowMin - lowMax != 0;
    }

    private boolean isHighDifference() {
        return highMin - highMax != 0;
    }

}
