// Based on https://github.com/electron/electron-quick-start/

/**
Point d'entrée pour l'app nodejs.
Ce point d'entrée ne peut pas communiqué avec les pages chargées
sauf en utilisant un système de message ou en désactivant des sécuritées
importante. L'utilisation d'un fichier 'preload' permet d'avoir accès aux deux
contextes temporairement.
 **/

// Modules to control application life and create native browser window
const {app, shell, BrowserWindow, globalShortcut} = require("electron")
const path = require("path");
let fenetre;

// Config
app.commandLine.appendSwitch('ignore-gpu-blacklist');
app.on('ready', () => {
	globalShortcut.register('Control+Shift+I', () => {
		fenetre.webContents.openDevTools();
		return false;
	});
});

// Chemin vers flash
let pluginName
switch (process.platform) {
	case "win32":
		if (process.arch == "x64") {
			pluginName = "flash/windows/26.0.0.151/pepflashplayer_64.dll";
		} else {
			pluginName = "flash/windows/26.0.0.151/pepflashplayer_32.dll";
		}
		break;
	case "darwin":
		pluginName = "flash/mac/26.0.0.151/PepperFlashPlayer.plugin";
		break;
	case "linux":
		if (process.arch == "x64") {
			pluginName = "flash/linux/26.0.0.151/libpepflashplayer_64.so";
		} else {
			pluginName = "flash/linux/26.0.0.151/libpepflashplayer_32.so";
		}
		app.commandLine.appendSwitch("no-sandbox");
		break;
}
app.commandLine.appendSwitch("ppapi-flash-path", path.join(__dirname, pluginName));

function initialisationFenetre() {
	// Création de la fenêtre
	fenetre = new BrowserWindow({
		width: 800,
		height: 600,
		useContentSize: true,
		show: false,
		backgroundColor: "#222222",
		webPreferences: {
			nodeIntegration: false,
			contextIsolation: false,
			enableRemoteModule: false,
			plugins: true,
			//preload: path.join(__dirname, "chargement.js")
		},
		icon: path.join(__dirname, 'ressources/icone.png')
	});
	fenetre.webContents.on('new-window', (event, url) => {
		// Sprawdzamy, czy to link zewnętrzny
		if (url.startsWith('http:') || url.startsWith('https:')) {
			// Blokujemy otwarcie okna wewnątrz Electrona
			event.preventDefault();
			// Otwieramy w domyślnej przeglądarce (np. Firefox, Chrome)
			shell.openExternal(url);
		}
	});

	// Debug
	// this.fenetre.webContents.openDevTools();

	// On enlève le menu
	fenetre.setMenu(null);

	// Chargement de l'index
	// fenetre.loadFile("transformice-electron.html");
	///fenetre.loadURL("http://transformice.com/transformice-electron.html?d=" + Date.now());
	fenetre.loadURL(`file://${__dirname}/game.html`);

	// Affichage de la fenêtre quand elle est prête
	fenetre.once("ready-to-show", () => {
		fenetre.show()
	});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
	initialisationFenetre()

	app.on("activate", function () {
		// On macOS it's common to re-create a window in the app when the
		// dock icon is clicked and there are no other windows open.
		if (BrowserWindow.getAllWindows().length === 0) {
			initialisationFenetre()
		}
	});
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
	if (process.platform !== "darwin") {
		app.quit()
	}
});

