package depart.commun.monde2d;

import ca.ntro.app.Ntro;
import ca.ntro.app.fx.controls.World2dMouseEventFx;
import ca.ntro.app.world2d.Object2dFx;
import depart.commun.messages.MsgB;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;

public class Balle2d extends Object2dFx {
	
	private static final double CONST_A = 900;
	private static final double CONST_B = 0.8;

	private long attrA;
	
	public Balle2d() {
		super();

		setWidth(20);           
		setHeight(20);
		
		setAccelerationY(CONST_A);
	}

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

		attrA = Ntro.time().nowMilliseconds();
	}

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

		if(getTopLeftY() + getHeight() > getWorldHeight()) {

			setTopLeftY(getWorldHeight()-getHeight());
			setSpeedY(-1 * Math.abs(getSpeedY()) * CONST_B); 

			if(Math.abs(getSpeedY()) > 100) {

				long varA = Ntro.time().nowMilliseconds();
				
				Ntro.newMessage(MsgB.class)
					.setParamA((varA - attrA) / 1E3)
					.send();
				
				attrA = varA;
			}
		}
	}

	@Override
	public void drawOnWorld(GraphicsContext gc) {
		gc.setFill(Color.BROWN);

		gc.fillArc(getTopLeftX(),
				   getTopLeftY(),
				   getWidth(),
				   getHeight(),
				   0,
				   360,
				   ArcType.CHORD);
	}

}
