import StartEnergy from "./start-energy"
import TuneSignal from "./tune-signal"
import FindArc from "./find-arc"
import TuneAntenna from "./tune-antenna"
import TuneWaves from "./tune-waves"

function run(quest, container, cb) {
	const canvas = document.createElement('canvas')
	canvas.width = 600
	canvas.height = 600

	container.appendChild(canvas)

	const ctx = canvas.getContext('2d')

	canvas.addEventListener('click', (ev) => {
		quest.click(ev.offsetX, ev.offsetY)
	})

	canvas.addEventListener('mousemove', (ev) => {
		canvas.style.cursor = quest.getCursor(ev.offsetX, ev.offsetY)
	})

	if (ctx) {
		let prev = 0
		const loop = (dt) => {
			ctx.fillStyle = '#000'
			ctx.fillRect(0, 0, 800, 800)

			quest.update(dt - prev)
			quest.updateInternal(dt - prev)
			prev = dt

			quest.draw(ctx)
			quest.drawInternal(ctx)

			if (quest.result) {
				cb()
			} else {
				requestAnimationFrame(loop)
			}
		}

		loop(0)
	}
}

export default {
	'start-energy': StartEnergy,
	'tune-signal': TuneSignal,
	'find-arc': FindArc,
	'tune-antenna': TuneAntenna,
	'tune-waves': TuneWaves,
	run
}
