package depart.frontal.taches;

import ca.ntro.app.tasks.frontend.FrontendTasks;
import depart.frontal.vues.VueA;
import depart.frontal.vues.VueB;
import depart.commun.modeles.ModeleA;
import depart.frontal.evenements.EvtA;
import depart.frontal.vues.VueRacine;
import ca.ntro.app.Ntro;
import ca.ntro.app.session.Session;

import static ca.ntro.app.tasks.frontend.FrontendTasks.*;

public class TachesB {
	
	public static void creerTaches(FrontendTasks tasks) {
		
		tasks.taskGroup("TachesB")
		
		     .waitsFor("TachesA")
		
		     .contains(subTasks -> {

				tacheB1(subTasks);

				tacheB2(subTasks);

				tacheB3(subTasks);

				tacheB4(subTasks);

		     });
	}

	private static void tacheB1(FrontendTasks subTasks) {

		subTasks.task("tacheB1")
		
		        .waitsFor(window())
		      
		        .waitsFor(created(VueRacine.class))

		        .executes(inputs -> {
		    	  
		    	  var objA  = inputs.get(created(VueRacine.class));
		    	  var objB  = inputs.get(window());

		    	  objB.installRootView(objA);
		      });
	}

	private static void tacheB2(FrontendTasks subTasks) {

		subTasks.task("tacheB2")

		        .waitsFor(window())

		        .executes(inputs -> {

		    	    var objC = inputs.get(window());

		    	    objC.show();
		    	    
		        });
	}

	private static void tacheB3(FrontendTasks tasks) {

		tasks.task("tacheB3")

		     .waitsFor(created(VueRacine.class))  // .waitsFor pour chaque Vue
		     .waitsFor(created(VueA.class))
		     .waitsFor(created(VueB.class))

		     .executes(inputs -> {

		    	 Ntro.newEvent(EvtA.class).trigger();

		     });
	}

	private static void tacheB4(FrontendTasks tasks) {

		tasks.task("tacheB4")

		     .waitsFor(session(Session.class)) 

		     .executes(inputs -> {
		    	 
		    	 Session<?> session = inputs.get(session(Session.class));
		    	 
		    	 session.setModelSelection(ModeleA.class, session.sessionId());

		     });
	}

}
