package org.tela_botanica.client;

import org.tela_botanica.client.modeles.objets.Configuration;

import com.google.gwt.ajaxloader.client.AjaxLoader;
import com.google.gwt.ajaxloader.client.AjaxLoader.AjaxLoaderOptions;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.gwtext.client.core.Ext;
import com.gwtext.client.widgets.Viewport;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class cel2 implements EntryPoint {

	/**
	 * This is the entry point method.
	 */
	@Override
	public void onModuleLoad() {	
		
		/*
		// A décommenter + compiler en detailed dans le cas d'une erreur
		// survernant uniquement sur le serveur
		GWT.setUncaughtExceptionHandler(null);
		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {	
			@Override
			public void onUncaughtException(Throwable e) {
					Window.alert("uncaught: " + e.getMessage());
					String s = buildStackTrace(e, "RuntimeException:\n");
					Window.alert(s);
					e.printStackTrace();
			}
		});
		*/
		// TODO: config devrait être un singleton
		Configuration config = new Configuration();
		
		// Chargement de l'api google maps v3 avant l'initialisation pour 
		// éviter des "complications"
		// TODO: fabriquer une classe qui cherche dans la config une liste d'api
		// à charger, les instancie et ensuite lance l'application
		AjaxLoaderOptions optAj = AjaxLoaderOptions.newInstance();
		optAj.setOtherParms("key="+Configuration.getCleGoogleMaps()+"&sensor=false");
			
		Runnable callback = new Runnable() {
			public void run() {
				initialiserCel();
			};
		};
		AjaxLoader.loadApi("maps", "3", callback, optAj);
	}
	
	private void initialiserCel() {
		CarnetEnLigneMediateur carnetEnLigneMediateur= CarnetEnLigneMediateur.Instance();
		new Viewport(carnetEnLigneMediateur.getPanneauPrincipalCarnetEnLigne()) ;
		carnetEnLigneMediateur.getEtatUtilisateur() ;

		Timer t = new Timer() {
			@Override
			public void run() {
				 Ext.get("loading").hide();
			}
		};
		
		t.schedule(3000);
	}
	
	/*private String buildStackTrace(Throwable t, String log) {
	     if (t != null) {
		     log += t.getClass().toString();
		     log += t.getMessage();
		     //
		     StackTraceElement[] stackTrace = t.getStackTrace();
		     if (stackTrace != null) {
		    	 StringBuffer trace = new StringBuffer();
		    
			     for (int i = 0; i < stackTrace.length; i++) {
			    	 trace.append(stackTrace[i].getClassName() + "." + stackTrace[i].getMethodName() + "("
			    			 + stackTrace[i].getFileName() + ":" + stackTrace[i].getLineNumber());
			     }
		    
			     log += trace.toString();
		     }
		     //
		     Throwable cause = t.getCause();
		     if (cause != null && cause != t) {
		    	 log += buildStackTrace(cause, "CausedBy:\n");
		     }
	     }
	     return log;
	}*/
	
	public static native void  LogVersFirebug(Object o) /*-{
		if (!!($wnd.console && $wnd.console.log)) {
			console.log(o);
		}
	}-*/;
}
