package depart.commun.monde2d;

import ca.ntro.app.fx.controls.World2dMouseEventFx;
import ca.ntro.app.world2d.Object2dFx;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Palette2d extends Object2dFx {
	
	public static final double VITESSE_FRAPPER = 550;
	public static final double VITESSE_RETOMBER = 300;
	
	private Balle2d balle = null;
	
	private boolean attrA = false;
	private double  targetTopLeftY;

	public Palette2d() {
		super();

		setWidth(100);           
		setHeight(20);
	}
	
	void setBalle(Balle2d balle) {
		this.balle = balle;
	}

	@Override
	public void onAddedToWorld() {
		setTopLeftX(getWorldWidth() / 2 - getWidth() / 2);
		setTopLeftY(getWorldHeight() - getHeight() - 2);
	}

	@Override
	protected boolean onMouseEvent(World2dMouseEventFx mouseEvent) {
		return false;
	}
	
	@Override
	public void onTimePasses(double secondsElapsed) {
		super.onTimePasses(secondsElapsed);

		if(attrA
		      && getTopLeftY() + getHeight() > targetTopLeftY) {
			
			attrA = false;

			setTopLeftY(targetTopLeftY);
			setSpeedY(0);
		}
	}

	@Override
	public void drawOnWorld(GraphicsContext gc) {
		gc.setFill(Color.CORNFLOWERBLUE);
		
		gc.fillRect(getTopLeftX(), 
				    getTopLeftY(), 
				    getWidth(), 
				    getHeight());
	}

	public void sauter() {
		attrA = true;

		targetTopLeftY = getTopLeftY();
		setTopLeftY(getTopLeftY()-50);
		setSpeedY(+VITESSE_RETOMBER);
		
		if(balle.getTopLeftX() +  balle.getWidth() > getTopLeftX()
				&& balle.getTopLeftX() < getTopLeftX() + getWidth()
				&& balle.getTopLeftY() + balle.getHeight() > getTopLeftY()) {
			balle.setTopLeftY(getTopLeftY());
			balle.setSpeedY(-VITESSE_FRAPPER);
		}
	}

	public void suivreSouris(World2dMouseEventFx mouseEvent) {
		if(attrA) return;

		setTopLeftY(mouseEvent.worldY() - getHeight()/2);
		setTopLeftX(mouseEvent.worldX() - getWidth()/2);

		targetTopLeftY = getTopLeftY();
	}

}
