/*
 * Copyright (c) 2015, CCT and/or its affiliates. All rights reserved.
 * CCT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package com.cctintl.jfx.transitions.hamburger;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.Transition;
import javafx.beans.binding.Bindings;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.util.Duration;

import com.cctintl.jfx.controls.JFXHamburger;
import com.cctintl.jfx.controls.JFXRippler;
import com.cctintl.jfx.jidefx.CachedTimelineTransition;

public class HamburgerSlideCloseTransition extends CachedTimelineTransition implements HamburgerTransition {
	
	public HamburgerSlideCloseTransition(){
		super(null,null);
	}
	
	public HamburgerSlideCloseTransition(JFXHamburger burger) {
		super(burger, createTimeline(burger));				
		timeline.bind(Bindings.createObjectBinding(()->createTimeline(burger),
				((Region) burger.getChildren().get(0)).widthProperty(), ((Region) burger.getChildren().get(0)).heightProperty() ));					
		setCycleDuration(Duration.seconds(0.3));
		setDelay(Duration.seconds(0));
		
		setOnFinished((finish)->{
			if(this.getRate() == 1)
				burger.getChildren().get(1).setVisible(false);
		});
		burger.widthProperty().addListener((o,oldVal,newVal)->{
			System.out.println(newVal);
		});
	}
	
	@Override
	protected void starting() {		
		super.starting();
		
		if(node.getParent() instanceof JFXRippler){
			JFXRippler rippler = (JFXRippler) node.getParent();
			BorderPane p = new BorderPane(node);
			p.setMaxWidth(((JFXHamburger)node).getWidth());
			p.setMinWidth(((JFXHamburger)node).getWidth());
			p.addEventHandler(MouseEvent.ANY, (event)->{
				if(!event.isConsumed()){
					event.consume();
					node.fireEvent(event);	
				}
			});
			rippler.setControl(p);
		}
		
		if(this.getRate() == -1) {
			((JFXHamburger)node).getChildren().get(1).setVisible(true);
		}
	}
	
	
	private static Timeline createTimeline(JFXHamburger burger){
		double burgerWidth = burger.getChildren().get(0).getLayoutBounds().getWidth();
		double burgerHeight = burger.getChildren().get(2).getBoundsInParent().getMaxY() - burger.getChildren().get(0).getBoundsInParent().getMinY();
		
		double hypotenuse = Math.sqrt(Math.pow(burgerHeight, 2) + Math.pow(burgerWidth, 2));
		double angle = (Math.toDegrees(Math.asin(burgerWidth/hypotenuse)) + 90);
		
		return new Timeline(
				new KeyFrame(
						Duration.ZERO,       
						new KeyValue(burger.rotateProperty(), 0,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(0).rotateProperty(), 0,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(0).translateYProperty(), 0,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(2).rotateProperty(), 0,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(2).translateYProperty(), 0,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(1).opacityProperty(), 1 ,Interpolator.EASE_BOTH),
						new KeyValue(burger.getChildren().get(1).translateXProperty(), 0,Interpolator.EASE_BOTH)
						),
						new KeyFrame(Duration.millis(1000),
								new KeyValue(burger.rotateProperty(), 0,Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(0).rotateProperty(), angle,Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(0).translateYProperty(), (burgerHeight/2)-burger.getChildren().get(0).getBoundsInLocal().getHeight()/2,Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(2).rotateProperty(), -angle ,Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(2).translateYProperty(), - ((burgerHeight/2)-burger.getChildren().get(2).getBoundsInLocal().getHeight()/2),Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(1).opacityProperty(), 0,Interpolator.EASE_BOTH),
								new KeyValue(burger.getChildren().get(1).translateXProperty(), - burger.getWidth()/1.1,Interpolator.EASE_BOTH)
								)
				);
	}
	
	public Transition getAnimation(JFXHamburger burger){
		return new HamburgerSlideCloseTransition(burger);
	}
}
@@ -1484,106 +1484,8 @@
 );%0D%0A
-%09%09burger.widthProperty().addListener((o,oldVal,newVal)-%3E%7B%0D%0A%09%09%09System.out.println(newVal);%0D%0A%09%09%7D);%0D%0A
 %09%7D%0D%0A

remove logging code