var Hapi = require('hapi')
var test = require('tap').test
var PouchDB = require('pouchdb-core')
  .plugin(require('pouchdb-mapreduce'))
  .plugin(require('pouchdb-adapter-memory'))

var hoodie = require('../../').register
var hapiPluginOptions = {
  register: hoodie,
  options: {
    inMemory: true,
    loglevel: 'error',
    paths: {},
    PouchDB: PouchDB
  }
}

require('npmlog').level = 'error'

test('respond to all /hoodie/* - requests with an index.html', function (group) {
  var server

  group.beforeEach(function (done) {
    server = new Hapi.Server()
    server.connection({port: 8090})
    server.register(hapiPluginOptions, function () {
      done()
    })
  })

  group.afterEach(function (done) {
    server.stop(done)
  })

  group.test('respond to /admin/', function (t) {
    checkRequestForPath(t, '/hoodie/admin/')
  })

  group.test('repond to /account/', function (t) {
    checkRequestForPath(t, '/hoodie/account/')
  })

  group.test('respond to /store/', function (t) {
    checkRequestForPath(t, '/hoodie/store/')
  })

  group.test('respond to simple /hoodie/', function (t) {
    server.inject({
      url: 'http://localhost:8090/hoodie/unkown.css',
      headers: {
        accept: 'text/css'
      }
    }, function (response) {
      t.is(response.statusCode, 404, 'statusCode is 404')

      t.end()
    })
  })

  group.test('respond to simple /hoodie/', function (t) {
    server.inject({
      url: 'http://localhost:8090/hoodie/unkown',
      headers: {
        accept: 'text/html'
      }
    }, function (response) {
      t.is(response.statusCode, 200, 'statusCode is 200')

      server.stop(t.end)
    })
  })

  function checkRequestForPath (t, path) {
    server.inject({
      url: 'http://localhost:8090' + path + 'unknownSubPath',
      headers: {
        accept: 'text/html'
      }
    }, function (response) {
      t.is(response.statusCode, 200, 'statusCode is 200')
      t.match(response.payload, /<html/, 'response is HTML')

      t.end()
    })
  }

  group.end()
})

@@ -1073,322 +1073,16 @@
  to 
-simple /hoodie/', function (t) %7B%0A    server.inject(%7B%0A      url: 'http://localhost:8090/hoodie/unkown.css',%0A      headers: %7B%0A        accept: 'text/css'%0A      %7D%0A    %7D, function (response) %7B%0A      t.is(response.statusCode, 404, 'statusCode is 404')%0A%0A      t.end()%0A    %7D)%0A  %7D)%0A%0A  group.test('respond to simple 
 /hoodie/
 ', f
@@ -1077,16 +1077,22 @@
 /hoodie/
+unkown
 ', funct
@@ -1277,35 +1277,35 @@
 nse.statusCode, 
-200
+404
 , 'statusCode is
@@ -1305,19 +1305,19 @@
 Code is 
-200
+404
 ')%0A%0A    
@@ -1405,32 +1405,32 @@
 server.inject(%7B%0A
+
       url: 'http
@@ -1458,27 +1458,8 @@
 path
- + 'unknownSubPath'
 ,%0A  

/hoodie/unknown responds with 404