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.image.Image;

public class Terrain2dDepart extends Object2dFx {

	
	private transient Image fond = new Image("/images/fond.jpg");

	private transient double fondTopLeftX = 0;
	private transient double fondTopLeftY = 0;
	private transient double fondWidth    = fond.getWidth();
	private transient double fondHeight   = fond.getHeight();
	
	public Terrain2dDepart() {
		super();
		
		setTopLeftX(0);
		setTopLeftY(0);
	}

	@Override
	public void onAddedToWorld() {
		relocaliserRedimensionner();
	}

	private void relocaliserRedimensionner() {
		setWidth(getWorldWidth());
		setHeight(getWorldHeight());

		fondTopLeftX = 0;
		fondTopLeftY = 0;
		fondWidth    = fond.getWidth();
		fondHeight   = fond.getHeight();

		if(fondWidth > getWorldWidth()
				&& fondHeight > getWorldHeight()) {
			
			fondTopLeftX = fondWidth/2 - getWorldWidth()/2;
			fondTopLeftY = fondHeight/2 - getWorldHeight()/2;
			
			fondWidth = getWorldWidth();
			fondHeight = getWorldHeight();
		}
	}

	@Override
	public void onWorldChangedSize(double oldWidth, double oldHeight) {
		relocaliserRedimensionner();
	}

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

	@Override
	public void drawOnWorld(GraphicsContext gc) {
		gc.drawImage(fond, fondTopLeftX,
				           fondTopLeftY,
				           fondWidth,
				           fondHeight,
						   getTopLeftX(), 
				           getTopLeftY(), 
				           getWidth(), 
				           getHeight());
	}

}
