//Import Depedencies
const express = require('express');
const router = express.Router();

const controller = require('./controller');
const config = require('../../config');

const { validatorGconvert } = require('../enrichment-map/gene-validator');
const { enrichment } = require('../enrichment-map/enrichment');

const isAuthenticated = token => {
  return config.MASTER_PASSWORD != '' && config.MASTER_PASSWORD === token;
};
const errorMsg = {
  error: 'Invalid access token'
};

// Expose a rest endpoint for controller.submitLayout
router.post('/submit-layout', function (req, res) {
  if (isAuthenticated(req.body.token)) {
    controller.submitLayout(req.body.uri, req.body.version, req.body.layout, req.body.user)
      .then((package) => {
        res.json(package);
      });
  } else {
    res.json(errorMsg);
  }
});

// Expose a rest endpoint for controller.submitGraph
router.post('/submit-graph', function (req, res) {
  if (isAuthenticated(req.body.token)) {
    controller.submitGraph(req.body.uri, req.body.version, req.body.graph)
    .then((package) => {
      res.json(package);
    });
  } else {
    res.json(errorMsg);
  }
});

// Expose a rest endpoint for controller.submitDiff
router.post('/submit-diff', function (req, res) {
  if (isAuthenticated(req.body.token)) {
    controller.submitDiff(req.body.uri, req.body.version, req.body.diff, req.body.user)
      .then((package) => {
        res.json(package);
      });
  } else {
    res.json(errorMsg);
  }
});

// Expose a rest endpoint for controller.getGraphAndLayout
router.get('/get-graph-and-layout', function (req, res) {
  controller.getGraphAndLayout(req.query.uri, req.query.version).then((package) => {
    res.json(package);
  });
});

router.get('/gene-query', (req, res) => {
  const genes = req.query.genes;

  validatorGconvert(genes).then(gconvertResult => {
    res.json(gconvertResult);
  });
});

//expose a rest endpoint for enrichment
router.get('/enrichment/', (req, res) => {
  const genes = req.query.genes;
  let user_settings = {};
  if (req.query.setting !== undefined) {
    user_settings = JSON.parse(req.query.setting);
  }
  enrichment(genes, user_settings).then(enrichmentResult => {
    res.json(enrichmentResult);
  });
});

// Expose a rest endpoint for controller.endSession
router.get('/disconnect', function (req, res) {
  controller.endSession(req.query.uri, req.query.version, req.query.user)
    .then((package) => {
      res.json(package);
    });
});

module.exports = router;
@@ -1953,17 +1953,16 @@
 richment
-/
 ', (req,
@@ -2011,32 +2011,30 @@
 ;%0A  let user
-_setting
+Option
 s = %7B%7D;%0A  if
@@ -2078,24 +2078,22 @@
     user
-_setting
+Option
 s = JSON
@@ -2109,23 +2109,23 @@
 q.query.
-setting
+options
 );%0A  %7D%0A 
@@ -2151,16 +2151,14 @@
 user
-_setting
+Option
 s).t

remove trailing slash, change setting to options