import * as express from 'express';
import * as bodyParser from 'body-parser';

import configs from './configs';
import db from './db';

const app = express();
const router = express.Router();
const Accounts = db.get('accounts');


process.env.TZ = configs.timezone;

app.use(express.static(__dirname + '/static'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use('/api', router);

// ------------------------------------
// REST API
// ------------------------------------

// create new account
router.post('/api/account', (req, res) => {
  const newAccount = req.query;
  if(!newAccount.id || !newAccount.password) {
    res.statusMessage = 'Invalid credential';
    res.status(401).send();
    return;
  }

  Accounts.findOne({id:newAccount.id}).then(existence => {
    if (existence) {
      res.statusMessage = 'Conflict: duplicate id';
      res.status(409).send();
      return;
    }

    Accounts.insert({
      ...newAccount,
      created_at: new Date(),
    });

    res.send('Success');
  });
});


// show all account list
// POST is used instead of GET since React-router eats all GET request.
// It would rather split api domain and use GET request.
router.post('/account_list', (req, res) => {
  const query = req.query;

  Accounts.find(query).then((function (result) {
    res.json(result);
  }))
});

// show account by id
// POST is used instead of GET since React-router eats all GET request.
// It would rather split api domain and use GET request.
router.post('/account/:id', (req, res) => {
  const id = req.params.id;

  Accounts.findOne({id:id}).then((function (account) {
    if (!account) {
      res.statusMessage = 'invalid account id';
      res.status(404).send();
      return;
    }
    res.json(account);
  }))
});

// modify account detail
router.put('/account', (req, res) => {
  const modifiedAccount = {
    ...req.query,
    modified_at: new Date()
  };

  Accounts.updateById(modifiedAccount._id, modifiedAccount).then(updated => {
    if (!updated) {
      res.statusMessage = `Unable to update account ${modifiedAccount.id}`;
      res.status(405).send();
      return;
    }
    res.json(updated);
  });
});

// login
router.post('/login', (req, res) => {
  const credential = req.query;

  Accounts.findOne({id:credential.id}).then(account => {
    if(!account || account.password !== credential.password) {
      res.statusMessage = 'Invalid credential';
      res.status(401).send();
      return;
    }

    const {password, ...rest} = account;

    res.json(rest);
  });
});

app.listen(configs.serverPort, () => {
  console.log(`Server listening on port ${configs.serverPort}`);
});

@@ -598,20 +598,19 @@
 t = req.
-quer
+bod
 y;%0A  if(
@@ -1259,28 +1259,27 @@
 query = req.
-quer
+bod
 y;%0A%0A  Accoun
@@ -1886,20 +1886,19 @@
  ...req.
-quer
+bod
 y,%0A    m
@@ -2223,32 +2223,34 @@
 (req, res) =%3E %7B%0A
+  
   const credenti
@@ -2258,20 +2258,19 @@
 l = req.
-quer
+bod
 y;%0A%0A  Ac

fix POST request parse