/**
 * Webpack helpers & dependencies
 */
const webpackMerge = require('webpack-merge'),
  webpackConfig = require('angular-webpack-config');

const copyWebpackPlugin = require('copy-webpack-plugin'),
  definePlugin = require('webpack/lib/DefinePlugin'),
  htmlElementsWebpackPlugin = require('html-elements-webpack-plugin')
  uglifyJsPlugin = require('webpack/lib/optimize/UglifyJsPlugin');

const path = require('path');

const browserConfig = function(root, settings) {

  return {
    /**
     * Add additional plugins to the compiler.
     *
     * See: http://webpack.github.io/docs/configuration.html#plugins
     */
    plugins: [
      /**
       * Plugin: CopyWebpackPlugin
       * Description: Copy files and directories in webpack.
       *
       * Copies project static assets.
       *
       * See: https://www.npmjs.com/package/copy-webpack-plugin
       */
      new copyWebpackPlugin([
        {
          context: `src/client/assets/images`,
          from: '**/*',
          to: './images'
        },
        {
          from: `${root(settings.paths.src.client.assets.root)}/config.json`,
          to: './config.json'
        }
      ]),

      new definePlugin({
        'process.env': {
          'BASE_URL': "process.env.BASE_URL",
          'PORT': "process.env.PORT",
          'REDIS_URL': "process.env.REDIS_URL"
        },
        'PORT': "process.env.PORT",
      }),
      /**
       * Plugin: HtmlElementsWebpackPlugin
       * Description: Generate html tags based on javascript maps.
       *
       * If a publicPath is set in the webpack output configuration, it will be automatically added to
       * href attributes, you can disable that by adding a "=href": false property.
       * You can also enable it to other attribute by settings "=attName": true.
       *
       * The configuration supplied is map between a location (key) and an element definition object (value)
       * The location (key) is then exported to the template under then htmlElements property in webpack configuration.
       *
       * Example:
       *  Adding this plugin configuration
       *  new HtmlElementsWebpackPlugin({
           *    headTags: { ... }
           *  })
       *
       *  Means we can use it in the template like this:
       *  <%= webpackConfig.htmlElements.headTags %>
       *
       * Dependencies: HtmlWebpackPlugin
       */
      new htmlElementsWebpackPlugin(require(root(`${settings.paths.config}/html-elements.config`)))
    ]
  };
};
const inbrowserConfig = {
  plugins: [
    new definePlugin({
      'PLATFORM' : '"client"'
    })
  ]
};
const serverConfig =  {
  plugins: [
    new definePlugin({
      'PLATFORM' : '"server"',
      'process.env': {
        'BASE_URL': "process.env.BASE_URL",
        'PORT': "process.env.PORT",
        'REDIS_URL': "process.env.REDIS_URL"
      },
      'PORT': "process.env.PORT",
    })
  ]
};

const browserProdConfig = {
  plugins: [
    new uglifyJsPlugin({
        // beautify: false,
        // mangle: {
        //   screw_ie8: true
        // },
        output: {
          comments: false
        },
        compress: {
          screw_ie8: true,
          warnings: false,
          conditionals: true,
          unused: true,
          comparisons: false,
          sequences: true,
          dead_code: true,
          evaluate: true,
          if_return: true,
          join_vars: true,
          negate_iife: false // we need this for lazy v8
        }
      })
  ]
}

const serverProdConfig = {
  plugins: [
    new uglifyJsPlugin({
        // beautify: false,
        mangle: false, // to ensure process.env still works
        output: {
          comments: false
        },
        compress: {
          screw_ie8: true,
          warnings: false,
          conditionals: true,
          unused: true,
          comparisons: false,
          sequences: true,
          dead_code: true,
          evaluate: true,
          if_return: true,
          join_vars: true,
          negate_iife: false // we need this for lazy v8
        }
      })
  ]
}
/**
 * Webpack configuration
 *
 * See: http://webpack.github.io/docs/configuration.html#cli
 */


module.exports = function(options, root, settings) {
  switch (options.env) {
    case 'prod':
    case 'production':
      return !!options.platform
        ? options.platform === 'server'
          ? webpackMerge({customizeArray: webpackMerge.unique('plugins',['UglifyJsPlugin'], plugin => plugin.constructor && plugin.constructor.name)
})(serverProdConfig, webpackMerge(serverConfig, webpackConfig.universal.server.prod(root, settings)))
          : webpackMerge({customizeArray: webpackMerge.unique('plugins',['UglifyJsPlugin'], plugin => plugin.constructor && plugin.constructor.name)
})(browserProdConfig, webpackMerge(browserConfig(root, settings), inbrowserConfig, webpackConfig.universal.browser.prod(options, root, settings)))
        : webpackMerge({customizeArray: webpackMerge.unique('plugins',['UglifyJsPlugin'], plugin => plugin.constructor && plugin.constructor.name)
})(browserProdConfig, webpackMerge(browserConfig(root, settings), inbrowserConfig, webpackConfig.spa.prod(options, root, settings)))
    case 'test':
    case 'testing':
      return webpackConfig.test(root, settings);
    case 'dev':
    case 'development':
      return !!options.platform
        ? options.platform === 'server'
          ? webpackMerge(webpackConfig.universal.server.dev(root, settings), serverConfig)
          : webpackMerge(webpackConfig.universal.browser.dev(options, root, settings), browserConfig(root, settings), inbrowserConfig)
        : options.hmr
          ? webpackMerge(webpackConfig.spa.hmr(options, root, settings), browserConfig(root, settings), inbrowserConfig)
          : webpackMerge(webpackConfig.spa.dev(options, root, settings), browserConfig(root, settings), inbrowserConfig);
  }
};
