const electron = require('electron')
const path = require('path')

// In main process.
const ipcMain = require('electron').ipcMain

const {app, BrowserWindow, Menu} = electron

let mainWindow = null
let sqlWindow = null

const template = [
    {
        label: 'File',
        submenu: [
            {
                label: 'Open...',
                accelerator: 'CmdOrCtrl+O',
                click() {}
            },
            {
                label: 'Save...',
                accelerator: 'CmdOrCtrl+S',
                click() {
                    // We can't call saveFile(content) directly because we need to get
                    // the content from the renderer process. So, send a message to the
                    // renderer, telling it we want to save the file.
                    mainWindow.webContents.send('save-file')
                }
            }
        ]
    },
    {
        label: 'Edit',
        submenu: [
            {
                label: 'Undo',
                accelerator: 'CmdOrCtrl+Z',
                role: 'undo'
            },
            {
                label: 'Redo',
                accelerator: 'Shift+CmdOrCtrl+Z',
                role: 'redo'
            },
            {
                type: 'separator'
            },
            {
                label: 'Cut',
                accelerator: 'CmdOrCtrl+X',
                role: 'cut'
            },
            {
                label: 'Copy',
                accelerator: 'CmdOrCtrl+C',
                role: 'copy'
            },
            {
                label: 'Paste',
                accelerator: 'CmdOrCtrl+V',
                role: 'paste'
            },
            {
                label: 'Select All',
                accelerator: 'CmdOrCtrl+A',
                role: 'selectall'
            }
        ]
    },
    {
        label: 'Developer',
        submenu: [
            {
                label: 'Toggle Developer Tools',
                accelerator: process.platform === 'darwin'
                    ? 'Alt+Command+I'
                    : 'Ctrl+Shift+I',
                click() { mainWindow.webContents.toggleDevTools() }
            }
        ]
    }
]

if (process.platform === 'darwin') {
    const name = app.getName()
    template.unshift({
        label: name,
        submenu: [
            {
                label: 'About ' + name,
                role: 'about'
            },
            {
                type: 'separator'
            },
            {
                label: 'Services',
                role: 'services',
                submenu: []
            },
            {
                type: 'separator'
            },
            {
                label: 'Hide ' + name,
                accelerator: 'Command+H',
                role: 'hide'
            },
            {
                label: 'Hide Others',
                accelerator: 'Command+Alt+H',
                role: 'hideothers'
            },
            {
                label: 'Show All',
                role: 'unhide'
            },
            {
                type: 'separator'
            },
            {
                label: 'Quit',
                accelerator: 'Command+Q',
                click() { app.quit() }
            }
        ]
    })
}

app.on('ready', () => {
    console.log('Application is ready for start.')

    mainWindow = new BrowserWindow({
        width: 1000,
        height: 600,
        useContentSize: true,
        nodeIntegration: true,
        webPreferences: {
            experimentalFeatures: true
        }
    })

    mainWindow.loadURL('file://' + path.join(__dirname, '/../index.html'))

    const menu = Menu.buildFromTemplate(template)
    Menu.setApplicationMenu(menu)

    mainWindow.on('closed', () => {
        mainWindow = null
    })
})

app.on('window-all-closed', app.quit)

ipcMain.on('show-sql', (event, sql) => {
    if (!sqlWindow) {
        sqlWindow = new BrowserWindow({
            width: 700,
            height: 500,
            alwaysOnTop: true,
            useContentSize: true,
            nodeIntegration: true,
            webPreferences: {
                experimentalFeatures: true
            }
        })
    }

    sqlWindow.loadURL('file://' + path.join(__dirname, '/../sql-window.html'))

    // sqlWindow.webContents.openDevTools()

    sqlWindow.sql = sql

    sqlWindow.on('closed', () => {
        sqlWindow = null
    })
})

@@ -3398,17 +3398,17 @@
 width: 1
-0
+1
 00,%0A    
@@ -3423,9 +3423,9 @@
 ht: 
-6
+7
 00,%0A
@@ -3738,32 +3738,98 @@
    mainWindow.on
+ce('ready-to-show', () =%3E %7B mainWindow.show() %7D)%0A    mainWindow.on
 ('closed', () =%3E
@@ -3830,24 +3830,16 @@
  () =%3E %7B
-%0A       
  mainWin
@@ -3844,28 +3844,24 @@
 indow = null
-%0A   
  %7D)%0A%7D)%0A%0Aapp.
@@ -3884,24 +3884,96 @@
 losed', 
-app.quit
+function() %7B%0A    if (process.platform !== 'darwin') %7B%0A        app.quit()%0A    %7D%0A%7D
 )%0A%0AipcMa

add closing app