import Express from 'express';
import path from 'path';
import BodyParser from 'body-parser';

import webpack from 'webpack'
import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import webpackConfig from '../../webpack.config'

import React from 'react';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';

import template from '../../public/template';
import { createIdFromTimeStamp } from './utils';

var MongoClient = require('mongodb').MongoClient;

const POST_DATA_SIZE_LIMIE = '50mb';
const databaseName = 'turingMachine';
const databaseCollection = 'saves';
const url = "mongodb://localhost:27017/" + databaseName;

var app = new Express();
app.use(BodyParser({limit: POST_DATA_SIZE_LIMIE}));

const compiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(compiler, { noInfo: true, publicPath: webpackConfig.output.publicPath }));
app.use(webpackHotMiddleware(compiler));

app.use(BodyParser.urlencoded({
  extended: true,
  limit: POST_DATA_SIZE_LIMIE
}));
app.use(BodyParser.json({limit: POST_DATA_SIZE_LIMIE})); 

app.use(Express.static(path.join(__dirname + '/../../public')));
app.use('/error', Express.static(path.join(__dirname + '/../../public')));

app.get('/', function(req, res) {
	// res.sendFile(path.join(__dirname + "/../../public/index.html"));
	res.send(template({}))
});


app.get('/error/404', function(req, res) {
	// res.sendFile(path.join(__dirname + "/../../public/index.html"));
	res.send(template({}))
});

app.get('/:id', function(req, res) {
	MongoClient.connect(url, function(err, db) {
		if (err || db === null) {
			res.redirect('/error/404');
			return;
		}

		db.collection(databaseCollection).findOne({
				id: req.params.id.toString()
			},
			function(err, target) {
				if (target && target.state) {
					res.send(template(target.state));
				} else {
					res.redirect('/error/404');
				}
			}
		);


		db.close();
	});
});

app.post('/', function(req, res) {
	MongoClient.connect(url, function(err, db) {
		if (err || db === null) {
			res.status(403).send({
				error: "No response"
			});
			return;
		}

		var id = createIdFromTimeStamp();
		db.collection(databaseCollection).insert({
			id: id,
			state: req.body
		}, function(err, docsInserted) {
			res.send({
				id: id
			});
		});

		db.close();
	});
});

app.all('*', function(req, res) {
  res.redirect('/error/404');
});

var server = app.listen(3000, function() {
	// var host = server.address().address;
	var port = server.address().port;

	console.log("Example app listening at http://localhost:%s", port);
})
@@ -747,60 +747,8 @@
 s();
-%0Aapp.use(BodyParser(%7Blimit: POST_DATA_SIZE_LIMIE%7D));
 %0A%0Aco
@@ -1011,16 +1011,40 @@
 ZE_LIMIE
+,%0A  parameterLimit:50000
 %0A%7D));%0Aap
@@ -2435,11 +2435,9 @@
 ten(
-300
+8
 0, f

Fix payload too large