package meerkat.voting.gui.uiFX;

/**
 * Created by Laura on 12/12/2016.
 */

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import meerkat.voting.gui.controllersFX.MainController;
import meerkat.voting.gui.controllersFX.PrinterController;
import meerkat.voting.gui.controllersFX.VistaNavigator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.fxml.FXMLLoader;

import java.io.IOException;

public class MainFX extends Application {

    private static final Logger logger = LoggerFactory.getLogger(GraphicalUI.class);
    private Stage printing;

//    @Override
//    public void init() throws Exception {
//        parent.setGraphicalFx(this);
//    }

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

    private static boolean launched = false;

    private static Object monitor = new Object();

    public static  void waitUntilLaunched() throws InterruptedException {
        synchronized (monitor) {
            while (!launched) {
                monitor.wait();
            }
        }
    }

    private static void notifyLaunched() {
        synchronized (monitor) {
            launched = true;
            monitor.notifyAll();
        }
    }

    @Override
    public void start(Stage stage) throws Exception{
        stage.setTitle("Voting Booth");
        stage.setScene(
                createScene(
                        loadMainPane()
                )
        );
        stage.show();
        createPrinter(stage.getX(), stage.getY());
        stage.requestFocus();

        notifyLaunched();
    }

    @Override
    public void stop(){
        System.out.println("MainFX: Stage is closing ---- need to callShutDown() on VotingBoothImpl");
    }

    /**
     * Loads the main fxml layout.
     * Sets up the vista switching VistaNavigator.
     * Loads the first vista into the fxml layout.
     *
     * @return the loaded pane.
     * @throws IOException if the pane could not be loaded.
     */
    private Pane loadMainPane() throws IOException {
        FXMLLoader loader = new FXMLLoader(getClass().getResource(VistaNavigator.MAIN));

        Pane mainPane = (Pane) loader.load();

        MainController mainController = loader.getController();
        mainController.init();//setCurrentCommand(null);

        VistaNavigator.setMainController(mainController);
        VistaNavigator.loadVista(VistaNavigator.EMPTY_START);

        return mainPane;
    }

    /**
     * Creates the main application scene.
     *
     * @param mainPane the main application layout.
     *
     * @return the created scene.
     */
    private Scene createScene(Pane mainPane) {
        Scene scene = new Scene(mainPane);
        scene.getStylesheets().setAll(
                getClass().getResource("/views/vista.css").toExternalForm()
        );
        return scene;
    }

    private void createPrinter(double mainX, double mainY) throws IOException {
        printing = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource(VistaNavigator.PRINTER));
        Pane mainPane = null;
        mainPane = (Pane) loader.load();
        PrinterController printerController = loader.getController();
        VistaNavigator.setPrinterController(printerController);

        printing.setX(mainX - printing.getWidth());
        printing.setY(mainY - printing.getHeight());
        printing.setTitle("Virtual Printer");

        Scene scene = new Scene(mainPane);
        printing.setScene(scene);
        printing.show();
    }



}
