'use strict'
var ref = require('ssb-ref')
var Stack = require('stack')
var BlobsHttp = require('multiblob-http')
var sort = require('ssb-sort')
var pull = require('pull-stream')
var WebBoot = require('web-bootloader/handler')

function msgHandler(path, handler) {
  return function (req, res, next) {
    console.log(req.method, req.url)
    if(req.method !== 'GET') return next()
    if(req.url.indexOf(path) === 0) {
      var id = req.url.substring(path.length)
      console.log("MSG?", id)
      if(!ref.isMsg(id))
        next(new Error('not a valid message id:'+id))
      else {
        req.id = id
        handler(req, res, next)
      }
    }
    else
      next()
  }
}

function send(res, obj) {
  res.writeHead(200, {'Content-Type': 'application/json'})
  res.end(JSON.stringify(obj, null, 2))
}

module.exports = function (sbot) {

  return Stack(
    WebBoot,
    function (req, res, next) {
      res.setHeader('Access-Control-Allow-Origin', '*')
      res.setHeader("Access-Control-Allow-Headers",
      "Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since");
      res.setHeader("Access-Control-Allow-Methods", "GET", "HEAD");
      next()
    },
    msgHandler('/msg/', function (req, res, next) {
      sbot.get(req.id, function (err, msg) {
        if(err) return next(err)
        send(res, {key: req.id, value: msg})
      })
    }),
    msgHandler('/thread/', function (req, res, next) {
      sbot.get(req.id, function (err, value) {
        if(err) return next(err)
        var msg = {key: req.id, value: value}

        pull(
          sbot.links({rel: 'root', dest: req.id, values: true, keys: true}),
          pull.collect(function (err, ary) {
            if(err) return next(err)
            ary.unshift(msg)
            send(res, sort(ary))
          })
        )
      })

    }),
    BlobsHttp(sbot.blobs, '/blobs')
  )
}







@@ -218,16 +218,41 @@
 andler')
+%0Avar URL = require('url')
 %0A%0Afuncti
@@ -863,17 +863,40 @@
 sbot) %7B%0A
+  var prefix = '/blobs'
 %0A
-
   return
@@ -1905,48 +1905,535 @@
     
-BlobsHttp(sbot.blobs, '/blobs'
+function (req, res, next) %7B%0A      if(!(req.method === %22GET%22 %7C%7C req.method == 'HEAD')) return next()%0A%0A      var u = URL.parse('http://makeurlparseright.com'+req.url)%0A      var hash = decodeURIComponent(u.pathname.substring((prefix+'/get/').length))%0A      //check if we don't already have this, tell blobs we want it, if necessary.%0A      sbot.blobs.has(hash, function (err, has) %7B%0A        if(has) next()%0A        else sbot.blobs.want(hash, function (err, has) %7B next() %7D)%0A      %7D)%0A    %7D,%0A    BlobsHttp(sbot.blobs, prefix
 )%0A  )%0A
-
 %7D%0A%0A%0A%0A%0A%0A%0A
@@ -2428,12 +2428,13 @@
   )%0A%7D%0A%0A%0A%0A%0A%0A%0A
+%0A

call blobs.want if necessary, so that blobs are always returned if possible