   # 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'


      # So that meteor-file knows what to update
      file._id = id

      # We make sure list of files is rendered before hashing
      Deps.flush()

      # TODO: Remove the following workaround for a bug
      # Deps.flush does not seem to really do it, so we have to use Meteor.setTimeout to workaround
      # See: https://github.com/meteor/meteor/issues/1619
      Meteor.setTimeout ->
        # TODO: We should read in chunks, not whole file
        reader.readAsArrayBuffer file
      , 5 # ms, 0 does not seem to work, 5 seems to work

hideOverlay = ->
  allCount = ImportingFile.documents.find().count()

          imported: true
          publicationId: publicationId

# process next element from import queue
Deps.autorun ->
  # We exclude status and progress fields so that function
  # doesn't get recalled on every status or progress change
  document = ImportingFile.documents.findOne
    finished: false
    errored: false
    sha256:
      $exists: true
  ,
    fields:
      status: 0
      uploadProgress: 0
      readProgress: 0
  return unless document

  console.log "Autorun called for import queue " + document.name

  Meteor.call 'create-publication', document.name, document.sha256, (error, result) ->
    if error
      ImportingFile.documents.update document._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 importingFilesArrayBuffer[document._id], result.publicationId, result.samples
    else
      uploadFile importingFilesArrayBuffer[document._id], result.publicationId


computeChecksum = (file, callback) ->
  hash = new Crypto.SHA256
    onProgress: (progress) ->
      #TODO: update progressbar
  hash.update file.content, (error, result) ->
    if error
      ImportingFile.documents.update file._id,
        $set:
          errored: true
          status: error.toString()
    return
  hash.finalize callback

testPDF = (file, callback) ->
  PDFJS.getDocument(data: file.content, password: '').then callback, (message, exception) ->
    ImportingFile.documents.update file._id,
      $set:
        errored: true
        status: "Invalid PDF file"

# process next element from preprocess queue
Deps.autorun ->
  # we exclude status field so that it doesn't get recalled on status change
  document = ImportingFile.documents.findOne
    sha256:
      $exists: false
    buffered: true
  ,
    fields:
      status: 0
  return unless document

  ImportingFile.documents.update document._id,
    $set:
      status: "Preprocessing"

  console.log "Called autorun for preprocessing queue " + document.name

  reader = new FileReader()
  reader.onload = ->
    importingFilesArrayBuffer[document._id].content = @result
    console.log "Testing PDF with PDFJS"
    testPDF importingFilesArrayBuffer[document._id], ->
      console.log "Computing checksum"
      computeChecksum importingFilesArrayBuffer[document._id], (error, result) ->
        if error
          ImportingFile.documents.update document._id,
            $set:
              errored: true
              status: error.toString()
          return

        ImportingFile.documents.update document._id,
          $set:
            sha256: result
            status: "In import queue"

  #TODO: read file in chunks
  reader.readAsArrayBuffer importingFilesArrayBuffer[document._id]


importFile = (file) ->
  ImportingFile.documents.insert
    name: file.name
    status: "In preprocessing queue"
    readProgress: 0
    uploadProgress: 0
    finished: false
