import {FullscreenEffect} from "./FullscreenEffect.js";

export class SnowEffect extends FullscreenEffect
{
	static NAME = 'snow';

	constructor()
	{
		super();

		this.audio = new Audio('js/effects/storm.mp3');
		this.audio.loop = true;
		this.image = new Image();
		this.image.src = 'js/effects/snow.png';
		this.offset = 0;
		this.speed = 1.0;
	}

	init()
	{
		super.init();

		this.audio.play();
	}

	update(timestamp)
	{
		super.update(timestamp);

		this.offsetX = (timestamp * 0.65 * this.speed) % this.image.width;
		this.offsetY = (timestamp * 1.5 * this.speed) % this.image.height;
	}

	render(context, camera)
	{
		super.render(context);

		const cameraX = -1 * (camera.position.x % this.image.width);
		const cameraY = -1 * (camera.position.y % this.image.height);

		for (let y = -1; y < Math.ceil(context.canvas.height / this.image.height) + 1; y++) {
			for (let x = -1; x < Math.ceil(context.canvas.width / this.image.width) + 1; x++) {
				context.drawImage(
					this.image,
					cameraX + this.offsetX + this.image.width * x,
					cameraY + this.offsetY + this.image.height * y,
				);
			}
		}
	}

	destroy()
	{
		super.destroy();

		this.audio.pause();
		this.audio.remove();
	}
}
