// This helper remembers the size and position of your windows (and restores
// them in that place after app relaunch).
// Can be used for more than one window, just construct many
// instances of it and give each different name.

import { app, BrowserWindow, screen } from 'electron';
import jetpack from 'fs-jetpack';

export default function (name, options) {

  var userDataDir = jetpack.cwd(app.getPath('userData'));
  var stateStoreFile = 'window-state-' + name +'.json';
  var defaultSize = {
    width: options.width,
    height: options.height
  };
  var state = {};
  var win;

  var restore = function () {
    var restoredState = {};
    try {
      restoredState = userDataDir.read(stateStoreFile, 'json');
    } catch (err) {
      // For some reason json can't be read (might be corrupted).
      // No worries, we have defaults.
    }
    return Object.assign({}, defaultSize, restoredState);
  };

  var getCurrentPosition = function () {
    var position = win.getPosition();
    var size = win.getSize();
    return {
      x: position[0],
      y: position[1],
      width: size[0],
      height: size[1]
    };
  };

  var windowWithinBounds = function (windowState, bounds) {
    return windowState.x >= bounds.x &&
            windowState.y >= bounds.y &&
            windowState.x + windowState.width <= bounds.x + bounds.width &&
            windowState.y + windowState.height <= bounds.y + bounds.height;
  };

  var resetToDefaults = function () {
    var bounds = screen.getPrimaryDisplay().bounds;
    return Object.assign({}, defaultSize, {
      x: (bounds.width - defaultSize.width) / 2,
      y: (bounds.height - defaultSize.height) / 2
    });
  };

  var ensureVisibleOnSomeDisplay = function (windowState) {
    var visible = screen.getAllDisplays().some(function (display) {
      return windowWithinBounds(windowState, display.bounds);
    });
    if (!visible) {
      // Window is partially or fully not visible now.
      // Reset it to safe defaults.
      return resetToDefaults(windowState);
    }
    return windowState;
  };

  var saveState = function () {
    if (!win.isMinimized()) {
      Object.assign(state, getCurrentPosition());
    }
    userDataDir.write(stateStoreFile, state, { atomic: true });
  };

  state = ensureVisibleOnSomeDisplay(restore());

  win = new BrowserWindow(Object.assign({}, options, state));

  win.on('close', saveState);

  return win;
}

@@ -356,22 +356,23 @@
 ions) %7B%0A
-%0A
   
-var
+const
  userDat
@@ -414,27 +414,29 @@
 rData'));%0A  
-var
+const
  stateStoreF
@@ -556,19 +556,19 @@
 %0A  %7D;%0A  
-var
+let
  state =
@@ -574,19 +574,19 @@
 = %7B%7D;%0A  
-var
+let
  win;%0A%0A 
@@ -618,19 +618,19 @@
 ) %7B%0A    
-var
+let
  restore
@@ -913,19 +913,21 @@
   %7D;%0A%0A  
-var
+const
  getCurr
@@ -962,20 +962,20 @@
     
-var position
+const %5Bx, y%5D
  = w
@@ -1000,16 +1000,29 @@
     
-var size
+const %5Bwidth, height%5D
  = w
@@ -1031,24 +1031,25 @@
 .getSize();%0A
+%0A
     return %7B
@@ -1060,43 +1060,17 @@
    x
-: position%5B0%5D,%0A      y: position%5B1%5D
+,%0A      y
 ,%0A  
@@ -1082,17 +1082,8 @@
 idth
-: size%5B0%5D
 ,%0A  
@@ -1096,17 +1096,9 @@
 ight
-: size%5B1%5D
+,
 %0A   
@@ -1109,19 +1109,21 @@
   %7D;%0A%0A  
-var
+const
  windowW
@@ -1454,18 +1454,24 @@
     
-var
+const %7B
  bounds
+ %7D
  = s
@@ -1495,23 +1495,16 @@
 isplay()
-.bounds
 ;%0A    re
@@ -1656,19 +1656,21 @@
   %7D;%0A%0A  
-var
+const
  ensureV
@@ -1720,19 +1720,21 @@
 ) %7B%0A    
-var
+const
  visible
@@ -2048,19 +2048,21 @@
   %7D;%0A%0A  
-var
+const
  saveSta

update to ES6 style