import express from 'express';
import { urlencoded } from 'body-parser';

const debug = require('debug')('test-server');

const app = express();

const config = {
  dataCenter: "Local",
  tier: "Local"
};

const dataCenters = [
  "Any",
  "Local"
];

const settingsData = {
 "settings": [{
  "name": "Test.BoolValue",
  "description": "A boolean value. Can be true or false",
  "isBool": true,
  "allDefaults": [{
   "dataCenter": "Any",
   "tier": "Any",
   "value": "False"
  }],
  "allOverrides": [],
  "activeOverride": null,
  "defaultValue": {
   "dataCenter": "Any",
   "tier": "Any",
   "value": "False"
  }
 }, {
  "name": "Test.TextValue",
  "description": "A text value. Free-form text input",
  "isBool": false,
  "allDefaults": [{
   "dataCenter": "Any",
   "tier": "Any",
   "value": "Hello, world"
  }],
  "allOverrides": [],
  "activeOverride": null,
  "defaultValue": {
   "dataCenter": "Any",
   "tier": "Any",
   "value": "Hello, world"
  },
 }],
 "availableDataCenters": ["Any", "Local"]
};

const setActiveOverride = setting => {
  // 'Any' is the fallback
  let o = setting.allOverrides.find(o => o.dataCenter === config.dataCenter);
  debug('Override for %s: ', config.dataCenter, o);
  if (!o) {
    o = setting.allOverrides.find(o => o.dataCenter === 'Any');
    debug('Override for Any: ', o);
  }

  setting.activeOverride = o;
};

app.use(urlencoded({ extended: false }));


app.get('/settings.json', (req, res) => {
  res.json(settingsData);
});


app.post('/set', (req, res) => {
  const { settingName, dataCenter, value } = req.body;

  debug('Body:', req.body);
  debug('Config:', config);

  const setting = settingsData.settings.find(s => s.name === settingName);

  if (!setting)
  {
    res.status(404).end();
    return;
  }

  const existing = setting.allOverrides.find(o => o.dataCenter === dataCenter);
  if (existing) {
    existing.value = value;
  } else {
    const override = {
      dataCenter,
      tier: config.tier,
      value
    };
    setting.allOverrides.push(override);
  }

  setActiveOverride(setting);

  res.json(setting);
});

app.post('/clear', (req, res) => {
  const { settingName, dataCenter } = req.body;

  const setting = settingsData.settings.find(s => s.name === settingName);

  if (!setting)
  {
    res.status(404).end();
    return;
  }

  setting.allOverrides = setting.allOverrides.filter(o => o.dataCenter !== dataCenter);
  setActiveOverride(setting);

  res.json(setting);
});

app.use(express.static('public'));
app.use(express.static('dist'));


const server = app.listen(3000, () => {
  const host = server.address().address;
  const port = server.address().port;

  console.log(`Example app listening at http://${host}:${port}`);
});

@@ -66,16 +66,52 @@
 arser';%0A
+import %7B readFileSync %7D from 'fs';%0A%0A
 %0Aconst d
@@ -171,24 +171,86 @@
 express();%0A%0A
+app.set('views', './views');%0Aapp.set('view engine', 'jade');%0A%0A
 const config
@@ -367,745 +367,70 @@
 a = 
-%7B%0A %22settings%22: %5B%7B%0A  %22name%22: %22Test.BoolValue%22,%0A  %22description%22: %22A boolean value. Can be true or false%22,%0A  %22isBool%22: true,%0A  %22allDefaults%22: %5B%7B%0A   %22dataCenter%22: %22Any%22,%0A   %22tier%22: %22Any%22,%0A   %22value%22: %22False%22%0A  %7D%5D,%0A  %22allOverrides%22: %5B%5D,%0A  %22activeOverride%22: null,%0A  %22defaultValue%22: %7B%0A   %22dataCenter%22: %22Any%22,%0A   %22tier%22: %22Any%22,%0A   %22value%22: %22False%22%0A  %7D%0A %7D, %7B%0A  %22name%22: %22Test.TextValue%22,%0A  %22description%22: %22A text value. Free-form text input%22,%0A  %22isBool%22: false,%0A  %22allDefaults%22: %5B%7B%0A   %22dataCenter%22: %22Any%22,%0A   %22tier%22: %22Any%22,%0A   %22value%22: %22Hello, world%22%0A  %7D%5D,%0A  %22allOverrides%22: %5B%5D,%0A  %22activeOverride%22: null,%0A  %22defaultValue%22: %7B%0A   %22dataCenter%22: %22Any%22,%0A   %22tier%22: %22Any%22,%0A   %22value%22: %22Hello, world%22%0A  %7D,%0A %7D%5D,%0A %22availableDataCenters%22: %5B%22Any%22, %22Local%22%5D%0A%7D
+JSON.parse(readFileSync(%60$%7B__dirname%7D/settings.json%60, 'utf8'))
 ;%0A%0Ac
@@ -814,24 +814,87 @@
 false %7D));%0A%0A
+app.get('/', (req, res) =%3E res.render('index', settingsData));%0A
 %0Aapp.get('/s

use jade for view engine and read in settings from external json file