import { app, ipcMain } from 'electron';
import { autoUpdater } from 'electron-updater';

export default (params) => {
  if (process.platform === 'darwin' || process.platform === 'win32') {
    // autoUpdater.setFeedURL(updateUrl);
    ipcMain.on('autoUpdate', (event, args) => {
      try {
        autoUpdater.allowPrerelease = Boolean(params.settings.get('beta'));
        if (args.action === 'check') {
          autoUpdater.checkForUpdates();
        } else if (args.action === 'install') {
          console.log('install update');
          autoUpdater.quitAndInstall();
          // we need to send a quit event
          setTimeout(() => {
            app.quit();
          }, 20);
        }
      } catch (e) {
        console.error(e);
        event.sender.send('autoUpdate', { error: true });
      }
    });

    autoUpdater.on('update-not-available', () => {
      console.log('update-not-available');
      params.mainWindow.webContents.send('autoUpdate', { available: false });
    });

    autoUpdater.on('update-available', () => {
      console.log('update-available');
      params.mainWindow.webContents.send('autoUpdate', { available: true });
    });

    autoUpdater.on('download-progress', (progressObj) => {
      let logMessage = `Download speed: ${progressObj.bytesPerSecond}`;
      logMessage = `${logMessage} - Downloaded ${progressObj.percent}%`;
      logMessage = `${logMessage} (${progressObj.transferred}/${progressObj.total})`;

      console.log(logMessage);
    });

    autoUpdater.on('update-downloaded', () => {
      console.log('update-downloaded');
      params.mainWindow.webContents.send('autoUpdate', { downloaded: true });
    });

    autoUpdater.on('error', () => {
      console.log('update-error');
      params.mainWindow.webContents.send('autoUpdate', { error: true });
    });
  }
};

@@ -81,16 +81,66 @@
 pdater';
+%0Aimport %7B isDevMode %7D from '../../environment.js';
 %0A%0Aexport
@@ -166,16 +166,31 @@
 %3E %7B%0A  if
+ (!isDevMode &&
  (proces
@@ -245,16 +245,17 @@
 'win32')
+)
  %7B%0A    /

Remove auto-update in development