
package com.bekwam.examples.javafx.oldscores3;

import com.bekwam.jfxbop.guice.GuiceControllerFactory;
import com.google.inject.Guice;
import com.google.inject.Injector;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;
import javafx.util.BuilderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An application that compares standardized test scores before 1995 with today's scores
 *
 * Third refactor to introduce complete Guice wiring of FX objects and a centralized navigation controller
 *
 * @author carl_000
 */
public class OldScoresApplication3 extends Application {

    private final Logger logger = LoggerFactory.getLogger(OldScoresApplication3.class);

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

        if( logger.isDebugEnabled() ) {
            logger.debug("[START]");
        }

        Injector injector = Guice.createInjector(new OldScoresModule());

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml3/MainView.fxml"),
                                           null,
                                           injector.getInstance(BuilderFactory.class),
                                           injector.getInstance(GuiceControllerFactory.class));

        Parent p = loader.load();
        MainViewController mv = loader.getController();

        Scene scene = new Scene( p );

        scene.setOnKeyPressed(evt -> {

            if( evt.getCode().equals(KeyCode.F1) ) {
                try {
                    if( logger.isDebugEnabled() ) {
                        logger.debug("[OPEN HELP]");
                    }
                    injector.getInstance(NavigationDelegate.class).openHelpDialog();

                } catch (Exception exc) {
                    String msg = "error showing help dialog";
                    logger.error(msg);
                    Alert alert = new Alert(Alert.AlertType.ERROR, msg);
                    alert.showAndWait();
                }
            }
        });

        primaryStage.setTitle("Old Scores");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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