package top.r3944realms.docchecktoolrefactored.ui.extend;

import javafx.scene.control.SpinnerValueFactory;
import lombok.Setter;

@Setter
public class LongSpinnerValueFactory extends SpinnerValueFactory<Long> {

    public LongSpinnerValueFactory(long min, long max, long initialValue) {
        setMin(min);
        setMax(max);
        setValue(initialValue);

        setConverter(new javafx.util.StringConverter<>() {
            @Override
            public String toString(Long object) {
                return object.toString();
            }

            @Override
            public Long fromString(String string) {
                try {
                    return Long.parseLong(string);
                } catch (NumberFormatException e) {
                    return getValue();
                }
            }
        });
    }

    private long min;
    private long max;

    @Override
    public void decrement(int steps) {
        long newValue = getValue() - steps;
        if (newValue < min) newValue = min;
        setValue(newValue);
    }

    @Override
    public void increment(int steps) {
        long newValue = getValue() + steps;
        if (newValue > max) newValue = max;
        setValue(newValue);
    }
}

