package UE20_290425_Threads2;

import javafx.application.Platform;
import javafx.scene.control.ProgressBar;

import static UE20_290425_Threads2.Pferderennen.textArea;

public class Horse extends ProgressBar implements Runnable {
    private static final double finish_line = 1_000_000;
    private static final double maxSleepTime = 1, maxStepSize = 10_000;
    private final int index;
    private double pos = 0;

    public Horse(double var, int index) {
        super(var);
        this.index = index;
    }

    public void setPos(double pos) {
        this.pos = pos;
    }

    @Override
    public void run() {
        while (pos < finish_line) {
            pos += (int) (Math.random() * maxStepSize + 1);
            Platform.runLater(() -> this.setProgress(Math.min(pos / finish_line, 1.0)));
            try {
                Thread.sleep((int) (Math.random() * maxSleepTime + 1));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        Platform.runLater(() -> textArea.appendText("Horse/Thread " + index + " ist im Ziel\n"));
    }
}
