package someStuff;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;

public class CSMA extends Application {
	//Creates a text field for output
	 protected final TextArea textArea = new TextArea();
	GraphicsContext gc;//Global graphic content value
	//Address address;//Unused
	int stepCount = 0;//Setting step count

	 public static void main(String[] args) {
	        launch(args);
	    }
	 
	    @Override
	    public void start(Stage primaryStage) {
	        primaryStage.setTitle("CSMA/CD Simulation");
	        Group root = new Group();
	        
	        Canvas canvas = new Canvas(500, 400);//Creates GUI screen area
	        GraphicsContext gc = canvas.getGraphicsContext2D();
	        drawShapes(gc);//Calls drawShapes function
	        root.getChildren().add(canvas);
	        primaryStage.setScene(new Scene(root));
	        primaryStage.show();//Displays GUI
	        Button btn = new Button();//Creates step button
	        btn.setLayoutX(100);
	        btn.setLayoutY(80);
	       // root.getChildren().add(text2);
	        btn.setText("Step");
	        root.getChildren().add(btn);//Adds button to screen
	        
	        //Following lines output text per step
	        Text text2 = new Text(25, 50, "node 1 and 2 transmitting signals");
	        text2.setFill(Color.DARKBLUE);
	        text2.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        Text text3 = new Text(25, 50, "Node signals collide!");
	        text3.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        text3.setFill(Color.RED);
	        Text text4 = new Text(25, 50, "Nodes waiting");
	        text4.setFill(Color.DARKBLUE);
	        text4.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        Text text5 = new Text(25, 50, "Node 1 transmits & Completes");
	        text5.setFill(Color.DARKBLUE);
	        text5.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        Text text6 = new Text(25, 50, "Node 2 transmits & Completes");
	        text6.setFill(Color.DARKBLUE);
	        text6.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        Text text7 = new Text(25, 50, "Transmission Complete!");
	        text7.setFill(Color.GREEN);
	        text7.setFont(Font.font(java.awt.Font.SANS_SERIF, 30));
	        
	       // Scene derp = new Scene(root);
	        btn.setOnAction(new EventHandler<ActionEvent>() {
	            @Override
	            public void handle(ActionEvent event) {
	            		
	                     stepCount++;
	                     drawShapes(gc);
	                     if (stepCount == 2){
	                    	 root.getChildren().add(text2);
	                     }else if(stepCount == 3){
	                    	 root.getChildren().remove(text2);
	                    	 root.getChildren().add(text3);
	                     }else if(stepCount == 4){
	                    	 root.getChildren().remove(text3);
	                    	 root.getChildren().add(text4);           	 	                    	 
	                     }else if(stepCount == 5){
	                    	 root.getChildren().remove(text4);
	                    	 root.getChildren().add(text5);
	                     }else if(stepCount == 6){
	                    	 root.getChildren().remove(text5);
	                    	 root.getChildren().add(text6);	                    	
	                     }else if(stepCount == 7){
	                    	 root.getChildren().remove(text6);
	                    	 root.getChildren().add(text7);	                    	 
	                     }else if(stepCount == 8){
	                    	 Platform.exit();
	                     }

	     	           

	                     
	                     //step(stepCount);

	                 }

	        });
	       // primaryStage.setScene(derp);
	        //primaryStage.show();

	        

	    
	    }

//		Draw Shapes for GUI
	    private void drawShapes(GraphicsContext gc) {
	        gc.setFill(Color.ORANGE);
	        gc.setStroke(Color.BLUE);
	        gc.setLineWidth(5);

	        gc.strokeOval(60, 90, 30, 30);
	        gc.strokeOval(60, 180, 30, 30);
	       
	        gc.fillPolygon(new double[]{260, 300, 260, 300},
	                       new double[]{190, 190, 220, 220}, 4);
	        
	        //Draws shapes per step
	        if (stepCount == 1) {
	            
	            gc.strokeLine(100, 120, 250, 200);
	            drawShapes(gc);
	            Text text2 = new Text(25, 50, "test");
	        	text2.setTextAlignment(TextAlignment.RIGHT);

	        }else if (stepCount == 2){
	        	gc.strokeLine(100, 190, 250, 200);
	        	
	        }else if(stepCount == 3){
	        	gc.setStroke(Color.RED);
	        	gc.strokeLine(100, 190, 250, 200);
	        	gc.strokeLine(100, 120, 250, 200);
	        }else if(stepCount == 4){
	        	gc.setStroke(Color.WHITE);
	        	gc.setLineWidth(9);
	        	gc.strokeLine(100, 190, 250, 200);
	        	gc.strokeLine(100, 120, 250, 200);
	        	
	        }else if(stepCount == 5){
	        	gc.setStroke(Color.BLUE);
	        	gc.strokeLine(100, 190, 250, 200);
	        	
	        }else if(stepCount == 6){
	        	gc.setStroke(Color.BLUE);
	        	gc.strokeLine(100, 190, 250, 200);
	        	gc.strokeLine(100, 120, 250, 200);
	        	
	        }else if(stepCount == 7){
	        	gc.setStroke(Color.GREEN);
	        	gc.strokeLine(100, 190, 250, 200);
	        	gc.strokeLine(100, 120, 250, 200);
	        }

	       
	        
	    }

	   
	    }