package UE20_290425_Threads2;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;

public class Pferderennen extends Application {
    protected static final TextArea textArea = new TextArea();
    protected static final ArrayList<Horse> horses = new ArrayList<>();
    private static final ArrayList<Thread> threads = new ArrayList<>();
    private static boolean lock = false;

    public static void main(String[] args) {
        launch(args);
    }

    private static void startRace() {
        for (Horse horse : horses) horse.setPos(0);
        threads.clear();
        for (Horse horse : horses) {
            Thread thread = new Thread(horse);
            threads.add(thread);
            thread.start();
        }
        new Thread(() -> {
            for (Thread t : threads)
                try {
                    t.join();
                } catch (InterruptedException ignored) {
                }
            Platform.runLater(() -> {
                textArea.appendText("\nFinish!\n");
                lock = false;
            });
        }).start();
    }

    @Override
    public void start(Stage stage) throws Exception {
        BorderPane backPane = new BorderPane();

        Label titel = new Label("Pferderennen - mit Threads");
        backPane.setTop(titel);

        VBox button_box = new VBox(10);
        Button button_plus = new Button("+");
        Button button_minus = new Button("-");
        button_box.getChildren().addAll(button_plus, button_minus);
        backPane.setLeft(button_box);

        Button button_start = new Button("Start");
        VBox start_box = new VBox(button_start);
        start_box.setPadding(new Insets(10));
        backPane.setRight(start_box);

        textArea.setPrefRowCount(10);
        textArea.setEditable(false);
        backPane.setBottom(textArea);

        VBox horse_bars = new VBox(10);

        for (ProgressBar horse : horses) horse_bars.getChildren().add(horse);

        backPane.setCenter(horse_bars);

        button_plus.setOnAction(_ -> {
            if (lock) return;
            Horse horse = new Horse(0, horses.size() + 1);
            horse.setMaxWidth(Double.MAX_VALUE);
            horses.add(horse);
            horse_bars.getChildren().add(horse);
            textArea.appendText("Adding " + horses.size() + " Bars\n");
        });
        button_minus.setOnAction(_ -> {
            if (lock) return;
            if (!horses.isEmpty()) {
                horse_bars.getChildren().remove(horses.removeLast());
                textArea.appendText("Adding " + horses.size() + " Bars\n");
            }
        });
        button_start.setOnAction(_ -> {
            if (lock) {
                textArea.appendText("Please wait for the race to finish\n");
                return;
            }
            lock = true;
            textArea.appendText("Ergebnis:\n");
            startRace();
        });

        Scene scene = new Scene(backPane, 600, 400);
        stage.setTitle("Pferderennen");
        stage.setScene(scene);
        stage.show();
    }
}