import express from 'express';
import { match } from 'react-router';
import { applyMiddleware, createStore } from 'redux';

import statics from './statics';
import routes from '../src/routes';
import ENV from '../src/shared/env';
import rootReducer from '../src/reducers/';
import applyEnvMiddleWare from './middleware';
import {symbols, color} from './shared/console';
import renderMainPage from './templates/main-page';
import requestMiddleware from '../src/middleware/request';
import fetchRequiredActions from '../src/shared/fetch-data';
import renderMainContainer from './containers/main-container';

const port = 8000;
const app = express();
const context = 'server';
const staticPaths = setStaticsPaths(statics);
const envMiddleware = applyEnvMiddleWare(ENV, app);
const serverStore = applyMiddleware( requestMiddleware )( createStore );

app.use(function (req, res) {

  const store = serverStore(rootReducer);

  match({ routes , location: req.url }, (error, redirectLocation, renderProps) => {

    if ( error ) return res.status(500).send( error.message );
    if ( redirectLocation ) return res.redirect( 302, redirectLocation.pathname + redirectLocation.search );
    if ( renderProps == null ) return res.status(404).send( 'Not found' );

    fetchRequiredActions(store.dispatch, renderProps.components, renderProps.params, context)
      .then(() => {
        let mainView = renderMainContainer(store, renderProps);
        return renderMainPage( ENV, mainView, store );
      })
      .then( page => res.status(200).send(page) )
      .catch( err => res.end(err.message) );
  });
});

function setStaticsPaths(staticPaths){
  staticPaths.map(function(staticPath){
    app.use(staticPath.route, express.static(staticPath.dir));
    console.log('[BASE] ' + color('success', symbols.ok) + ' Applied static path ' + staticPath.route);
  });
}

app.listen(port, function (err) {
  if (err) {
    console.log(err);
    return;
  }
  console.log('[BASE] ' + color('success', symbols.ok) + ' Server up on http://localhost:' +  port);

});

@@ -157,22 +157,19 @@
 %0Aimport 
-routes
+ENV
  from '.
@@ -178,35 +178,54 @@
 src/
-routes';%0Aimport ENV
+shared/env';%0Aimport %7B symbols, color %7D
  from '.
 ./sr
@@ -216,37 +216,32 @@
 or %7D from '.
-.
-/src
 /shared/
 env';%0Aimport
@@ -220,35 +220,39 @@
  from './shared/
-env
+console
 ';%0Aimport rootRe
@@ -249,123 +249,351 @@
 ort 
-rootReducer from '../src/reducers/';%0Aimport applyEnvMiddleWare from './middleware';%0Aimport %7Bsymbols, color%7D
+applyEnvMiddleWare from './middleware';%0A%0Aconst port = 8000;%0Aconst app = express();%0Aconst context = 'server';%0Aconst staticPaths = setStaticsPaths(statics);%0Aconst envMiddleware = applyEnvMiddleWare(ENV, app);%0Aconst serverStore = applyMiddleware( requestMiddleware )( createStore );%0A%0Aimport routes from '../src/routes';%0Aimport rootReducer
  from '.
 /sha
@@ -584,39 +584,39 @@
 ucer from '.
+.
 /s
-hared/console
+rc/reducers/
 ';%0Aimport re
@@ -846,248 +846,8 @@
 ';%0A%0A
-const port = 8000;%0Aconst app = express();%0Aconst context = 'server';%0Aconst staticPaths = setStaticsPaths(statics);%0Aconst envMiddleware = applyEnvMiddleWare(ENV, app);%0Aconst serverStore = applyMiddleware( requestMiddleware )( createStore );%0A%0A
 app.

Change import order to handle correctly webpack build