var hyperdrive = require('hyperdrive')
var level = require('level')
var raf = require('random-access-file')
var untildify = require('untildify')
var path = require('path')
var exists = require('path-exists').sync
var mkdirp = require('mkdirp').sync

var dir = untildify('~/.sciencefair/data/elife_dws/')
mkdirp(dir)
var key = '4b2e3d9a1a41f44658f3f89afba5e0c0e166bcbe20c5bcc276ef3828ca4db717'

var drive = hyperdrive(level(untildify('~/.sciencefair/data/elife_dws.db')))
var archive = drive.createArchive(
  key,
  {
    live: true,
    file: function (name) {
      return raf(dir + name)
    }
  }
)

var discovery = require('discovery-swarm')
var defaults = require('datland-swarm-defaults')

var swarm = discovery(defaults({
  hash: false,
  stream: function () {
    return archive.replicate()
  }
}))

swarm.join(archive.discoveryKey)

var through = require('through2')
var pump = require('pump')

function download (id, cb) {
  console.log(id)
  pump(archive.list(), through.obj(visit), cb)

  function visit (entry, enc, cb) {
    var folder = entry.name.split('/')[0]
    if (folder === id) {
      console.log('found entry, downloading')
      return archive.download(entry, cb)
    }
    cb()
  }
}

function metadata (meta, cb) {
  pump(archive.list(), through.obj(visit), cb)

  function visit (entry, enc, cb) {
    var parts = path.parse(entry.name)
    if (parts.ext === '.json') {
      meta.push(require(path.join(dir, entry.name)))
    }
    cb()
  }
}

var yuno = require('yunodb')

var dbopts = {
  location: untildify('~/.sciencefair/data/elife_dws.yuno'),
  keyField: '$.identifier[?(@.type === "doi")].id',
  indexMap: {
    'title': true,
    'author[*].surname': true,
    'year': false
  }
}

module.exports = {
  name: 'eLife hyperdrive',
  key: key,
  dir: 'elife_dws',
  connect: connect,
  downloadPaper: downloadPaper
}

function downloadPaper (paper, cb) {
  if (paper.assetPaths().length > 0) {
    cb()
  } else {
    download(paper.path, cb)
  }
}

function connect (cb) {
  var db = yuno(dbopts, (err, dbhandle) => {
    if (err) throw err

    db = dbhandle
    db.datasource = { name: 'eLife hyperdrive' }

    if (!exists(dbopts.location)) {
      var meta = []
      metadata(meta, function () {
        db.add(meta, function (err) {
          if (err) cb(err)
          cb(null, db)
        })
      })
    } else {
      cb(null, db)
    }
  })
}

@@ -293,16 +293,17 @@
 life_dws
+2
 /')%0Amkdi
@@ -325,72 +325,72 @@
  = '
-4b2e3d9a1a41f44658f3f89afba5e0c0e166bcbe20c5bcc276ef3828ca4db717
+c31c23c1d803caf53974dab82126956a03d30fc6c29eb2b5fdd35705f3d70d6f
 '%0A%0Av
@@ -457,16 +457,17 @@
 life_dws
+2
 .db')))%0A
@@ -1565,16 +1565,17 @@
 life_dws
+2
 .yuno'),
@@ -1702,16 +1702,41 @@
  'year':
+ false,%0A    'identifier':
  false%0A 
@@ -1817,16 +1817,17 @@
 life_dws
+2
 ',%0A  con

use tiny db