import { app, BrowserWindow, screen } from 'electron'
import '../renderer/store'
const path = require('path')
const { handleUpdate } = require('./update')
const electronApi = require('./electronApi.js')

/**
 * 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`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 563,
    useContentSize: true,
    width: 1000,
    //配置
    // 全屏
    fullscreen: false,
    autoHideMenuBar: true, //隐藏菜单
    webPreferences: {
        // 使渲染进程拥有node环境
        nodeIntegration: true,
        //关闭web权限检查，允许跨域
        webSecurity: false,
    },
  })

  mainWindow.loadURL(winURL)

  //解决报错(source: chrome-devtools://devtools/bundled/inspector.js)
  if (process.env.NODE_ENV === "development") {
    mainWindow.webContents.on("did-frame-finish-load", () => {
      mainWindow.webContents.once("devtools-opened", () => {
        mainWindow.focus();
      });
      mainWindow.webContents.openDevTools();
    });
  }


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

  handleUpdate(mainWindow) // 在这里执行，并把mainWindow传入
}

app.on('ready', createWindow)


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

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

app.on('ready', () => {
    if (process.env.NODE_ENV !== 'production') {
        require('vue-devtools').install()
    }
})

/**
 * 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()
})
 */
