var spawn = require('child_process').spawn;
var path = require("path");
var fs = require("fs");
var _ = require('lodash');
var webpack = require('webpack');
var WebpackDev = require('webpack-dev-server');
var config = require('./webpack.config.dev.js');
var httpProxy = require('http-proxy');


var loadSettings = function () {
  var fileName = './settings.json.default.json';
  if (fs.existsSync('./settings.json')) {
    fileName = './settings.json';
  }

  return require(fileName).couchserver || {
    port: process.env.FAUXTON_PORT || 8000,
    contentSecurityPolicy: true,
    proxy: {
      target: 'http://127.0.0.1:5984',
      changeOrigin: false
    }
  };
};

var settings = loadSettings();

var less = function () {
  var _less = spawn('npm', ['run', 'build:less:debug']);

  _less.stdout.on('data', (data) => {
    console.log(data.toString());
  });

  _less.stderr.on('error', (data) => {
    console.log('less error:', data.toString());
  });

  _less.on('close', (code) => {
    console.log(`less compile finished with code ${code}`);
  });
};

var devSetup = function (cb) {
  console.log('setup dev environment');
  var cmd = 'devSetupWithClean';
  if (settings.noClean) {
    cmd = 'devSetup';
  }

  var grunt = spawn('grunt', [cmd]);

  grunt.stdout.on('data', (data) => {
    console.log(data.toString());
  });

  grunt.stderr.on('error', (data) => {
    console.log('Setup error:', data.toString());
  });

  grunt.on('close', (code) => {
    console.log('dev setup finished with code', code);
    if (code === 0) {
      cb();
    }
  });
};

var defaultHeaderValue = "default-src 'self'; child-src 'self' data: blob:; img-src 'self' data:; font-src 'self'; " +
                  "script-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline';";
var setCSP = function (res) {
  if (!settings.contentSecurityPolicy) {
    return;
  }

  var headerValue = settings.contentSecurityPolicyHeader || defaultHeaderValue;

  console.log('seeting', headerValue);
  res.set('Content-Security-Policy', headerValue);
};

var runWebpackServer = function () {
  var fileTypes = ['.js', '.css', '.png', '.swf', '.eot', '.woff', '.svg', '.ttf', '.swf'];
  function isFile (url) {
    return _.contains(fileTypes, path.extname(url));
  }


  var options = {
    contentBase: __dirname + '/dist/debug',
    publicPath: '/',
    outputPath: '/',
    filename: 'bundle.js',
    host: 'localhost',
    port: process.env.FAUXTON_PORT || 8000,
    hot: false,
    historyApiFallback: true,
    stats: {
      colors: true,
    }
  };

  var compiler = webpack(config);

  compiler.plugin('done', function () {
    console.log('Bundled done!');
    console.log('Compiling less');
    less();
  });

  var server = new WebpackDev(compiler, options);
  var proxy = httpProxy.createServer({
    secure: false,
    changeOrigin: true,
    target: settings.proxy.target
  });

  proxy.on('proxyRes', function (proxyRes, req, res) {
    if (proxyRes.headers['set-cookie']) {
      proxyRes.headers['set-cookie'][0] = proxyRes.headers["set-cookie"][0].replace('Secure', '');
    }
  });

  server.app.all('*', function (req, res, next) {
    setCSP(res);
    proxy.web(req, res);
  });

  server.listen(options.port, options.host, function (err) {
    if (err) {
      console.log(err);
      return;
    }
    console.log('listening on', options.host, options.port);
    console.log('Starting first compile. This will take about 10 seconds...');
  });
};


devSetup(runWebpackServer);

@@ -1943,47 +1943,8 @@
 e;%0A%0A
-  console.log('seeting', headerValue);%0A
   re

remove console.log