package com.gitrekt.resort.controller;

import com.gitrekt.resort.model.entities.Booking;
import com.gitrekt.resort.model.services.BookingService;
import java.io.IOException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextInputDialog;

/**
 * FXML Controller class for the guest home screen.
 */
public class GuestHomeScreenController implements Initializable {

    @FXML
    private Button viewBookingButton;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    @FXML
    private void onBrowseRoomsButtonClicked() {
        ScreenManager.getInstance().switchToScreen("/fxml/BrowseRoomsScreen.fxml");
    }

    @FXML
    private void onLeaveFeedbackButtonClicked() {
        ScreenManager.getInstance().switchToScreen("/fxml/LeaveFeedbackScreen.fxml");
    }

    @FXML
    private void onBackButtonClicked() {
        ScreenManager.getInstance().switchToScreen("/fxml/HomeScreen.fxml");
    }

    @FXML
    private void onViewBookingButtonClicked() throws IOException {
        TextInputDialog dialog = new TextInputDialog();
        dialog.setTitle("Find booking");
        dialog.setHeaderText("Enter booking number");
        dialog.setContentText("Please enter your booking number: ");

        Optional<String> result = dialog.showAndWait();
        if(result.isPresent()) {
            Long bookingId = Long.valueOf(result.get());
            BookingService bookingService = new BookingService();
            Booking booking = bookingService.getBookingById(bookingId);
            Object temp = ScreenManager.getInstance().switchToScreen(
                "/fxml/BookingDetailsScreen.fxml"
            );
            BookingDetailsScreenController controller = (BookingDetailsScreenController) temp;
            controller.intializeBookingData(booking);
        }
    }
}

@@ -388,16 +388,66 @@
 tDialog;
+%0Aimport javax.persistence.EntityNotFoundException;
 %0A%0A/**%0A *
@@ -1653,24 +1653,46 @@
 gService();%0A
+            try %7B%0A    
             
@@ -1751,16 +1751,154 @@
 ingId);%0A
+                // Force loading of booking from DB (because of lazy loading)%0A                System.out.println(booking.toString());%0A    
         
@@ -1975,16 +1975,20 @@
         
+   
+ 
 %22/fxml/B
@@ -2025,19 +2025,27 @@
         
-);%0A
+    );%0A    
         
@@ -2143,16 +2143,20 @@
         
+ 
+   
 controll
@@ -2189,16 +2189,529 @@
 oking);%0A
+            %7D catch (EntityNotFoundException e) %7B%0A                return;%0A            %7D%0A            Booking booking = bookingService.getBookingById(bookingId);%0A            if(booking != null) %7B%0A                Object temp = ScreenManager.getInstance().switchToScreen(%0A                    %22/fxml/BookingDetailsScreen.fxml%22%0A                );%0A                BookingDetailsScreenController controller = (BookingDetailsScreenController) temp;%0A                controller.intializeBookingData(booking);%0A            %7D%0A%0A
         

Fix dialog invalid booking number input