/**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for the PianoTile
var PianoTile = Container.expand(function () {
	var self = Container.call(this);
	// Create and attach a black rectangle as the tile
	var tileGraphics = self.attachAsset('tile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Set the speed of the tile
	self.speed = globalSpeed;
	self.isTapped = false;
	// Update function to move the tile downwards
	self.update = function () {
		self.y += self.speed;
		// Check if tile is off-screen
		if (self.y > 2732) {
			// End game if a tile is missed
			if (self.isTapped) {
				if (self.y > 2732 + self.height) {
					self.destroy();
				}
				return;
			}
			LK.showGameOver();
			return;
		}
	};
	// Handle tap events
	self.down = function (x, y, obj) {
		// Increase score and destroy the tile
		LK.setScore(LK.getScore() + 1);
		self.isTapped = true;
		self.alpha = 0.1;
		// if (playTimeout) {
		// 	LK.clearTimeout(playTimeout);
		// }
		if (chunks.length > 0) {
			chunks[globalIndex].play();
		}
		if (globalIndex < chunks.length - 1) {
			globalIndex++;
		}
		/*
		// Play choice1 sound at game launch
		choice1.play();
		playTimeout = LK.setTimeout(function () {
		choice1.stop();
		}, 1000);
		*/
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x4169e1 //Init game with black background 
});
/**** 
* Game Code
****/ 
// }
// 	return chunks;
// 	}
// 		chunks.push(soundChunk);
// 		});
// 			end: end
// 			start: start,
// 		var soundChunk = LK.getSound('choice1', {
// 		var end = (i + 1) * chunkLength / 118464;
// 		var start = i * chunkLength / 118464;
// 	for (var i = 0; i < 119; i++) {
// 	var chunkLength = 118464 / 119;
// 	var chunks = [];
// function createChunks() {
function createChunks() {
	var chunks = [];
	var totalDuration = 118464; // Durée totale en ms
	var chunkDuration = 1000; // Durée de chaque chunk en ms
	var numFullChunks = Math.floor(totalDuration / chunkDuration); // Nombre de chunks complets (118)
	// Créer 118 chunks de 1000ms
	for (var i = 0; i < numFullChunks; i++) {
		var start = i * chunkDuration / totalDuration;
		var end = (i + 1) * chunkDuration / totalDuration;
		var soundChunk = LK.getSound('choice_' + i, {
			start: start,
			end: end
		});
		chunks.push(soundChunk);
	}
	// Ajouter le dernier chunk avec le reste
	if (totalDuration % chunkDuration > 0) {
		var start = numFullChunks * chunkDuration / totalDuration;
		var end = 1; // Jusqu'à la fin du son
		var soundChunk = LK.getSound('choice1', {
			start: start,
			end: end
		});
		chunks.push(soundChunk);
	}
	return chunks;
}
var chunks = [];
LK.setTimeout(function () {
	chunks = createChunks();
}, 3000);
// Initialize global speed variable
var globalSpeed = 18; // 10 / 12 / 15 / 18 / 20 / 25
var globalTickRate = 900 / globalSpeed;
var globalIndex = 0;
var perfectY = 1800;
// Initialize global variable for choice1
var choice1 = LK.getSound('choice1');
var playTimeout;
// Initialize score display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create column assets to separate lanes
var column1 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024 - 400 - 3,
	y: 2732 / 2
});
game.addChild(column1);
var column2 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2732 / 2
});
game.addChild(column2);
var column3 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024 + 400 + 3,
	y: 2732 / 2
});
game.addChild(column3);
var perfectLine1 = LK.getAsset('perfectLine', {
	anchorX: 0.5,
	anchorY: 0.5,
	alpha: 0.5,
	x: 1024,
	y: perfectY - 450
});
game.addChild(perfectLine1);
var perfectLine2 = LK.getAsset('perfectLine', {
	anchorX: 0.5,
	anchorY: 0.5,
	alpha: 0.5,
	x: 1024,
	y: perfectY + 450
});
game.addChild(perfectLine2);
// Array to keep track of active tiles
var tiles = [];
// Function to create a new tile
var lastLane = 0;
function createTile() {
	var newTile = new PianoTile();
	// Randomly position the tile in one of the two lanes
	newTile.x = lastLane = lastLane == 1024 - 200 - 3 ? 1024 + 200 + 3 : 1024 - 200 - 3;
	newTile.y = -500; // Start above the screen
	tiles.push(newTile);
	game.addChild(newTile);
}
// Game update function
game.update = function () {
	// Update score display
	scoreTxt.setText(LK.getScore());
	// speed 10 => every 90 ticks
	// speed 15 => every 60 ticks
	if (LK.ticks % globalTickRate == 0) {
		createTile();
	}
}; /**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Define a class for the PianoTile
var PianoTile = Container.expand(function () {
	var self = Container.call(this);
	// Create and attach a black rectangle as the tile
	var tileGraphics = self.attachAsset('tile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Set the speed of the tile
	self.speed = globalSpeed;
	self.isTapped = false;
	// Update function to move the tile downwards
	self.update = function () {
		self.y += self.speed;
		// Check if tile is off-screen
		if (self.y > 2732) {
			// End game if a tile is missed
			if (self.isTapped) {
				if (self.y > 2732 + self.height) {
					self.destroy();
				}
				return;
			}
			LK.showGameOver();
			return;
		}
	};
	// Handle tap events
	self.down = function (x, y, obj) {
		// Increase score and destroy the tile
		LK.setScore(LK.getScore() + 1);
		self.isTapped = true;
		self.alpha = 0.1;
		// if (playTimeout) {
		// 	LK.clearTimeout(playTimeout);
		// }
		if (chunks.length > 0) {
			chunks[globalIndex].play();
		}
		if (globalIndex < chunks.length - 1) {
			globalIndex++;
		}
		/*
		// Play choice1 sound at game launch
		choice1.play();
		playTimeout = LK.setTimeout(function () {
		choice1.stop();
		}, 1000);
		*/
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x4169e1 //Init game with black background 
});
/**** 
* Game Code
****/ 
// }
// 	return chunks;
// 	}
// 		chunks.push(soundChunk);
// 		});
// 			end: end
// 			start: start,
// 		var soundChunk = LK.getSound('choice1', {
// 		var end = (i + 1) * chunkLength / 118464;
// 		var start = i * chunkLength / 118464;
// 	for (var i = 0; i < 119; i++) {
// 	var chunkLength = 118464 / 119;
// 	var chunks = [];
// function createChunks() {
function createChunks() {
	var chunks = [];
	var totalDuration = 118464; // Durée totale en ms
	var chunkDuration = 1000; // Durée de chaque chunk en ms
	var numFullChunks = Math.floor(totalDuration / chunkDuration); // Nombre de chunks complets (118)
	// Créer 118 chunks de 1000ms
	for (var i = 0; i < numFullChunks; i++) {
		var start = i * chunkDuration / totalDuration;
		var end = (i + 1) * chunkDuration / totalDuration;
		var soundChunk = LK.getSound('choice_' + i, {
			start: start,
			end: end
		});
		chunks.push(soundChunk);
	}
	// Ajouter le dernier chunk avec le reste
	if (totalDuration % chunkDuration > 0) {
		var start = numFullChunks * chunkDuration / totalDuration;
		var end = 1; // Jusqu'à la fin du son
		var soundChunk = LK.getSound('choice1', {
			start: start,
			end: end
		});
		chunks.push(soundChunk);
	}
	return chunks;
}
var chunks = [];
LK.setTimeout(function () {
	chunks = createChunks();
}, 3000);
// Initialize global speed variable
var globalSpeed = 18; // 10 / 12 / 15 / 18 / 20 / 25
var globalTickRate = 900 / globalSpeed;
var globalIndex = 0;
var perfectY = 1800;
// Initialize global variable for choice1
var choice1 = LK.getSound('choice1');
var playTimeout;
// Initialize score display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create column assets to separate lanes
var column1 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024 - 400 - 3,
	y: 2732 / 2
});
game.addChild(column1);
var column2 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2732 / 2
});
game.addChild(column2);
var column3 = LK.getAsset('column', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024 + 400 + 3,
	y: 2732 / 2
});
game.addChild(column3);
var perfectLine1 = LK.getAsset('perfectLine', {
	anchorX: 0.5,
	anchorY: 0.5,
	alpha: 0.5,
	x: 1024,
	y: perfectY - 450
});
game.addChild(perfectLine1);
var perfectLine2 = LK.getAsset('perfectLine', {
	anchorX: 0.5,
	anchorY: 0.5,
	alpha: 0.5,
	x: 1024,
	y: perfectY + 450
});
game.addChild(perfectLine2);
// Array to keep track of active tiles
var tiles = [];
// Function to create a new tile
var lastLane = 0;
function createTile() {
	var newTile = new PianoTile();
	// Randomly position the tile in one of the two lanes
	newTile.x = lastLane = lastLane == 1024 - 200 - 3 ? 1024 + 200 + 3 : 1024 - 200 - 3;
	newTile.y = -500; // Start above the screen
	tiles.push(newTile);
	game.addChild(newTile);
}
// Game update function
game.update = function () {
	// Update score display
	scoreTxt.setText(LK.getScore());
	// speed 10 => every 90 ticks
	// speed 15 => every 60 ticks
	if (LK.ticks % globalTickRate == 0) {
		createTile();
	}
};