var exec = require('cordova/exec')

var FileTransferManager = function (options) {
  this._handlers = {
    event: []
  }
  this.options = options
  var that = this
  this.options.callback = function (result) {
    that.emit('event', result)
  }
  if (!this.options.parallelUploadsLimit) {
    this.options.parallelUploadsLimit = 1
  }
  exec(this.options.callback, null, 'FileTransferBackground', 'initManager', [this.options])
}

FileTransferManager.prototype.startUpload = function (payload) {
  if (!payload) {
    return this.options.callback({ state: 'FAILED', error: 'upload settings object is missing or invalid argument' })
  }

  if (!payload.id) {
    return this.options.callback({ state: 'FAILED', error: 'upload id is required' })
  }

  if (!payload.serverUrl) {
    return this.options.callback({ id: payload.id, state: 'FAILED', error: 'server url is required' })
  }

  if (payload.serverUrl.trim() === '') {
    return this.options.callback({ id: payload.id, state: 'FAILED', error: 'invalid server url' })
  }

  if (!payload.filePath) {
    if (payload.file) {
      payload.filePath = payload.file
    } else {
      return this.options.callback({ id: payload.id, state: 'FAILED', error: 'filePath is required' })
    }
  }

  if (!payload.fileKey) {
    payload.fileKey = 'file'
  }

  if (payload.showNotification === null || payload.showNotification === undefined) {
    payload.showNotification = true
  }

  if (!payload.headers) {
    payload.headers = {}
  }

  if (!payload.parameters) {
    payload.parameters = {}
  }

  var self = this
  window.resolveLocalFileSystemURL(payload.filePath, function (entry) {
    payload.filePath = entry.toURL().replace('file://', '')
    exec(self.options.callback, null, 'FileTransferBackground', 'startUpload', [payload])
  }, function () {
    self.options.callback({ id: payload.id, state: 'FAILED', error: 'file does not exist: ' + payload.filePath })
  })
}

FileTransferManager.prototype.removeUpload = function (id, successCb, errorCb) {
  if (!id) {
    return errorCb({ error: 'upload id is required' })
  }
  exec(successCb, errorCb, 'FileTransferBackground', 'removeUpload', [id])
}

FileTransferManager.prototype.acknowledgeEvent = function (id, successCb, errorCb) {
  if (!id && errorCb) {
    return errorCb({ error: 'event id is required' })
  }
  exec(successCb, errorCb, 'FileTransferBackground', 'acknowledgeEvent', [id])
}

/**
 * Listen for an event.
 *
 * Any event is supported, but the following are built-in:
 *
 *   - registration
 *   - notification
 *   - error
 *
 * @param {String} eventName to subscribe to.
 * @param {Function} callback triggered on the event.
 */

FileTransferManager.prototype.on = function (eventName, callback) {
  if (!Object.prototype.hasOwnProperty.call(this._handlers, eventName)) {
    this._handlers[eventName] = []
  }
  this._handlers[eventName].push(callback)
}

/**
 * Remove event listener.
 *
 * @param {String} eventName to match subscription.
 * @param {Function} handle function associated with event.
 */

FileTransferManager.prototype.off = function (eventName, handle) {
  if (Object.prototype.hasOwnProperty.call(this._handlers, eventName)) {
    var handleIndex = this._handlers[eventName].indexOf(handle)
    if (handleIndex >= 0) {
      this._handlers[eventName].splice(handleIndex, 1)
    }
  }
}

/**
 * Emit an event.
 *
 * This is intended for internal use only.
 *
 * @param {String} eventName is the event to trigger.
 * @param {*} all arguments are passed to the event listeners.
 *
 * @return {Boolean} is true when the event is triggered otherwise false.
 */

FileTransferManager.prototype.emit = function () {
  var args = Array.prototype.slice.call(arguments)
  var eventName = args.shift()
  if (!Object.prototype.hasOwnProperty.call(this._handlers, eventName)) { return false }

  for (var i = 0, length = this._handlers[eventName].length; i < length; i++) {
    var callback = this._handlers[eventName][i]
    if (typeof callback === 'function') {
      callback.apply(undefined, args)
    } else {
      console.log('event handler: ' + eventName + ' must be a function')
    }
  }
  return true
}

module.exports = {
  init: function (options) {
    return new FileTransferManager(options || {})
  },
  FileTransferManager: FileTransferManager
}

@@ -80,47 +80,8 @@
 ) %7B%0A
-  this._handlers = %7B%0A    event: %5B%5D%0A  %7D%0A
   th
@@ -2370,484 +2370,102 @@
 %0A%7D%0A%0A
-/**%0A * Listen for an event.%0A *%0A * Any event is supported, but the following are built-in:%0A *%0A *   - registration%0A *   - notification%0A *   - error%0A *%0A * @param %7BString%7D eventName to subscribe to.%0A * @param %7BFunction%7D callback triggered on the event.%0A */%0A%0AFileTransferManager.prototype.on = function (eventName, callback) %7B%0A  if (!Object.prototype.hasOwnProperty.call(this._handlers, eventName)) %7B%0A    this._handlers%5BeventName%5D = %5B%5D%0A  %7D%0A  this._handlers%5BeventName%5D.push(
+FileTransferManager.prototype.on = function (eventName, callback) %7B%0A  this.callback = 
 callback
 )%0A%7D%0A
@@ -2464,1099 +2464,256 @@
 back
-)
 %0A%7D%0A%0A
-/**%0A * Remove event listener.%0A *%0A * @param %7BString%7D eventName to match subscription.%0A * @param %7BFunction%7D handle function associated with event.%0A */%0A%0AFileTransferManager.prototype.off = function (eventName, handle) %7B%0A  if (Object.prototype.hasOwnProperty.call(this._handlers, eventName)) %7B%0A    var handleIndex = this._handlers%5BeventName%5D.indexOf(handle)%0A    if (handleIndex %3E= 0) %7B%0A      this._handlers%5BeventName%5D.splice(handleIndex, 1)%0A    %7D%0A  %7D%0A%7D%0A%0A/**%0A * Emit an event.%0A *%0A * This is intended for internal use only.%0A *%0A * @param %7BString%7D eventName is the event to trigger.%0A * @param %7B*%7D all arguments are passed to the event listeners.%0A *%0A * @return %7BBoolean%7D is true when the event is triggered otherwise false.%0A */%0A%0AFileTransferManager.prototype.emit = function () %7B%0A  var args = Array.prototype.slice.call(arguments)%0A  var eventName = args.shift()%0A  if (!Object.prototype.hasOwnProperty.call(this._handlers, eventName)) %7B return false %7D%0A%0A  for (var i = 0, length = this._handlers%5BeventName%5D.length; i %3C length; i++) %7B%0A    var callback = this._handlers%5BeventName%5D%5Bi%5D%0A    if (typeof 
+FileTransferManager.prototype.off = function (eventName, handle) %7B%0A  this.callback = null%0A%7D%0A%0AFileTransferManager.prototype.emit = function () %7B%0A  var args = Array.prototype.slice.call(arguments)%0A  var eventName = args.shift()%0A  if (typeof this.
 call
@@ -2739,18 +2739,21 @@
 ) %7B%0A    
-  
+this.
 callback
@@ -2778,18 +2778,16 @@
 args)%0A  
- 
- 
 %7D else %7B
@@ -2783,26 +2783,24 @@
 %0A  %7D else %7B%0A
-  
     console.
@@ -2858,22 +2858,16 @@
 ction')%0A
-    %7D%0A
   %7D%0A  re

simplify event emitter