import {Provider} from 'mobx-react';
import http from 'http';
import https from 'https';
import debug from 'debug';
import express from 'express';
import fs from "fs";
import path from "path";

const webpack = require('webpack');
const webpackConfig = require('./../../webpack.config');
const compression = require('compression');
const cors = require('cors');
const rt = require("thumb-express");
const compiler = webpack(webpackConfig);
const app = express();
const HTML = fs.readFileSync(path.join(__dirname, '..', '..', 'public', 'index.html'), 'utf-8');

const renderView = (renderProps, appstate) => {
  const initialState = {
    appstate: appstate.toJson()
  };
  return HTML//.replace('<!--rootComponent-->', componentHTML);
};

const shouldCompress = (req, res) => {
  if (req.headers['x-no-compression']) {
    // don't compress responses with this request header
    return false
  }

  // fallback to standard filter function
  return compression.filter(req, res)
}


app.use(compression({filter: shouldCompress}));
app.use(cors());
app.use(rt.init(__dirname + '/../../assets'));
app.use(express.static(__dirname + '/../../assets'));
app.use('/assets', express.static(__dirname + '/../../assets'));
app.use(require("webpack-dev-middleware")(compiler, {
  publicPath: webpackConfig.output.publicPath,
  noInfo: true
}));
app.use(require("webpack-hot-middleware")(compiler, {
  log: console.log,
  path: '/__webpack_hmr',
  heartbeat: 10 * 1000,
  reload: true
}));
app.use((req, res) => {
  res.send(HTML);
});

const port = process.env.PORT || 7700;
app.set('port', port);
const server = app.listen(app.get('port'), (err, result) => {
  console.log('--------------------------');
  console.log('Server started');
  console.log('Listening at http://localhost:' + app.get('port') + '/');
  console.log('--------------------------')
});
