'use strict';

var gulp = require('gulp'),
    eslint = require('gulp-eslint'),
    gutil        = require('gulp-util');

var m = require('../common.js');

var errBeep = function(beeps) {
    for (var i=0 ; i<beeps ; i++) {
        gutil.beep();
    }
};

var lintErrMessage = function() {
    errBeep(10);
    errBeep(5);
    gutil.log(gutil.colors.red('\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ESLINT ERROR !!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n'+
        '      See errors above for details to fix\n      Please fix errors now!!\n\n'));
    errBeep(20);
    errBeep(5);
};


var lintError;
var doLinting = function(paths, success, error) {

    gulp.src(paths)
        .pipe(eslint())
        .pipe(eslint.format())
        .on('data', function(file) {
            if(file.eslint && file.eslint.messages && file.eslint.messages.length){
                lintError = true;
            }
        })
        .on('finish', function() {
            if (lintError) {
                // reset error flag
                lintError = false;
                error();
            } else {
                success();
            }
        });
};

module.exports = {

    doLinting: doLinting,

    lintErrMessage: lintErrMessage
};