package depart.commun.monde2d;

import ca.ntro.app.Ntro;
import ca.ntro.app.fx.controls.World2dMouseEventFx;
import ca.ntro.app.world2d.Object2dFx;
import depart.frontal.evenements.EvtRelocaliserViewport;
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 = 400;
	private static final double CONST_B = 0.95;
	private static final double CONST_C = 50;
	
	private Palette2d palette;
	
	private enum Zone {
		A,B,C;
	}
	
	private Zone attrA = Zone.A;
	
	private int attrB = +1;
	
	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(getTopLeftY() + getHeight() > palette.getTopLeftY()) {

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

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

		}

		double varA = getWorldWidth() / getWorldHeight();

		if(attrA != Zone.A
				&& getTopLeftY() < getWorldHeight() * 0.50) {
			
			attrA = Zone.A;
			attrB *= -1;

			Ntro.newEvent(EvtRelocaliserViewport.class)
				.setTopLeftX(0)
				.setTopLeftY(0)
				.setWidth(getWorldWidth())
				.setHeight(getWorldHeight())
				.trigger();

		} else if(getTopLeftY() >= getWorldHeight() * 0.50
					&& getTopLeftY() < getWorldHeight() * 0.75) {
			
			attrA = Zone.B;

			double varB = getTopLeftY() - getWorldHeight() * 0.50;
			double varC = getWorldHeight()-varB;
			double varD = varC*varA;
			
			Ntro.newEvent(EvtRelocaliserViewport.class)
				.setTopLeftX(getWorldWidth()/2 - varD/2 + attrB*CONST_C * varB/getWorldHeight()/0.25)
				.setTopLeftY(varB)
				.setWidth(varD)
				.setHeight(varC)
				.trigger();

		} else if(attrA != Zone.C
				    && getTopLeftY() >= getWorldHeight() * 0.75){ 

			attrA = Zone.C;

			double varB = getWorldHeight() * 0.25;
			double varC = getWorldHeight() - varB;
			double varD = varC*varA;
			
			Ntro.newEvent(EvtRelocaliserViewport.class)
				.setTopLeftX(getWorldWidth()/2 - varD/2 + attrB*CONST_C)
				.setTopLeftY(varB)
				.setWidth(varD)
				.setHeight(varC)
				.trigger();

		}

	}

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

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

}
