package proy.gui;

import java.util.List;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import proy.datos.servicios.implementacion.HibernateUtil;
import proy.excepciones.ConnectionException;
import proy.gui.componentes.IconoAplicacion;
import proy.gui.componentes.VentanaErrorExcepcion;
import proy.gui.componentes.VentanaErrorExcepcionInesperada;
import proy.gui.componentes.VentanaEsperaBaseDeDatos;
import proy.logica.gestores.Coordinador;

public class Main extends Application {

	private PilaScene apilador;
	private Coordinador coordinador;
	private Stage stagePrincipal;

	public static void main(String[] args) {
		java.util.Enumeration<String> loggers = java.util.logging.LogManager.getLogManager().getLoggerNames();
		while(loggers.hasMoreElements()){
			String log = loggers.nextElement();
			java.util.logging.Logger.getLogger(log).setLevel(java.util.logging.Level.WARNING);
		}
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) {
		verParametros(getParameters().getRaw());
		stagePrincipal = primaryStage;
		apilador = new PilaScene(primaryStage);
		coordinador = new Coordinador() {
		}; //TODO cambiar en el futuro
		primaryStage.getIcons().add(new IconoAplicacion());
		primaryStage.setOnCloseRequest((WindowEvent e) -> {
			//TODO cambiar en el futuro
			//			apilador.desapilarScene();
			//			if(!apilador.isEmpty()){
			//				e.consume();
			//			}
			//			else{
			HibernateUtil.close();
			//			}
		});
		iniciarHibernate();
		//TODO cambiar en el futuro
		//		ControladorIva.nuevaSceneIva(InicioController.URLVista, apilador, coordinador);
		stagePrincipal.setScene(new Scene(new GridPane()));
		stagePrincipal.show();
	}

	private void iniciarHibernate() {
		VentanaEsperaBaseDeDatos ventanaEspera = new VentanaEsperaBaseDeDatos(stagePrincipal.getOwner());
		Task<Boolean> task = new Task<Boolean>() {
			@Override
			public Boolean call() throws ConnectionException {
				HibernateUtil.getSessionFactory();
				return true;
			}
		};

		task.setOnRunning(
				(event) -> {
					ventanaEspera.showAndWait();
				});

		task.setOnSucceeded(
				(event) -> {
					ventanaEspera.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
					ventanaEspera.hide();
				});

		task.setOnFailed(
				(event) -> {
					try{
						throw task.getException();
					} catch(ConnectionException e){
						HibernateUtil.close();
						new VentanaErrorExcepcion(e.getMessage(), stagePrincipal);
						System.exit(1);
					} catch(Throwable e){
						HibernateUtil.close();
						new VentanaErrorExcepcionInesperada(stagePrincipal);
						System.exit(1);
					}
				});

		Thread hiloHibernate = new Thread(task);
		hiloHibernate.setDaemon(false);
		hiloHibernate.start();
	}

	private void verParametros(List<String> raw) {
		//Analizar parmetros
	}
}

@@ -37,16 +37,49 @@
 l.List;%0D
+%0Aimport java.util.logging.Level;%0D
 %0A%0D%0Aimpor
@@ -879,38 +879,78 @@
 til.
-Enumeration%3CString%3E loggers = 
+logging.Logger.getLogger(%22org.hibernate%22).setLevel(Level.WARNING);%0D%0A%09%09
 java
@@ -966,20 +966,16 @@
 ging.Log
-Mana
 ger.getL
@@ -980,115 +980,151 @@
 tLog
-M
+ger(%22com.mch
 an
-a
 ge
-r().getLoggerNames();%0D%0A%09%09while(loggers.hasMoreElements())%7B%0D%0A%09%09%09String log = loggers.nextElement(
+.v2.c3p0%22).setLevel(Level.WARNING);%0D%0A%09%09java.util.logging.Logger.getLogger(%22com.mchange.v2.log.MLog%22).setLevel(Level.WARNING
 );%0D%0A
-%09
 %09%09ja
@@ -1160,40 +1160,73 @@
 ger(
-log).setLevel(java.util.logging.
+%22com.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL%22).setLevel(
 Leve
@@ -1238,21 +1238,16 @@
 NING);%0D%0A
-%09%09%7D%0D%0A
 %09%09launch

Revert "Desactivados logs"