import { app, BrowserWindow,BrowserView, ipcMain,Menu,shell,screen,ipcRenderer} from 'electron'
import { runInDebugContext } from 'vm'
const isMac = process.platform === 'darwin'
let FlexRAN = false
let url = "ustb-oai.com"
const template = [
  // { role: 'appMenu' }
  ...(isMac ? [{
    label: app.name,
    submenu: [
      { role: 'about' },
      { type: 'separator' },
      { role: 'services' },
      { type: 'separator' },
      { role: 'hide' },
      { role: 'hideothers' },
      { role: 'unhide' },
      { type: 'separator' },
      { role: 'quit' }
    ]
  }] : []),
  // { role: 'fileMenu' }
  {
    label: '查看',
    submenu: [
      {
        label: 'AI-Engine Jupyter Lab ',
        click() {

            openFlexran()
          
        },
      },
      {
        label: 'AI-Engine Code-Server ',
        click: async () => {
          const { shell } = require('electron')
          await shell.openExternal('https://code.ustb-oai.com:8889')
        }
      },     
    ]
  },
  // {
  //   label: '远程连接',
  //   submenu: [
  //     {
  //       label: 'OAI核心网',
  //       click() {
  //         createNewWindow("192.168.1.12") 
  //       },
  //     },
  //     {
  //       label: 'OAI基站',
  //       click() {
  //         createNewWindow("192.168.1.193") 
  //       },
  //     },
  //     {
  //       label: 'OAI-SIM',
  //       click() {
  //         createNewWindow("192.168.1.212") 
  //       },
  //     },
  //     {
  //       label: 'AI-Engine',
  //       click() {
  //         createNewWindow("192.168.1.150",'tony','199710') 
  //       },
  //     },
      
  //   ]
  // },
  {
    label: '编辑',
    submenu: [
      { role: 'undo' },
      { role: 'redo' },
      { type: 'separator' },
      { role: 'cut' },
      { role: 'copy' },
      { role: 'paste' },
      ...(isMac ? [
        { role: 'pasteAndMatchStyle' },
        { role: 'delete' },
        { role: 'selectAll' },
        { type: 'separator' },
        {
          label: 'Speech',
          submenu: [
            { role: 'startSpeaking' },
            { role: 'stopSpeaking' }
          ]
        }
      ] : [
        { role: 'delete' },
        { type: 'separator' },
        { role: 'selectAll' }
      ])
    ]
  },
  {
    label: '运行',
    submenu: [
      {
        label: '运行OAI基站（USRP B210）',
        click() {
          runeNB()
        },
      },
      { type: 'separator' },
      {
        label: 'OAI基站（L2-nFAPI）',

        submenu: [
          { label: '启动OAI基站',
          click() {
            addUE()
          },
         },
        
        ]
      },

      
    ]
  },
  {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click: async () => {
          const { shell } = require('electron')
          await shell.openExternal('http://ustb-oai.com')
        }
      }
    ]
  }
]

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow,view,child
let newWindows=[]
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function runeNB(){
  mainWindow.webContents.send('eNB', '/page1');
}
function runeNBl2(){
  mainWindow.webContents.send('eNBl2', '/page1');
}
function addUE(){
  mainWindow.webContents.send('addUE', '/page1');
}


function createNewWindow(suburl){
  let win
    /**
   * Initial window options
   */
     win = new BrowserWindow({
      minWidth:300,
      minHeight:300,
      width: 300, 
      height: 600, 
      useContentSize: true,
      parent:mainWindow,
      webPreferences: {
        nodeIntegration: true,
        webSecurity: false
    }
    })
    
    // win.loadURL(winURL+`/#/ssh/${host}/start_enb/${user==undefined?'root':user}/${pass==undefined?'199710':pass}/`)
    win.loadURL(`http://code.ustb-oai.com:2222/ssh/host/${host}?autologin=1&user=${user==undefined?'root':user}&pass=${pass==undefined?'199710':pass}`)
    //+`/#/ssh/${host}/start_enb/${user==undefined?'root':user}/${pass==undefined?'199710':pass}/`
    newWindows.push(win)
}

function createWindow () {
  
Menu.setApplicationMenu( Menu.buildFromTemplate(template));

  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    minWidth:920,
    minHeight:500,
    width: screen.getPrimaryDisplay().workAreaSize.width, 
    height: screen.getPrimaryDisplay().workAreaSize.height, 
    useContentSize: true,
    webPreferences: {
      nodeIntegration: true,
      webSecurity: false
  }
  })
  mainWindow.loadURL(winURL)

  //mainWindow.isMaximized() ? mainWindow.unmaximize() : mainWindow.maximize()
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}
 let openFlexran=async() =>{
  // child = new BrowserWindow({
  //   minWidth:500,
  //   minHeight:500,
  //   width: 900, 
  //   height: 600, 
  //   parent:mainWindow,
  //   useContentSize: true,
  //   webPreferences: {
  //     nodeIntegration: true,
  //     webSecurity: false
  // }
  // })
  // console.log(winURL)
  // child.loadURL(`http://${url}:8888`)
  await shell.openExternal(`http://${url}:8888/lab/tree/dqn_zoo`)

};






app.on('ready', createWindow)




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

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

app.on('resize', (e, cmd) => {
  view.setBounds({ x:  screen.getPrimaryDisplay().workAreaSize.width/2, y: 0, width:  screen.getPrimaryDisplay().workAreaSize.width/2, height: screen.getPrimaryDisplay().workAreaSize.height/2 })
  console.log("aaa")
})

ipcMain.on('change-url', function(event, arg) {
  console.log("ssss");  // prints "ping"
  url = arg
  event.returnValue = 'pong';
});

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
