const { app } = require('electron');
const { BrowserWindow } = require('electron')
const fs = require('fs');
const path = require('path');
const process = require('child_process').spawn;
const portfinder = require('detect-port');
let io, browserWindows, ipc, apiProcess, loadURL;
let appApi, menu, dialog, notification, tray, webContents;
let globalShortcut, shell, screen, clipboard;
let loadingWindow;

app.on('ready', () => {

    // yf add 
    startLoadingWindow();

    portfinder(8000, (error, port) => {
        startSocketApiBridge(port);
    });
});

function startSocketApiBridge(port) {
    io = require('socket.io')(port);

    // yf add
    startAspCoreBackend(port);

    io.on('connection', (socket) => {

        global.elesocket = socket;
        global.elesocket.setMaxListeners(0);
        console.log('ASP.NET Core Application connected...', 'global.elesocket', global.elesocket.id, new Date());

        appApi = require('./api/app')(socket, app);
        browserWindows = require('./api/browserWindows')(socket);
        ipc = require('./api/ipc')(socket);
        menu = require('./api/menu')(socket);
        dialog = require('./api/dialog')(socket);
        notification = require('./api/notification')(socket);
        tray = require('./api/tray')(socket);
        webContents = require('./api/webContents')(socket);
        globalShortcut = require('./api/globalShortcut')(socket);
        shell = require('./api/shell')(socket);
        screen = require('./api/screen')(socket);
        clipboard = require('./api/clipboard')(socket);
    });
}

function startAspCoreBackend(electronPort) {
    portfinder(8000, (error, electronWebPort) => {
        loadURL = `http://localhost:${electronWebPort}`
        const parameters = [`/electronPort=${electronPort}`, `/electronWebPort=${electronWebPort}`];

        const manifestFile = require("./bin/electron.manifest.json");
        let binaryFile = manifestFile.executable;

        const os = require("os");
        if (os.platform() === "win32") {
            binaryFile = binaryFile + '.exe';
        }

        const binFilePath = path.join(__dirname, 'bin', binaryFile);
        apiProcess = process(binFilePath, parameters);

        apiProcess.stdout.on('data', (data) => {
            var text = data.toString();
            console.log(`stdout: ${data.toString()}`);

            if (data.toString().indexOf("MainWindowShowed") > -1 && loadingWindow && !loadingWindow.isDestroyed()) {
                loadingWindow.close();
            }
        });
    });
}

function startLoadingWindow() {
    const manifestFile = require("./bin/electron.manifest.json");
    let loadingUrl = manifestFile.loadingUrl;
    let icon = manifestFile.icon;
    if (loadingUrl) {
        loadingWindow = new BrowserWindow({
            width: manifestFile.width,
            height: manifestFile.height,
            transparent: true,
            frame: false,
            show: false,
            devTools: true,
            icon: path.join(__dirname, icon)
        })
        if (manifestFile.devTools) {
            loadingWindow.webContents.openDevTools();
        }
        loadingWindow.loadURL(loadingUrl);
        loadingWindow.once('ready-to-show', () => {
            loadingWindow.show()
        })
        loadingWindow.on('closed', () => {
            loadingWindow = null
        })
    }
}

//app.on('activate', () => {
    // 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 (win === null) {
//        createWindow();
//    }
//});
@@ -26,24 +26,34 @@
 electron');%0A
+// yf add%0A
 const %7B Brow
@@ -86,16 +86,17 @@
 ctron')%0A
+%0A
 const fs
@@ -388,16 +388,27 @@
 ipboard;
+%0A%0A// yf add
 %0Alet loa
@@ -418,16 +418,412 @@
 gWindow;
+%0Alet mainWindowId;%0A%0A// yf add%0Aconst manifestJsonFile = require(%22./bin/electron.manifest.json%22);%0Aif (manifestJsonFile.singleInstance) %7B%0A    const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) =%3E %7B%0A        mainWindowId && BrowserWindow.fromId(mainWindowId) && BrowserWindow.fromId(mainWindowId).show();%0A    %7D);%0A    if (shouldQuit) %7B%0A        app.quit();%0A        return;%0A    %7D%0A%7D
 %0A%0Aapp.on
@@ -2935,130 +2935,161 @@
     
-%7D%0A        %7D);%0A    %7D);%0A%7D%0A%0Afunction startLoadingWindow() %7B%0A    const manifestFile = require(%22./bin/electron.manifest.json%22);
+    mainWindowId = parseInt(data.toString().replace(%22MainWindowShowed:%22, %22%22).trim())%0A            %7D%0A        %7D);%0A    %7D);%0A%7D%0A%0Afunction startLoadingWindow() %7B
 %0A   
@@ -3110,24 +3110,28 @@
 l = manifest
+Json
 File.loading
@@ -3154,24 +3154,28 @@
 n = manifest
+Json
 File.icon;%0A 
@@ -3262,24 +3262,28 @@
 th: manifest
+Json
 File.width,%0A
@@ -3306,24 +3306,28 @@
 ht: manifest
+Json
 File.height,
@@ -3509,24 +3509,28 @@
 if (manifest
+Json
 File.devTool

add makeSingleInstance