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;
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 Palette2d palette;
	
	public Balle2d(Palette2d palette) {
		super();
		
		this.palette = palette;

		setWidth(20);           
		setHeight(20);

		setAccelerationY(CONST_A);
	}

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

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

		if(getTopLeftX() + getWidth() > palette.getTopLeftX()
				&& getTopLeftX() < palette.getTopLeftX() + palette.getWidth()) {
			
			if(getTopLeftY() + getHeight() > palette.getTopLeftY()) {

				setTopLeftY(palette.getTopLeftY()-getHeight());

				setSpeedY(-1 * Math.abs(getSpeedY()) * CONST_B); 
			}

		} else if(getTopLeftY() + getHeight() > getWorldHeight()
					&& getSpeedY() > 0) {

			setTopLeftY(getWorldHeight()-getHeight());

			setSpeedY(-1 * Math.abs(getSpeedY()) * CONST_B); 
		}
	}

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

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

}
