var gulp = require('gulp');
var gutil = require('gulp-util');
var path = require("path");
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var livereload = require('gulp-livereload');
var webpack = require("webpack");

var getWebpackConfig = function() {
  return {
    context: __dirname + "/src/scripts",
    entry: {
      //vendor: ['gsap', 'd3', 'jquery', 'mustache.js', 'lodash', 'draggable-number.js'],
      Core: "./Core",
      Editor: "./Editor",
    },
    devtool: "source-map",
    externals: [
      'd3',
      'spectrum',
      {
        'js-signals': {
          root: 'signals',
          commonjs: './signals',
          amd: 'signals'
        },
        'draggable-number.js': {
          root: 'DraggableNumber',
          commonjs: 'DraggableNumber',
          commonjs2: 'DraggableNumber',
          amd: 'DraggableNumber'
        },
        'gsap': {
          root: 'TweenMax',
          commonjs: 'TweenMax',
          commonjs2: 'TweenMax',
          amd: 'TweenMax'
        },
        'jquery': {
          root: '$',
          commonjs: 'jquery',
          commonjs2: 'jquery',
          amd: 'jquery'
        },
        'lodash': {
          root: '_',
          commonjs: "lodash",
          commonjs2: "lodash",
          amd: "lodash"
        },
        'mustache.js': {
          root: 'Mustache',
          commonjs: "mustache",
          commonjs2: "mustache",
          amd: "mustache"
        }
      }
    ],
    output: {
        path: path.join(__dirname, "dist/scripts"),
        filename: "TweenTime.[name].js",
        libraryTarget: "umd",
        library: ["TweenTime", "[name]"]
    },
    module: {
      loaders: [
        { test: /\.js$/, exclude: [/bower_components/, /node_modules/, /dist/], loader: '6to5-loader?runtime=true'},
        { test: /\.tpl.html$/, loader: 'mustache'},
      ],
    },
    resolve: {
      root: [path.join(__dirname, "src/scripts/bower_components")],
      // Add alias when the library doesn't automagically load with bower resolver.
      alias: {
        //spectrum: 'bower_components/spectrum/spectrum.js'
        //'js-signals': 'bower_components/js-signals/dist/signals.js'
      }
    },
    plugins: [
      new webpack.ResolverPlugin(
          new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])
      )
    ],
  };
};

gulp.task('scripts', function(cb) {
  var conf = getWebpackConfig();
  webpack(conf, function(err, stats) {
    if(err) throw new gutil.PluginError("webpack", err);
    gutil.log("[webpack]", stats.toString({
        chunks: false
    }));
    cb();
  });
});

gulp.task('scripts:dist', function(cb) {
  var conf = getWebpackConfig();
  // Add .min.js to output filename.
  conf.output.filename = "TweenTime.[name].min.js";
  // Add minification with UglifyJs.
  conf.plugins.push(new webpack.optimize.UglifyJsPlugin({
    mangle: true,
    output: {
      comments: false
    },
    compress: {
      drop_debugger: false,
      sequences: true,
      //dead_code: true,
      conditionals: true,
      booleans: true,
      unused: true,
      if_return: true,
      join_vars: true,
      drop_console: false
    }
  }));
  webpack(conf, function(err, stats) {
    if(err) throw new gutil.PluginError("webpack", err);
    gutil.log("[webpack]", stats.toString({
        chunks: false
    }));
    cb();
  });
});

gulp.task('styles', function() {
  gulp.src('src/styles/editor.sass')
    .pipe(sourcemaps.init())
    .pipe(sass({
      style: 'expanded',
      errLogToConsole: true
    }))
    .pipe(sourcemaps.write('./maps'))
    .pipe(gulp.dest('./dist/styles/'));
});

gulp.task('livereload', function() {
  livereload.listen();
});

gulp.task('watch', function() {
  gulp.watch(['examples/*.html'], livereload.changed);
  gulp.watch(['dist/styles/*.css', 'dist/scripts/*.js'], livereload.changed);
  gulp.watch('src/styles/**', ['styles']);
  gulp.watch(['src/scripts/**', '!src/scripts/bower_components/**'], ['scripts:dist']);
});

gulp.task('default', ['watch', 'livereload', 'styles', 'scripts:dist']);
gulp.task('build', ['styles', 'scripts', 'scripts:dist']);

@@ -346,98 +346,8 @@
 : %7B%0A
-      //vendor: %5B'gsap', 'd3', 'jquery', 'mustache.js', 'lodash', 'draggable-number.js'%5D,%0A
     
@@ -360,24 +360,24 @@
 : %22./Core%22,%0A
+
       Editor
@@ -1205,163 +1205,8 @@
 sh%22%0A
-        %7D,%0A        'mustache.js': %7B%0A          root: 'Mustache',%0A          commonjs: %22mustache%22,%0A          commonjs2: %22mustache%22,%0A          amd: %22mustache%22%0A
     
@@ -1607,24 +1607,24 @@
 mustache'%7D,%0A
+
       %5D,%0A   
@@ -1631,335 +1631,8 @@
  %7D,%0A
-    resolve: %7B%0A      root: %5Bpath.join(__dirname, %22src/scripts/bower_components%22)%5D,%0A      // Add alias when the library doesn't automagically load with bower resolver.%0A      alias: %7B%0A        //spectrum: 'bower_components/spectrum/spectrum.js'%0A        //'js-signals': 'bower_components/js-signals/dist/signals.js'%0A      %7D%0A    %7D,%0A
     

remove some unused webpack config and commented code