 
 UPLOAD_CHUNK_SIZE = 128 * 1024 # bytes
 DRAGGING_OVER_DOM = false
-fileBuffer = []
-# current element of checksum and upload queue
-currentDocument =
-  checksum: null
-  upload: null
+importingFilesArrayBuffer = []
 
 # observes files that are processed and removes them from buffer
-processedFiles = ImportingFile.documents.find
-  $or: [
-    finished: true
-  ,
-    errored: true
-  ]
+processedFiles = ImportingFile.documents.find()
 processedFiles.observe
-  added: (document) ->
-    delete fileBuffer[document._id]
-
-# verify or upload next file from queue
-Deps.autorun ->
-  document = ImportingFile.documents.findOne
-    finished: false
-    errored: false
-    sha256:
-      $exists: true
-
-  # document will be null the first time it runs
-  # also, autorun will get called when document status changes
-  # if document id is unchanged it can return
-  return if not document or currentDocument.upload and currentDocument.upload._id == document._id
-  currentDocument.upload = document
-
-  Meteor.call 'create-publication', document.name, document.sha256, (error, result) ->
+  removed: (document) ->
+    console.log "Removing document from buffer " + document.name
+    delete importingFilesArrayBuffer[document._id]
+
+
+verifyFile = (file, publicationId, samples) ->
+  ImportingFile.documents.update file._id,
+    $set:
+      status: "Verifying file"
+
+  samplesData = _.map samples, (sample) ->
+    new Uint8Array file.content.slice sample.offset, sample.offset + sample.size
+  Meteor.call 'verify-publication', publicationId, samplesData, (error) ->
     if error
-      ImportingFile.documents.update document._id,
+      ImportingFile.documents.update file._id,
         $set:
           errored: true
           status: error.toString()
       return
 
-    if result.already
-      ImportingFile.documents.update document._id,
-        $set:
-          finished: true
-          status: "File already imported"
-          publicationId: result.publicationId
-      return
-
-    if result.verify
-      verifyFile fileBuffer[document._id].file, fileBuffer[document._id].fileContent, result.publicationId, result.samples
-    else
-      uploadFile fileBuffer[document._id].file, result.publicationId
-
-# calculate checksum for next file in queue
-Deps.autorun ->
-  document = ImportingFile.documents.findOne
-    preprocessed: true
-    sha256:
-      $exists: false
-
-  # document will be null the first time it runs
-  # also, autorun will get called when document status changes
-  # if document id is unchanged it can return
-  return if not document or currentDocument.checksum and currentDocument.checksum._id == document._id
-  currentDocument.checksum = document
-
-  ImportingFile.documents.update document._id,
-    $set:
-      status: "Computing checksum"
-
-  hash = new Crypto.SHA256
-    onProgress: (progress) ->
-      #TODO: update progressbar
-  hash.update fileBuffer[document._id].fileContent, (error, result) ->
-    #TODO: handle errors
-    if error
-      console.log "Import error: " + error.message
-  hash.finalize (error, result) ->
-    #TODO: handle errors
-    if error
-      console.log "Import error: " + error.message
-
-    ImportingFile.documents.update document._id,
-      $set:
-        sha256: result
-        status: "Checksum computed"
-
-verifyFile = (file, fileContent, publicationId, samples) ->
-  ImportingFile.documents.update file._id,
-    $set:
-      status: "Verifying file"
-
-  samplesData = _.map samples, (sample) ->
-    new Uint8Array fileContent.slice sample.offset, sample.offset + sample.size
-  Meteor.call 'verify-publication', publicationId, samplesData, (error) ->
-    if error
-      ImportingFile.document.update file._id,
-        $set:
-          errored: true
-          status: error.toString()
-      return
-
-    ImportingFile.document.update file._id,
+    ImportingFile.documents.update file._id,
       $set:
         finished: true
         imported: true

        status: "File imported"

uploadFile = (file, publicationId) ->
  ImportingFile.documents.update file._id,
    $set:
      status: "Uploading file"
  meteorFile = new MeteorFile file,
    collection: ImportingFile.Meta.collection


  # imported: true if file was successfully imported
  # publicationId: publication ID for the imported file
  # sha256: SHA256 hash for the file
  # buffered: true when file object is added to buffer

  @Meta
    name: 'ImportingFile'
