'use strict'
!(function(require, directRequire) {
  module.exports = {
    moveWindowToCenterWithSize: (a, b, c, d = 200) =>
      new Promise(e => {
        var f = Math.round
        const g = a.x,
          h = a.y,
          i = 20
        let j
        for (const a of nw.Screen.screens)
          if (
            g >= a.bounds.x &&
            g <= a.bounds.x + a.bounds.width &&
            h >= a.bounds.y &&
            h <= a.bounds.y + a.bounds.height
          ) {
            j = a
            break
          }
        b > j.bounds.width && (b = j.bounds.width),
          c > j.bounds.height && (c = j.bounds.height)
        const k = (j.bounds.width - b) / 2,
          l = (j.bounds.height - c) / 2,
          m = f((k - g) / (d / i)),
          n = f((l - h) / (d / i)),
          o = f((b - a.width) / (d / i)),
          p = f((c - a.height) / (d / i))
        console.log(g, h, a.width, a.height),
          console.log(k, l, b, c, j.bounds.width, j.bounds.height),
          console.log(m, n, o, p)
        const q = a.width,
          r = a.height,
          s = d / i
        let t = s
        const u = () => {
          0 > t
            ? e()
            : 1 == t
              ? (console.log('last one'),
                t--,
                a.moveTo(k, l),
                setImmediate(() => {
                  a.resizeTo(b, c)
                }),
                console.log('move  ', a.x, a.y, a.width, a.height),
                setTimeout(u, i))
              : (t--,
                a.moveTo(g + m * (s - t), h + n * (s - t)),
                a.resizeTo(q + o * (s - t), r + p * (s - t)),
                console.log('move  ', a.x, a.y, a.width, a.height),
                setTimeout(u, i))
        }
        setTimeout(u, i)
      })
  }
})(require('lazyload'), require)
