import test from 'zopf'
import proxyquire from 'proxyquire'
import path from 'path'
import Promise from 'bluebird'

import fixture from '../fixture'
import electron from '../stubs/electron'
import InstallStore from '../stubs/install-store'
import AppActions from '../stubs/app-actions'

import log from '../../app/util/log'
let logger = new log.Logger({sinks: {console: false}})
let opts = {id: 'kalamazoo', logger}

let setup = t => {
  let os = {}

  let noop = () => Promise.resolve()
  let win32 = {configure: noop}
  let darwin = {configure: noop}
  let linux = {configure: noop}

  let stubs = Object.assign({
    '../util/os': os,
    './configurators/win32': win32,
    './configurators/darwin': darwin,
    './configurators/linux': linux,
    '../stores/install-store': InstallStore,
    '../actions/app-actions': AppActions
  }, electron)

  let configure = proxyquire('../../app/tasks/configure', stubs)

  return {configure, os, win32, linux, darwin}
}

test('configure', t => {
  let objects = setup(t)
  let {configure, os} = objects

  t.case('rejects unsupported platform', t => {
    t.stub(os, 'platform').returns('irix')
    return t.rejects(configure.start(opts))
  })

  ;['win32', 'darwin', 'linux'].forEach((platform) => {
    t.case(platform, t => {
      t.stub(os, 'platform').returns(platform)
      t.mock(objects[platform]).expects('configure').resolves({executables: []})
      return configure.start(opts)
    })
  })

  let win32 = proxyquire('../../app/tasks/configurators/win32', {})
  let win32_path = fixture.path('configure/win32')

  t.case('win32 finds bats and exes', t => {
    let spy = t.spy()
    return win32.configure(win32_path).then(spy).then(_ => {
      let names = [
        'game.exe', 'launcher.bat',
        path.join('resources', 'editor.exe'),
        path.join('resources', 'quite', 'deep', 'share.bat')
      ]
      let paths = names.map(x => path.join(win32_path, x))
      t.is(1, spy.callCount)
      t.samePaths(paths, spy.getCall(0).args[0].executables)
    })
  })

  let common = {
    fix_execs: () => Promise.resolve()
  }
  let darwin = proxyquire('../../app/tasks/configurators/darwin', {
    './common': common
  })
  let darwin_path = fixture.path('configure/darwin')

  t.case('darwin finds app bundles', t => {
    let spy = t.spy()
    return darwin.configure(darwin_path).then(spy).then(_ => {
      let names = [
        'Some Grand Game.app/'
      ]
      let paths = names.map(x => `${darwin_path}/${x}`)
      t.is(1, spy.callCount)
      t.samePaths(paths, spy.getCall(0).args[0].executables)
    })
  })

  t.case('darwin finds binaries when no app bundles', t => {
    let spy = t.spy()
    return darwin.configure(linux_path).then(spy).then(_ => {
      let names = [
        'bin/game32',
        'bin/game64',
        'OpenHexagon',
        'quine'
      ]
      let paths = names.map(x => `${linux_path}/${x}`)
      t.is(1, spy.callCount)
      t.samePaths(paths, spy.getCall(0).args[0].executables)
    })
  })

  let linux = proxyquire('../../app/tasks/configurators/linux', {
    '../../promised/fs': {
      chmodAsync: () => Promise.resolve(),
      '@global': true,
      '@noCallThru': true
    }
  })
  let linux_path = fixture.path('configure/linux')

  t.case('linux finds scripts & binaries', t => {
    let spy = t.spy()
    return linux.configure(linux_path).then(spy).then(_ => {
      let names = [
        'bin/game32',
        'bin/game64',
        'OpenHexagon',
        'quine'
      ]
      let paths = names.map(x => `${linux_path}/${x}`)
      t.is(1, spy.callCount)
      t.samePaths(paths, spy.getCall(0).args[0].executables)
    })
  })
})

@@ -1439,24 +1439,176 @@
    %7D)%0A  %7D)%0A%0A
+  let fs = %7B%0A    chmodAsync: () =%3E Promise.resolve(),%0A    '@global': true,%0A    '@noCallThru': true%0A  %7D%0A  let stubs = %7B%0A    '../../promised/fs': fs%0A  %7D%0A%0A
   let win32 
@@ -1659,18 +1659,21 @@
 win32', 
-%7B%7D
+stubs
 )%0A  let 
@@ -2183,68 +2183,8 @@
 %7D)%0A%0A
-  let common = %7B%0A    fix_execs: () =%3E Promise.resolve()%0A  %7D%0A
   le
@@ -2249,36 +2249,13 @@
 n', 
-%7B%0A    './common': common%0A  %7D
+stubs
 )%0A  
@@ -2653,16 +2653,139 @@
 )%0A  %7D)%0A%0A
+  let linux = proxyquire('../../app/tasks/configurators/linux', stubs)%0A  let linux_path = fixture.path('configure/linux')%0A%0A
   t.case
@@ -3188,262 +3188,14 @@
  %7D)%0A
+
   %7D)%0A%0A
-  let linux = proxyquire('../../app/tasks/configurators/linux', %7B%0A    '../../promised/fs': %7B%0A      chmodAsync: () =%3E Promise.resolve(),%0A      '@global': true,%0A      '@noCallThru': true%0A    %7D%0A  %7D)%0A  let linux_path = fixture.path('configure/linux')%0A%0A
   t.

fix darwin configure spec