package spirale;

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.control.*;
import javafx.scene.canvas.*;
import javafx.stage.Stage;
import javafx.geometry.Pos;

public class App extends Application {

	private BorderPane root;
	private Button next;
	private HBox top;
	private Generator gen;
	private Canvas can;

	@Override
	public void start(Stage primaryStage){
		final int[] last_pos_x = {400};
		final int[] last_pos_y = { 400 };
		final int[] last_pos_state = { 0 };
		gen = new Fibonacci();
		can = new Canvas(800,800);
		GraphicsContext con = can.getGraphicsContext2D();
		con.setFill(Color.BLUE);

		root = new BorderPane();

		top = new HBox();
		next = new Button("Next");
		next.setOnAction((x) -> {
			int curr = gen.next();
			System.out.println(curr);
			int next_pos_x = last_pos_x[0], next_pos_y = last_pos_y[0];
			switch (last_pos_state[0]) {
				case 0: 
					next_pos_x = last_pos_x[0];
					next_pos_y = last_pos_y[0] + curr;
					break;
				case 1: 
					next_pos_x = last_pos_x[0] + curr;
					next_pos_y = last_pos_y[0];
					break;
				case 2: 
					next_pos_x = last_pos_x[0];
					next_pos_y = last_pos_y[0] - curr;
					break;
				case 3: 
					next_pos_x = last_pos_x[0] - curr;
					next_pos_y = last_pos_y[0];
					break;
			}
			last_pos_state[0]++;
			last_pos_state[0] = last_pos_state[0] % 4;
			con.strokeLine(last_pos_x[0], last_pos_y[0], next_pos_x, next_pos_y);
			last_pos_x[0] = next_pos_x;
			last_pos_y[0] = next_pos_y;
		});
		top.getChildren().add(next);
		top.setAlignment(Pos.CENTER);

		root.setTop(top);
		root.setCenter(can);

		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.setTitle("Mon paneau");
		primaryStage.show();
	}

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