var ScuttlebuttSecure = require('../')
var Feed = require('../feed')
var level = require('level-test')()
var tape = require('tape')
var ecc  = require('eccjs')
var k256 = ecc.curves.k256
var pull = require('pull-stream')
var proquint = require('proquint-')

var db = ScuttlebuttSecure(
          level('scuttlebutt-secure-feeds', {
            keyEncoding: 'binary', valueEncoding: 'binary'
          })
        )


function writeMessages (feed, ary, cb) {
  pull(
    pull.values(ary),
    pull.asyncMap(function (m, cb) {
      feed.append('message', m, cb)
    }),
    pull.drain(null, cb)
  )
}

function hex (b) {
  var s = b.toString('hex'), o = ''
  while(s.length) {
    if(o) o += '\n'
    o += s.substring(0, 64)
    s = s.substring(64)
  }
  return o

}

function toHuman (msg) {
  return (
    proquint.encodeCamel(msg.author).substring(0, 10*4) + ' / ' +
    msg.sequence + '\n' +
    msg.type.toString('utf8') + ' : '+
    new Date(msg.timestamp).toISOString() + '\n' +
    ( msg.type.toString('utf8') == 'INIT'
    ? hex(msg.message) + '\n'
    : msg.message.toString('utf8') + '\n' )
  )
}

tape('3 feeds', function (t) {

  var alice = db.feed(ecc.generate(k256))
  var bob   = db.feed(ecc.generate(k256))

  writeMessages(alice, [
    'hello there',
    'again again',
    'third time lucky'
  ], next)
  writeMessages(bob, [
    'foo bar baz',
    'apple banana cherry durian elderberry',
    'something else'
  ], next)

  var n = 2

  function next () {
    if(--n) return

    //request the feeds
    pull(
      db.createFeedStream(),
      pull.collect(function (err, ary) {
        if(err) throw err
        ary.map(function (e) {
          console.log(toHuman(e))
        })
        var sorted = ary.slice().sort(function (a, b) {
          return a.timestamp - b.timestamp
        })
        //2 INIT + 6 messages
        t.equal(ary.length, 8)
        t.deepEqual(ary, sorted)
        t.end()
      })
    )

  }

})





@@ -254,17 +254,69 @@
 -')%0A
-%0A
 var 
-db
+u
  =
+ require('../util')%0A%0Afunction create(name) %7B%0A%0A  return
  Scu
@@ -351,34 +351,12 @@
 vel(
-'scuttlebutt-secure-feeds'
+name
 , %7B%0A
@@ -437,16 +437,62 @@
       )%0A
+%7D%0A%0Avar db = create('scuttlebutt-secure-feeds')
 %0A%0Afuncti
@@ -1208,16 +1208,44 @@
  (t) %7B%0A%0A
+  var cbs = u.groups(next)%0A%0A
   var al
@@ -1412,20 +1412,21 @@
 y'%0A  %5D, 
-next
+cbs()
 )%0A  writ
@@ -1536,26 +1536,14 @@
  %5D, 
-next)%0A%0A  var n = 2
+cbs())
 %0A%0A  
@@ -1557,16 +1557,19 @@
 n next (
+err
 ) %7B%0A    
@@ -1575,20 +1575,22 @@
  if(
---n) return%0A
+err) throw err
 %0A   
@@ -2038,10 +2038,788 @@
 %7D%0A%0A%7D)%0A%0A%0A
-%0A
+tape('2 feeds - write', function (t) %7B%0A  var cbs = u.groups(done)%0A  var sbs1 = create('scuttlebutt-secure-feeds1')%0A  var sbs2 = create('scuttlebutt-secure-feeds2')%0A  var alice = sbs1.feed(ecc.generate(k256))%0A  var bob   = sbs1.feed(ecc.generate(k256))%0A%0A  writeMessages(alice, %5B%0A    'hello there',%0A    'again again',%0A    'third time lucky'%0A  %5D, cbs())%0A  writeMessages(bob, %5B%0A    'foo bar baz',%0A    'apple banana cherry durian elderberry',%0A    'something else'%0A  %5D, cbs())%0A%0A  function done (err) %7B%0A    if(err) throw err%0A%0A    pull(%0A      db.createReadStream(),%0A      pull.through(function (msg) %7B%0A        console.log(toHuman(msg))%0A      %7D),%0A      sbs2.createWriteStream(function (err, ary) %7B%0A        if(err) throw err%0A        console.log(ary)%0A        t.end()%0A      %7D)%0A    )%0A%0A  %7D%0A%0A%7D)
 %0A

test the simple parts of replication