var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var BrowserSyncPlugin = require('browser-sync-webpack-plugin');
// var CopyWebpackPlugin = require('copy-webpack-plugin');
var { CleanWebpackPlugin } = require('clean-webpack-plugin');

if (process.env.DEV) {
  console.log('Loading .env variables');
  require('dotenv').config({path: __dirname + '/.env'});
}

const JOJOAPPS_SERVER = '149.91.81.94';
const JOJOAPPS_SERVER_PORT = 2610;

// Env variables have effect in app : main.js & socket-service.js & index.html
// Also in Server : mariadb-connector.js
var definePlugin = new webpack.DefinePlugin({
  'process.env.DEV': JSON.stringify(JSON.parse(process.env.DEV || 'false')),
  'process.env.WEB': JSON.stringify(JSON.parse(process.env.WEB || 'false')),
  'process.env.CORDOVA': JSON.stringify(JSON.parse(process.env.CORDOVA || 'false')),
  'process.env.SERVER_HOST': JSON.stringify(process.env.SERVER_HOST || JOJOAPPS_SERVER),
  'process.env.SERVER_PORT': JSON.stringify(process.env.SERVER_PORT || JOJOAPPS_SERVER_PORT)
});

var outpath = './webpack/';

if (process.env.CORDOVA === "true") {
  outpath = './www/';
  console.log('CORDOVA environment : output in ',outpath);
} else if (process.env.WEB === "true") {
  outpath = './www_web/';
  console.log('WEB environment : output in ',outpath);
} else {
  console.log('DEV env : output in ', outpath);
}

// new CopyWebpackPlugin([
//   {
//     context: path.resolve(__dirname, 'src', 'assets'),
//     from: '**/*',
//     to: path.resolve(__dirname, outpath, 'assets')
//   }
// ]),
module.exports = {
  devtool: 'cheap-source-map',
  mode: 'development',
  entry: {
    app: path.resolve(__dirname, 'src/main.js')
  },
  output: {
    filename: 'app.bundle.js',
    path: path.resolve(__dirname, outpath)
  },

  plugins: [
    definePlugin,
    new CleanWebpackPlugin(),

    new HtmlWebpackPlugin({
      template: './src/index.html'
    }),
    new BrowserSyncPlugin({
      host: process.env.IP || 'localhost',
      port: process.env.PORT || 3000,
      server: {
        baseDir: [outpath, './build']
      },
      browser: ["firefox"]
    }),
    new BrowserSyncPlugin({
      host: process.env.IP || 'localhost',
      port: process.env.PORT || 3005,
      server: {
        baseDir: [outpath, './build']
      },
      browser: ["firefox"]
    }),
    new webpack.DefinePlugin({
      CANVAS_RENDERER: JSON.stringify(true),
      WEBGL_RENDERER: JSON.stringify(true)
    })
  ],
  module: {
    rules: [
      { test: /\.js$/, exclude: /(node_modules|server)/, loaders: "babel-loader" },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: {
          loader: 'file-loader',
          options: {
            esModule: false
          }
        }
      },
      {
        test: /\.html$/,
        exclude: /index.html/,
        use: [
          {
            loader: 'html-loader',
            options: { minimize: true }
          }
        ]
      },
      {
        test: [/\.vert$/, /\.frag$/],
        use: "raw-loader"
      }
    ]
  }
};
