import { app, BrowserWindow,screen } from 'electron'
import express from "express"

const electron = require('electron')
const ipc = electron.ipcMain

/**
 * 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
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `http://localhost:8888/index.html`

app.commandLine.appendSwitch('ppapi-flash-path',app.getPath('pepperFlashSystemPlugin'))
// app.commandLine.appendSwitch('ppapi-flash-version', '32.0.0.363')

function createWindow () {

  let size = screen.getPrimaryDisplay().workAreaSize
  let width = parseInt(size.width)
  let height = parseInt(size.height)

  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    width: width,
    height: height,
    show:false,
    titleBarStyle:'hiddenInset',
    autoHideMenuBar: true,
    webPreferences: {
      // 使用插件
      plugins: true,
      // 允许跨域
      webSecurity: false,
      // 渲染进程是否使用node
      nodeIntegration: true
    },
  })

  // 配置ESC键退出全屏
  // globalShortcut.register('ESC', () => {
  //   mainWindow.setFullScreen(false)
  // })

  mainWindow.loadURL(winURL)

  //进入软件即开启全屏
  mainWindow.setFullScreen(true)

  // mainWindow.webContents.openDevTools();

  mainWindow.once('ready-to-show', () => {
    mainWindow.show()
  })

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

  if (process.env.NODE_ENV === "production") {
      localServer()
  }
}

app.on('ready', createWindow)

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

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

// 最小化窗口
ipc.on('window-min',function(){
  mainWindow.minimize()
})

// 关闭窗口
ipc.on('window-close',function() {
  mainWindow.close()
})

function localServer() {
  let server = express()
  server.use(express.static(__dirname))
  server.listen(8888)
}
