const util   = require('util');
const fs     = require('fs');
const bcrypt = require('bcryptjs');

// Set up an Express app for the Web API.
const express    = require('express');
const whitelist  = require('whitelist-ips');
const bodyParser = require('body-parser');
const app        = express();
const router     = express.Router();
app.use(bodyParser.urlencoded({ extended: true }));

const Config = require('./Config');

class WebInterface {
  
  constructor(state) {
    this.state  = state;
    this.port   = Config.get('webPort') || 9000;

    this.whiteListed = [
      'localhost'
    ];
  }

  init() {
    util.log('[WEB] Initializing...');
    this.setUpMiddleWare();
    this.setUpGetRoutes();
    app.listen(this.port);
    util.log(`[WEB]: Web API activated and running on port ${this.port}.`);
    util.log(`[WEB]: Web API Secret: ${this.secret}`);
  }

  setUpMiddleWare() {
    app.use('/api', router);
    app.use(whitelist(['127.0.0.1']));
    app.use((err, req, res, next) => {
      console.error(err.stack);
      res.status(500).send('Server Error');
    });
  }

  setUpGetRoutes() {
    const { MobFactory, PlayerManager, ItemManager, RoomManager, HelpManager } = this.state;

    // Routes for the API's GET response.
    router.get('/npcs',    this.getResponseData(MobFactory, 'npcs'));
    router.get('/players', this.getResponseData(PlayerManager, 'players'));     
    router.get('/items',   this.getResponseData(ItemManager, 'items'));
    router.get('/rooms',   this.getResponseData(RoomManager, 'rooms'));
    router.get('/help',    this.getResponseData(HelpManager, 'helps'));
  }

  getResponseData(manager, name) {
    return (req, res) => {
      const response = this.parseEntitiesIntoResponse(manager, name);
      return res.json({ success: false, message: `An error has occurred: ${err}.` });
    }
  }

  parseEntitiesIntoResponse(manager, name) {
    const entities = manager[name];
    let response   = [];
    
    if (entities instanceof Set) {
      response = [...entities]; 
    }

    if (entities instanceof Map) {
      handleMap(entities, response);
    }

    function handleMap(entities, response) {
      for (let [key, value] of entities) {
        if (!value) { continue; }
        const val = value.serialize ? value.serialize() : value;
        response.push(val);
      }
    }

    return response;
  }

}

module.exports = WebInterface;
@@ -562,16 +562,46 @@
 Listed =
+ Config.get('webWhitelist') %7C%7C
  %5B%0A     
@@ -1800,65 +1800,24 @@
 n(%7B 
-success: false, message: %60An error has occurred: $%7Berr%7D.%60
+%5Bname%5D: response
  %7D);

Fix getResponseData method which was trying to return a nonexistent error. Remove more unused code and dependencies. Have a default in case they do not config a whitelist of web api users.