const scriptsInEvents = {

	async Gameplay_Event29_Act13(runtime, localVars)
	{
		// Get device info at game start
		window.apiHandler._getDeviceInfo();
	},

	async Gameplay_Event29_Act14(runtime, localVars)
	{
		// Generate and verify token at game start
		try {
			const token = await window.apiHandler.verifyTokenAtStart();
			runtime.globalVars.token = token;
			runtime.globalVars.isTokenValid = true;
			runtime.globalVars.gameStartTime = Date.now();
		} catch (error) {
			runtime.globalVars.isTokenValid = false;
		}
	},

	async Gameplay_Event173_Act11(runtime, localVars)
	{
		// Submit score on level completion
		const gameEndTime = Date.now();
		const levelScore = runtime.globalVars.LevelVariable - 1;
		const duration = Math.floor((gameEndTime - runtime.globalVars.gameStartTime) / 1000);
		
		window.apiHandler.submitFinalScore(
			runtime.globalVars.token,
			levelScore,
			duration
		).catch(error => {
			console.error('Error submitting level completion:', error);
		});
	},

	async Gameplay_Event180_Act7(runtime, localVars)
	{
		// Submit score on game end
		const gameEndTime = Date.now();
		const levelScore = runtime.globalVars.LevelVariable - 1;
		const duration = Math.floor((gameEndTime - runtime.globalVars.gameStartTime) / 1000);
		
		window.apiHandler.submitFinalScore(
			runtime.globalVars.token,
			levelScore,
			duration
		).catch(error => {
			console.error('Error submitting final score:', error);
		});
	}
};

self.C3.ScriptsInEvents = scriptsInEvents;
