package depart.frontal.vues;

import ca.ntro.app.Ntro;
import ca.ntro.app.frontend.ViewFx;
import javafx.animation.Animation.Status;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.util.Duration;

public class VueRacine extends ViewFx {

	@FXML
	private StackPane stackPaneA;

	private ViewFx attrA;

	private Timeline attrB = new Timeline();

	@Override
	public void initialize() {
		Ntro.assertNotNull(stackPaneA);
	}

	public void methodeA(ViewFx paramA) {
		if(attrB.getStatus() == Status.RUNNING) return;

		if(attrA instanceof VueA 
				&& paramA instanceof VueB) {

			attrA = null;
			methodeB(paramA, 1);
			
		} else if(attrA instanceof VueB 
				&& paramA instanceof VueA) {

			attrA = null;
			methodeB(paramA, -1);

		} else {

			stackPaneA.getChildren().clear();
			stackPaneA.getChildren().add(paramA.rootNode());
			attrA = paramA;
			
		}
	}

	private void methodeB(ViewFx paramA, int paramB) {

		Pane varA = paramA.rootNode();
		stackPaneA.getChildren().add(varA);

		Pane varB = (Pane) stackPaneA.getChildren().get(0);
		
		double varC = stackPaneA.getWidth();
		varC = paramB * varC;
		int varD = 400;

		attrB = new Timeline();
		attrB.getKeyFrames().add(new KeyFrame(new Duration(0), new KeyValue(varA.translateXProperty(), varC)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(0), new KeyValue(varA.opacityProperty(), 0.0)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(0), new KeyValue(varB.translateXProperty(), 0)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(0), new KeyValue(varB.opacityProperty(), 1.0)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(varD), new KeyValue(varA.translateXProperty(), 0, Interpolator.EASE_BOTH)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(varD), new KeyValue(varA.opacityProperty(), 1.0, Interpolator.EASE_BOTH)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(varD), new KeyValue(varB.translateXProperty(), -varC, Interpolator.EASE_BOTH)));
		attrB.getKeyFrames().add(new KeyFrame(new Duration(varD), new KeyValue(varB.opacityProperty(), 0.0, Interpolator.EASE_BOTH)));
		
		attrB.setOnFinished(evtFx -> {
			stackPaneA.getChildren().remove(varB);
			attrA = paramA;
		});
		
		attrB.playFromStart();
	}
}
