'use strict';
/**
 * Import plugins
 */
var gulp = require('gulp'),
    $ = require('gulp-load-plugins')(),
    runSequence = require('run-sequence'),
    argv = require('yargs').argv,
    del = require('del');
/**
 * Build vendors dependencies
 */
gulp.task('vendors', function() {

    /**
     * CSS VENDORS
     */
    gulp.src([
        'bower_components/font-awesome/css/font-awesome.min.css',
        'bower_components/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css',
        'bower_components/bootstrap-chosen/bootstrap-chosen.less'
    ])
        .pipe($.concat('vendors.css'))
        .pipe($.minifyCss())
        .pipe(gulp.dest('web/assets/css'));

    /**
     * JS VENDORS
     * (with jQuery and Bootstrap dependencies first)
     */

    gulp.src([
        'bower_components/jquery/dist/jquery.js',
        'bower_components/bootstrap-datepicker/dist/js/bootstrap-datepicker.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/affix.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/alert.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/button.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/carousel.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/collapse.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/dropdown.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/modal.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/tooltip.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/popover.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/scrollspy.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/tab.js',
        'bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/transition.js'
    ])
        .pipe($.concat('vendors.min.js'))
        .pipe($.uglify())
        .pipe(gulp.dest('web/assets/js'));


    /**
     * FONTS SOURCES
     * Important to add the bootstrap fonts to avoid issues with the fonts include path
     */
    gulp.src([
        'bower_components/bootstrap-sass-official/assets/fonts/bootstrap/*',
        'bower_components/font-awesome/fonts/*'
    ])
        .pipe(gulp.dest('web/assets/fonts'));

    /**
     * POLYFILLS SOURCES
     * Various polyfills required for old IE
     */
    gulp.src([
        'bower_components/html5shiv/dist/html5shiv.js',
        'bower_components/respond/dest/respond.src.js'
    ])
        .pipe($.concat('polyfills.min.js'))
        .pipe($.uglify())
        .pipe(gulp.dest('web/assets/js'));

    /**
     * POLYFILLS SOURCES
     * Various polyfills required for old IE
     */
    gulp.src([
        'bower_components/bootstrap-chosen/chosen-sprite.png',
        'bower_components/bootstrap-chosen/chosen-sprite@2x.png'
    ])
        .pipe(gulp.dest('web/assets/css'));
});
/**
 * Build styles from SCSS files
 * With error reporting on compiling (so that there's no crash)
 */
gulp.task('styles', function() {
    if (!argv.production) {
        console.log('[styles] Processing styles for dev env. No minifying here, for sourcemaps!')
    } else {
        console.log('[styles] Processing styles for production env.');
    }
    return $.rubySass('sass/main.scss', {sourcemap: !argv.production})
        .on('error', $.notify.onError(function (error) {
            console.log('Error', error.message);
            if (!argv.production) 

        }))
        .pipe($.autoprefixer({
            browsers: ['last 2 versions', 'safari 5', 'ie 8', 'ie 9', 'ff 27', 'opera 12.1']
        }))
        .pipe($.if(!argv.production, $.sourcemaps.write()))
        .pipe($.if(argv.production, $.minifyCss()))
        .pipe(gulp.dest('web/assets/css'));
});
/**
 * Build JS
 * With error reporting on compiling (so that there's no crash)
 * And jshint check to highlight errors as we go.
 */
gulp.task('scripts', function() {
    return gulp.src('js/**/*.js')
        .pipe($.jshint())
        .pipe($.jshint.reporter('jshint-stylish'))
        .pipe($.concat('main.js'))
        .pipe(gulp.dest('web/assets/js'))
        .pipe($.rename({ suffix: '.min' }))
        .pipe($.uglify())
        .pipe(gulp.dest('web/assets/js'));
});
/**
 * Clean output directories
 */
gulp.task('clean', del.bind(null, ['web/assets']));
/**
 * Serve
 */
gulp.task('serve', ['styles', 'scripts'], function () {
    gulp.watch(['sass/**/*.scss'], function() {
        runSequence('styles');
    });
    gulp.watch(['js/**/*.js'], function() {
        runSequence('scripts');
    });

});
/**
 * Task to build assets on production server
 */
gulp.task('build', ['clean'], function() {
    argv.production = true;
    runSequence('vendors', 'styles', 'scripts');
});
/**
 * Default task
 */
gulp.task('default', ['clean'], function(cb) {
    runSequence('vendors', 'styles', 'scripts', cb);
});

{
                return 'Message to the notifier: ' + error.message;
            }