'use strict'

const Hapi = require('hapi')
const _ = require('lodash')
const WebServerTrailpack = require('trailpack-webserver')
const lib = require('./lib')

/**
 * Hapi Trailpack
 *
 * @class Hapi
 * @see {@link http://trailsjs.io/doc/trailpack
 *
 * Bind application routes to Hapi.js (from trailpack-router)
 */
module.exports = class HapiTrailpack extends WebServerTrailpack {

  /**
   * Ensure that config/web is valid, and that no other competing web
   * server trailpacks are installed (e.g. express)
   */
  validate () {
    if (_.includes(_.keys(this.app.packs), 'express4', 'koa', 'koa2', 'restify')) {
      return Promise.reject(new Error('There is another web services trailpack installed that conflicts with trailpack-hapi!'))
    }

    return Promise.all([
      lib.Validator.validateWebConfig(this.app.config.web)
    ])
  }

  configure () {
    const webConfig = this.app.config.web

    webConfig.plugins || (webConfig.plugins = [ ])
    webConfig.options || (webConfig.options = { })

    webConfig.server = 'hapi'
    webConfig.views.relativeTo = this.app.config.main.paths.root

    _.defaultsDeep(webConfig.options, {
      host: webConfig.host,
      port: webConfig.port,
      routes: {
        files: {
          relativeTo: webConfig.views.relativeTo
        }
      }
    })
  }

  /**
   * Start Hapi Server
   */
  initialize () {
    this.webConfig = _.cloneDeep(this.app.config.web)

    this.server = new Hapi.Server()
    this.server.connection(this.webConfig.options)

    return lib.Server.registerPlugins(this.webConfig, this.server, this.app)
      .then(() => {
        lib.Server.registerRoutes(this.webConfig, this.server, this.app)
        lib.Server.registerViews(this.webConfig, this.server, this.app)

        return this.server.start()
      })
  }

  unload () {
    this.server.stop()
  }

  constructor (app, config) {
    super(app, {
      config: require('./config'),
      api: require('./api'),
      pkg: require('./package')
    })
  }
}


@@ -235,25 +235,26 @@
 oc/trailpack
+%7D
 %0A
-
  *%0A * Bind a
@@ -535,253 +535,14 @@
     
-if (_.includes(_.keys(this.app.packs), 'express4', 'koa', 'koa2', 'restify')) %7B%0A      return Promise.reject(new Error('There is another web services trailpack installed that conflicts with trailpack-hapi!'))%0A    %7D%0A%0A    return Promise.all(%5B%0A     
+return
  lib
@@ -595,15 +595,8 @@
 eb)%0A
-    %5D)%0A
   %7D%0A

comment (jsdoc) syntax, cleanup validator