const express = require('express');
const { promisify } = require('es6-promisify');
const bodyParser = require('body-parser');
const freeport = promisify(require('freeport'));

module.exports = function RocketChatMockServer({
	port = null,
	users = [],
}) {
	const findFreePort = port ? Promise.resolve(port) : freeport();

	let idCounter = 0;

	return findFreePort.then(freePort => new Promise((resolve) => {
		const mockRocketChat = express();
		mockRocketChat.use(bodyParser.json()); // for parsing application/json
		mockRocketChat.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
		mockRocketChat.port = freePort;
		mockRocketChat.url = `http://localhost:${mockRocketChat.port}`;
		mockRocketChat.post('/api/v1/users.register', (req, res) => {
			const newRcUser = {
				email: req.body.email,
				username: req.body.username,
				password: req.body.pass,
				_id: idCounter,
			};
			let validity = 'true';
			users.forEach((user) => {
				if (user.email === newRcUser.email) validity = 'email';
				// if (user.username === newRcUser.username) do something?
			});
			if (validity === 'email') return res.status(403).send({ error: 'Email already exists. [403]' });

			users.push(newRcUser);
			idCounter += 1;
			return res.send(
				{
					user: {
						_id: newRcUser._id,
						username: newRcUser.username,
					},
				},
			);
		});

		mockRocketChat.post('/api/v1/users.update', (req, res) => {
			const id = req.body.userId;
			users[id].password = req.body.data.password;

			return res.send({ user: users[id] });
		});

		mockRocketChat.get('/api/v1/users.list', (req, res) => {
			if (req.query.query) {
				const substrings = req.query.query.split('"');
				const email = substrings[3];
				const result = [];
				users.forEach((user) => {
					if (user.email === email) result.push(user);
				});
				return res.send({ users: result });
			}
			return res.send({ users });
		});

		mockRocketChat.listen(mockRocketChat.port, () => {
			resolve(mockRocketChat);
		});
	}));
};

@@ -744,16 +744,14 @@
 ers.
+c
 re
-gis
+a
 te
-r
 ', (
@@ -1117,16 +1117,22 @@
 email') 
+%7B%0A%09%09%09%09
 return r
@@ -1156,49 +1156,102 @@
 nd(%7B
- 
+%0A%09%09%09%09%09
 error: 
-'Email already exists. %5B403%5D' %7D);
+%60$%7BnewRcUser.email%7D is already in use :( %5Berror-field-unavailable%5D%60,%0A%09%09%09%09%7D);%0A%09%09%09%7D
 %0A%0A%09%09

update mock