// worker.js

import { ParticleSimulation } from "./demo.js";


var particleSimulation = new ParticleSimulation();

export class Controller {

	setup( request ) {

		var canvas = request.canvas;

		console.log( canvas );

		particleSimulation.setup( canvas, request.width, request.height );

		//self.postMessage({ type: "pong", id: request.id });
	}

	resetParticlePositions() {

		particleSimulation.resetParticlePositions();

	}

	mousemove( request ) {

		particleSimulation.eventManager.mousemove( request );

	}

	mousedown( request ) {

		console.log("onMouseDown");

		particleSimulation.eventManager.mousedown( request );

	}

	mouseup( request ) {

		particleSimulation.eventManager.mouseup( request );

	}

	mouseleave( request ) {

		particleSimulation.eventManager.mouseleave( request );

	}	

	wheel( request ) {

		particleSimulation.eventManager.wheel( request );

	}

	resize( request ) {

		particleSimulation.eventManager.resize( request );

	}

	resetParticlePositions() {

		particleSimulation.resetParticlePositions(  );
		
	}
}

const controller = new Controller();

self.onmessage = function (event) {

	const data = event.data;

	if (typeof data !== "object" || typeof data.method !== "string") {
		console.warn("Invalid request received:", data);
		return;
	}

	// Optional: wrap the data into a Request instance (if you need its methods)
	// const request = new Request(data.method, data.payload);

	// Or just use plain data object
	const request = data;

	const methodName = request.method;

	if (typeof controller[methodName] !== "function") {
		console.warn("No method found for:", request.method);
		return;
	}

	controller[methodName](request);
};
