import keystone from 'keystone';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import path from 'path';
import express from 'express';
import config from '../webpack.config.js';
const isDeveloping = process.env.NODE_ENV !== 'production';
const importRoutes = keystone.importer(__dirname);
const compiler = webpack(config);

import React from 'react';
import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { Provider } from 'react-redux';
import store from '../app/src/store/store.js';
import _ from 'lodash';
import fs from 'fs';
import { routes } from '../app/src/utils/routes.jsx';

const baseTemplate = fs.readFileSync(path.join(__dirname, '../public/index.html'));
const template = _.template(baseTemplate);

const serverApiRoutes = {
  api: importRoutes('./api')
};

if (isDeveloping) {
  keystone.pre('routes', webpackMiddleware(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath
  }));

  keystone.pre('routes', webpackHotMiddleware(compiler, {
    log: console.log // eslint-disable-line
  }));
}

exports = module.exports = function (app) {
  app.use(webpackMiddleware(compiler, {
    noInfo: true,
    publicPath: config.output.publicPath
  }));

  app.use(webpackHotMiddleware(compiler, {
    log: console.log, // eslint-disable-line
    path: '/__webpack_hmr',
    heartbeat: 10 * 1000
  }));

  if (isDeveloping) {
    app.all('*', (req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET, POST');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
    });
  }

  app.all('/api*', keystone.middleware.api);
  app.get('/api/posts/list', serverApiRoutes.api.posts.getAll);
  app.all('/api/posts/create', serverApiRoutes.api.posts.create);
  app.get('/api/posts/:id', serverApiRoutes.api.posts.getOne);
  app.all('/api/posts/:id/update', serverApiRoutes.api.posts.update);
  app.get('/api/posts/:id/remove', serverApiRoutes.api.posts.remove);

  app.all('/api/contact/create', serverApiRoutes.api.contact.create);

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

  app.use((req, res) => {
    match({ routes, location: req.url },
      (error, redirectLocation, renderProps) => {
        if (error) {
          res.status(500).send(error.message);
        } else if (redirectLocation) {
          res.redirect(302, redirectLocation.pathname + redirectLocation.search);
        } else if (renderProps) {
          const body = renderToString(
            React.createElement(Provider, { store },
              React.createElement(RouterContext, renderProps)
            )
          );
          res.status(200).send(template({ body }));
        } else {
          res.status(400).send('Not Found 🤔');
        }
      });
  });
};

@@ -2234,24 +2234,27 @@
 ublic'));%0A%0A 
+ //
  app.use((re
@@ -2268,18 +2268,21 @@
  =%3E %7B%0A  
+//
   
+ 
 match(%7B 
@@ -2307,24 +2307,27 @@
 req.url %7D,%0A 
+ //
      (error,
@@ -2360,24 +2360,27 @@
 rops) =%3E %7B%0A 
+ //
        if (e
@@ -2384,24 +2384,27 @@
  (error) %7B%0A 
+ //
          res
@@ -2434,24 +2434,27 @@
 .message);%0A 
+ //
        %7D els
@@ -2477,32 +2477,34 @@
 cation) %7B%0A  
+//
         
 res.redirect
@@ -2487,24 +2487,25 @@
   //        
+ 
 res.redirect
@@ -2561,24 +2561,27 @@
 n.search);%0A 
+ //
        %7D els
@@ -2598,24 +2598,27 @@
 erProps) %7B%0A 
+ //
          con
@@ -2644,16 +2644,19 @@
 tring(%0A 
+ //
         
@@ -2700,16 +2700,19 @@
 ore %7D,%0A 
+ //
         
@@ -2765,16 +2765,19 @@
 Props)%0A 
+ //
         
@@ -2782,16 +2782,19 @@
      )%0A 
+ //
         
@@ -2794,24 +2794,27 @@
         );%0A 
+ //
          res
@@ -2854,22 +2854,25 @@
  %7D));%0A  
+//
       
+ 
 %7D else %7B
@@ -2869,24 +2869,27 @@
   %7D else %7B%0A 
+ //
          res
@@ -2919,24 +2919,27 @@
 Found %F0%9F%A4%94');%0A 
+ //
        %7D%0A   
@@ -2936,16 +2936,19 @@
      %7D%0A 
+ //
      %7D);
@@ -2948,16 +2948,118 @@
     %7D);%0A
+  // %7D);%0A  app.get('/*', (req, res) =%3E %7B%0A    res.sendFile(path.join(__dirname, 'public/index.html'));%0A
   %7D);%0A%7D;

remove match until we can get it working :(