// eventManager.js

export default class EventManager {

	isDragging = false;

	lastX = 0;

	lastY = 0;

	camera;

	canvas;

	setCanvas( canvas ) {

		this.canvas = canvas;


		//this.registerEventListeners();

		//this.handleResize();

	}

	setup( canvas, camera ) {

		this.canvas = canvas;

		this.camera = camera;

		//this.registerEventListeners();

		//this.handleResize();

	}

	registerEventListeners() {

		this.canvas.addEventListener( "mousedown", this.onMouseDown.bind(this) );

		this.canvas.addEventListener( "mouseup", this.onMouseUp.bind(this) );

		this.canvas.addEventListener( "mouseleave", this.onMouseLeave.bind(this) );

		this.canvas.addEventListener( "mousemove", this.onMouseMove.bind(this) );

		this.canvas.addEventListener( "wheel", this.onWheel.bind(this), { passive: false } );

	}

	resize( event ) {

		this.canvas.width 	= event.width;

		this.canvas.height 	= event.height;

		//this.canvas.width = window.innerWidth;

		//this.canvas.height = window.innerHeight;

	}

	mousedown( event ) {

		console.log("mouseDownHandler");

		this.isDragging = true;

		this.lastX = event.clientX;

		this.lastY = event.clientY;

	}

	mouseup( event ) {

		this.isDragging = false;

	}

	mouseleave( event ) {

		this.isDragging = false;

	}

	mousemove( event ) {

		if ( !this.isDragging ) return;

		const deltaX = ( event.clientX - this.lastX ) * 0.005;

		const deltaY = ( event.clientY - this.lastY ) * 0.005;

		this.camera.rotate( deltaX, -deltaY );

		this.lastX = event.clientX;

		this.lastY = event.clientY;

	}

	wheel( event ) {


		const delta = event.deltaY * 0.01;

		this.camera.zoom( delta );

	}

}
