

const scriptsInEvents = {

	async Es_game_Event2_Act24(runtime, localVars)
	{
		    const deviceInfo = await window.apiHandler._getDeviceInfo();
            // Cache the device info for future use
            localStorage.setItem('deviceInfo', JSON.stringify(deviceInfo));
            console.log('Device info cached:', deviceInfo);
		
	},

	async Es_game_Event2_Act25(runtime, localVars)
	{
		// Initialize API Handler
		if (window.apiHandler) {
		    console.log('API Handler already exists');
		    await window.apiHandler.verifyTokenAtStart();
		} else {
		    console.error('API Handler not found! Check if api_template.js is loaded properly');
		}
	},

	async Es_game_Event12_Act1(runtime, localVars)
	{
		// Submit score when game ends or when zombie is killed
		if (window.apiHandler) {
		    const currentScore = runtime.globalVars.currentLevel;  
		    const level = runtime.globalVars.currentLevel;
		    try {
		        await window.apiHandler.submitScoreIfNeeded(currentScore,level);
		        console.log('Score submission attempted for score:', currentScore);
		    } catch (error) {
		        console.error('Error submitting score:', error);
		    }
		}
	},

	async Es_game_Event32_Act7(runtime, localVars)
	{
		// Submit score
		const gameEndTime = Date.now();
		window.apiHandler.submitScore(
		    runtime.globalVars.token,
		    runtime.globalVars.currentLevel,
		    runtime.globalVars.gameStartTime,
		    gameEndTime
		)
		.then(response => {
		    console.log('Level completion submitted:', response);
		})
		.catch(error => {
		    console.error('Error submitting level completion:', error);
		});
	}
};

globalThis.C3.JavaScriptInEvents = scriptsInEvents;
