const http = require('http');
const request = require('request');
const bonjour = require('bonjour')();
const config = require('../config');

function HttpServer() {
  if (!(this instanceof HttpServer)) return new HttpServer();

  const port = 8080;

  const self = this;

  self.deviceStatus = {};

  self.httpRequestHandler = function httpRequestHandler(req, res) {
    if (req.url === '/') {
      res.setHeader('Content-Type', 'application/json');
      if (config.resin) {
        const resinSupervisorUrl = `${
          process.env.RESIN_SUPERVISOR_ADDRESS
        }/v1/device?apikey=${process.env.RESIN_SUPERVISOR_API_KEY}`;
        request.get(resinSupervisorUrl, (err, response, body) => {
          res.end(
            JSON.stringify(
              Object.assign(self.deviceStatus, {
                resinSupervisorStatus: JSON.parse(body),
              })
            )
          );
        });
      } else {
        res.end(JSON.stringify(self.deviceStatus));
      }
    } else {
      res.statusCode = 404;
      res.end();
    }
  };

  http.createServer(self.httpRequestHandler).listen(port);

  bonjour.publish({ name: 'BBQ-Pi', type: 'http', port });
}

HttpServer.prototype.setDeviceStatus = function setDeviceStatus(deviceStatus) {
  this.deviceStatus = deviceStatus;
};

module.exports = HttpServer;

@@ -63,46 +63,8 @@
 ');%0A
-const bonjour = require('bonjour')();%0A
 cons
@@ -1071,68 +1071,8 @@
 rt);
-%0A%0A  bonjour.publish(%7B name: 'BBQ-Pi', type: 'http', port %7D);
 %0A%7D%0A%0A

remove buggy bonjour implementation