//const { app, BrowserWindow, Menu } = require('electron')
const path = require('path')

const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const Menu = electron.Menu
const app = electron.app

//var searcher = require('./searcher');
let win = null;
 
function createWindow () {
   win = new BrowserWindow({
    width: 850,
    height: 800,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true,
      contextIsolation: false
    }
  })

   // Set menu to window
   const menu = Menu.buildFromTemplate(template)
   Menu.setApplicationMenu(menu)

  //win.webContents.openDevTools() //Debug Tools
  win.loadFile('index.html')
  //searcher.send_brocast_mtk();
}

var template = [{
  label: 'Search',
  submenu: [{
    label: 'Lan Search',
    accelerator: 'CmdOrCtrl+L',
//    click:()=>searcher.send_brocast()
      click:()=>win.webContents.send('lan_search', "searching from menu")      
  },{
    type: 'separator'
  }
 /* 
,{
    label: 'Internet Search',
    accelerator: 'CmdOrCtrl+I',
    
  } 
 */
]
}, {
  label: 'Help',
  role: 'help',
  submenu: [{
    label: 'Help',
    click: function () {
      electron.shell.openExternal('http://www.aviosys.com')
    }
  },{
    label: 'Ver:1.0.0'  
  }]
}];


app.whenReady().then(() => {

  createWindow()
 
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })

  win.webContents.on('did-finish-load', () => {
    win.webContents.send('lan_search', "searchering from main star");    
  })
  
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

const { ipcMain } = require('electron')
let edit_win = null;
ipcMain.on('open_edit_window', (event, arg) => {
  console.log(arg);
  event.reply('open_edit_window', 'open edit window ok');

  //const { BrowserWindow } = require('electron');
  edit_win = new BrowserWindow({ 
    width: 500,
    height: 600,
    show: false ,
    webPreferences: {
      //preload: path.join(__dirname, 'edit_preload.js'),
      nodeIntegration: true,
      contextIsolation: false,
    }
  });
  edit_win.removeMenu();
  //edit_win.webContents.openDevTools()
  edit_win.loadFile('editor_mtk.html');
  edit_win.once('ready-to-show', () => {
    edit_win.webContents.send('load_data', arg); 
    edit_win.show();    
  })

})

ipcMain.on('close_edit_window', (event, arg) => {
  console.log(arg); 
  //Albert 2021/06/01 begin
  try {
    edit_win.close();
  }catch(err) {
  }
  //Albert 2021/06/01 end
})

//Albert 2021/05/27 begin
ipcMain.on('open_tf_edit_window', (event, arg) => {
  console.log(arg);
  event.reply('open_tf_edit_window', 'open tf edit window ok');

  edit_win = new BrowserWindow({ 
    width: 500,
    height: 600,
    show: false ,
    webPreferences: {
      //preload: path.join(__dirname, 'edit_preload.js'),
      nodeIntegration: true,
      contextIsolation: false
    }
  });
  edit_win.removeMenu();
  //edit_win.webContents.openDevTools()
  edit_win.loadFile('editor_tf.html');
  edit_win.once('ready-to-show', () => {
    edit_win.webContents.send('load_tf_data', arg);
    edit_win.show()  
  })

})
//Albert 2021/05/27 end


// ipcMain.on('synchronous-message', (event, arg) => {
//   console.log(arg) // prints "ping"
//   event.returnValue = 'pong'
// })
