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.enums.ActionJoueur;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Palette2d extends Object2dFx {
	
	public static final double MOVE_SPEED = 200;
	public static final double THROW_SPEED = 650;
	public static final double FALL_SPEED = 250;
	
	private Balle2d balle = null;

	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) {
		if(getTopLeftY() + getHeight() > getWorldHeight()) {
			setTopLeftY(getWorldHeight() - getHeight() - 2);
			setSpeedY(0);
		}

		super.onTimePasses(secondsElapsed);
	}

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

	public void reagirActionJoueur(ActionJoueur action) {
		if(action == ActionJoueur.GAUCHE) {

			setSpeedX(-MOVE_SPEED);

		}else if(action == ActionJoueur.DROITE) {

			setSpeedX(+MOVE_SPEED);

		}else if(action == ActionJoueur.SAUTER) {

			setTopLeftY(getWorldHeight() - getHeight() - 50);
			setSpeedY(+FALL_SPEED);
			
			if(balle.getTopLeftX() +  balle.getWidth() > getTopLeftX()
					&& balle.getTopLeftX() < getTopLeftX() + getWidth()
					&& balle.getTopLeftY() + balle.getHeight() > getTopLeftY()) {

				balle.setTopLeftY(getTopLeftY());
				balle.setSpeedY(-1 * (THROW_SPEED + Ntro.random().nextDouble(200)));
			}

		}else {
			
			setSpeedX(0);

		}
	}

}
