package GUI;

import DB.MdbInterface;
import Information.Student;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    private Scene scene;
    private HBox horizontalLayout;
    private SignInBox signInBox;
    private SignOutBox signOutBox;
    private static MdbInterface mdb;
    Student student;

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("Hunter CS Tutoring Center");
        primaryStage.setMinWidth(600);
        primaryStage.setMinHeight(400);

        signInBox = new SignInBox();
        signInBox.setPadding(new Insets(50));
        signInBox.setStyle("-fx-border-style: hidden solid hidden hidden");
        signInBox.getSignInButton().setOnAction(event -> handleEmpl());

        signOutBox = new SignOutBox();
        signOutBox.setPadding(new Insets(50));
        signOutBox.getSignOutButton().setOnAction(event -> handleSignOut(signOutBox.getSelectedStudent()));

        horizontalLayout = new HBox();
        horizontalLayout.setAlignment(Pos.CENTER);
        horizontalLayout.getChildren().addAll(signInBox, signOutBox);
        HBox.setMargin(signInBox, new Insets(20, 0, 20, 0));
        HBox.setMargin(signOutBox, new Insets(20, 0, 20, 0));

        scene = new Scene(horizontalLayout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        mdb = new MdbInterface();
        launch(args);
    }

    public static MdbInterface getMdb() {
        return mdb;
    }

    private int getEmpl() {
        return Integer.parseInt(signInBox.getTextField().getText());
    }

    private void handleEmpl() {
        int emplId;
        try{
            emplId = getEmpl();
        } catch (NumberFormatException e){
            signInBox.getTextField().clear();
            PopUp.display("Error", "EMPLID must contain only numbers");
            throw new NumberFormatException("EMPLID must contain only numbers");
        }

        if (mdb.studentExists(emplId)) {
            student = mdb.getStudentByEmplId(emplId);
            signOutBox.addActiveStudent(mdb.getStudentByEmplId(emplId));
            mdb.signIn(emplId);
        } else {
            try {
                new RegistrationWindow();
                student = mdb.getStudentByEmplId(emplId);
                signOutBox.addActiveStudent(mdb.getStudentByEmplId(emplId));
                mdb.signIn(emplId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void handleSignOut(Student student) {
        new SignOutReviewWindow(student);
        signOutBox.signOutStudent(student);
    }
}

@@ -1760,32 +1760,100 @@
 urn mdb;%0A    %7D%0A%0A
+    // Throws NumberFormatException if Integer cannot parse EMPL ID%0A
     private int 
@@ -1932,32 +1932,398 @@
 Text());%0A    %7D%0A%0A
+    // Precondition: emplId is a valid integer which has already been parsed%0A    // Throws IllegalArgumentException if emplId is not 8 digits%0A    private boolean validateEmpl(int emplId)%7B%0A        if(emplId %3C 9999999 %7C%7C emplId %3E 99999999)%7B%0A            throw new IllegalArgumentException(%22EMPL ID must be exactly 8 digits long%22);%0A        %7D%0A        return true;%0A    %7D%0A%0A
     private void
@@ -2369,16 +2369,17 @@
      try
+ 
 %7B%0A      
@@ -2400,24 +2400,58 @@
  getEmpl();%0A
+            validateEmpl(emplId);%0A
         %7D ca
@@ -2479,16 +2479,17 @@
 ption e)
+ 
 %7B%0A      
@@ -2564,27 +2564,34 @@
 rror%22, %22EMPL
+ 
 ID 
+field 
 must contain
@@ -2595,32 +2595,100 @@
 ain only numbers
+%5Cn%22 +%0A                    %22and EMPL ID must be exactly 8 digits long
 %22);%0A            
@@ -2724,16 +2724,17 @@
 on(%22EMPL
+ 
 ID must 
@@ -2762,24 +2762,353 @@
 );%0A        %7D
+ catch (IllegalArgumentException e) %7B%0A            signInBox.getTextField().clear();%0A            PopUp.display(%22Error%22, %22EMPL ID field must contain only numbers%5Cn%22 +%0A                    %22and EMPL ID must be exactly 8 digits long%22);%0A            throw new IllegalArgumentException(%22EMPL ID must be exactly 8 digits long%22);%0A        %7D
 %0A%0A        if
@@ -3637,24 +3637,67 @@
  %7D%0A        %7D
+%0A%0A        signInBox.getTextField().clear();
 %0A    %7D%0A%0A    

Validate EMPL ID on sign in