import React from 'react';
import { renderToString } from 'react-dom/server';
import { createMemoryHistory } from 'react-router';
import { createStore, applyMiddleware, compose } from 'redux';
import { syncHistoryWithStore } from 'react-router-redux';
import thunk from 'redux-thunk';

import express from 'express';
import { mixin } from '~/utils/mixin';
import packageJson from '~/../package.json';
import webpackConfig from '~/config/webpack';
import Class from '~/classes/Class';

import Reducers from '~/reducers/';
import App from '~/App';

/**
 * [mixin description]
 * @extends Mixin
 */
export default class Router extends mixin(Class, express.Router){
  /**
   * [constructor description]
   * @author THernandez03
   * @constructor
   * @param  {[type]} options [description]
   * @return {[type]} [description]
   */
  constructor(options){
    options = {
      started: new Date()
    , devPath: `http://${webpackConfig.host}:${webpackConfig.port}`
    , ...options
    };
    super(options);
    const router = this.supers[0];
    const devPath = options.devPath;

    router.get('/version', this.getVersion.bind(this));
    router.get('/server/*', (req, res) => {
      const memoryHistory = createMemoryHistory(req.path);
      const store = createStore(Reducers, compose(applyMiddleware(thunk)));
      const history = syncHistoryWithStore(memoryHistory, store);
      res.render('index', {
        path: devPath
      , App: renderToString(<App store={store} history={history}/>)
      , initialState: `
          <script>
            window.__InitialState = ${JSON.stringify(store.getState())}
          </script>
        `
      });
    });
    router.get('/*', (req, res) => {
      res.render('index', { path: devPath });
    });

    return router;
  }

  /**
   * [getVersion description]
   * @author THernandez03
   * @param  {Function} req [description]
   * @param  {Function} res [description]
   * @return {[type]} [description]
   */
  getVersion(req, res) {
    res.status(200).json({
      started: this.get('started').toISOString()
    , uptime: (new Date()).getTime() - this.get('started').getTime()
    , version: packageJson.version
    , author: {
        name: packageJson.author.name
      , email: packageJson.author.email
      , url: packageJson.author.url
      }
    });
  }
}

@@ -310,16 +310,53 @@
 press';%0A
+import Class from '~/classes/Class';%0A
 import %7B
@@ -480,45 +480,8 @@
 ck';
-%0Aimport Class from '~/classes/Class';
 %0A%0Aim
@@ -540,16 +540,118 @@
 /App';%0A%0A
+const devPath = (webpackConfig.host) ?%0A  %60http://$%7BwebpackConfig.host%7D:$%7BwebpackConfig.port%7D%60 : ''%0A;%0A%0A
 /**%0A * %5B
@@ -1007,62 +1007,8 @@
 Path
-: %60http://$%7BwebpackConfig.host%7D:$%7BwebpackConfig.port%7D%60
 %0A   
@@ -1086,45 +1086,8 @@
 %5B0%5D;
-%0A    const devPath = options.devPath;
 %0A%0A  

Set empty 'devPath' when not using 'local' env