// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package booking;

import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JOptionPane;

import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class MainForm {
    private static final String DATA_PROVIDER_NAME = "booking.DummyDataProvider";
    private DataProvider data;
    @FXML
    private TextArea bookings;
    @FXML
    private ComboBox<Hotel> hotels;
    private void handleException(Exception e) {
        JOptionPane.showMessageDialog(null, e.toString()); // JavaFX does not have a simple alternative
        Platform.exit();
    }
    public void initialize() {
        try {
            data = (DataProvider)Class.forName(DATA_PROVIDER_NAME).newInstance();
            hotels.setItems(FXCollections.observableList(data.getHotelList(this::handleException)));
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            handleException(e);
        }
    }
    public void fillBookings() {
        Hotel h = hotels.getValue();
        List<Booking> blst = data.getBookingList(h.getName(), this::handleException);
        bookings.setText(blst.stream().map(b -> b.toString()).collect(Collectors.joining("\r\n")));
    }
    public void setCloseHandler(Stage stg) {
        stg.setOnCloseRequest(evt -> {
                                        data.close(this::handleException);
                                        Platform.exit();
                                    });
    }
}
