/**
 * @Author: Bachir Soussi Chiadmi <bach>
 * @Date:   20-12-2017
 * @Email:  bachir@figureslibres.io
 * @Filename: gulpfile.js
 * @Last modified by:   bach
 * @Last modified time: 20-12-2017
 * @License: GPL-V3
 */

'use strict';

var gulp = require('gulp');
var util = require('gulp-util');
var sass = require('gulp-sass');
var watch = require('gulp-watch');
var autoprefixer = require('gulp-autoprefixer');
var jsmin = require('gulp-jsmin');
var cssmin = require('gulp-cssmin');
var rename = require('gulp-rename');
var mainBowerFiles = require('main-bower-files');

function handleError(err) {
  console.log(err.toString());
}

var config = {
  production: !!util.env.production
}

gulp.task('scripts', function () {
    gulp.src('./assets/scripts/corpus.js')
      .pipe(config.production ? jsmin() : util.noop())
      .pipe(rename({suffix: '.min'}))
      .pipe(gulp.dest('./assets/dist/scripts/'));
});

gulp.task('styles', function () {
  gulp.src('./assets/styles/corpus.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(autoprefixer({
        browsers: ['last 2 versions'],
        cascade: false
    })).on('error', handleError)
    .pipe(config.production ? cssmin() : util.noop())
    .pipe(rename({suffix: '.min'}))
    .pipe(gulp.dest('./assets/dist/styles/'));
});

gulp.task('bower', function() {
    gulp.src(mainBowerFiles({
      "overrides":{
        "jquery":{
          "ignore":true
        },
        "physics": {
          "main": [
            './build/physics.js'
          ]
        },
        "EaselJS":{
            "ignore":true
          // "main": [
          //   './lib/easeljs.js'
          // ]
        }
      }
    }))
      .pipe(config.production ? jsmin() : util.noop())
      .pipe(rename({suffix: '.min'}))
      .pipe(gulp.dest('./assets/dist/bower/'));
});

// default gulp task
gulp.task('default', ['bower', 'scripts', 'styles'], function() {
  gulp.watch('./assets/styles/*.scss', ['styles']);
  // gulp.watch('./assets/styles/*/*.scss', ['styles']);
  gulp.watch('./assets/scripts/*.js', ['scripts']);
});
