/**
 * Sass compilation and Autoprefixer
 */
module.exports = function(gulp, projectPath, themePath, includesPath, config) {
    var TASK_NAME = 'jslint',
        DEFAULT_CONFIG = {
            'src': ['**/*.js', '!**/.tmp/**/*.js', '!**/assets/**/*.js', '!**/vendor/**/*.js','!**/*.min.js'],
            'jsHintOptions': projectPath + themePath + '.jshintrc',
            'esLintOptions': {
                'configFile': projectPath + themePath + '.eslintrc',
                'fix': true
            }
        };

    return {
        name: TASK_NAME,
        task: function getJsLintTask(done) {
            var _subTasks = [],
                _config,
                fs = require('fs'),
                _extend = require('merge'),
                jshint = require('gulp-jshint'),
                eslint = require('gulp-eslint'),
                stylish = require('jshint-stylish'),
                merge = require('merge-stream');

            _config = _extend.recursive(true, DEFAULT_CONFIG, config);

            try {
                fs.accessSync(_config.jsHintOptions);
            } catch (err) {
                _config.jsHintOptions = __dirname + '/../.jshintrc';
            }

            try {
                fs.accessSync(_config.esLintOptions.configFile);
            } catch (err) {
                _config.esLintOptions.configFile = __dirname + '/../.eslintrc';
            }


            _subTasks.push(
                gulp.src(
                    _config.src.map(function (item) {
                        if (item.indexOf('!') === -1) {
                            return themePath + item;
                        }
                        return item;
                    }),
                    {
                        cwd: projectPath
                    })
                    .pipe(jshint(_config.jsHintOptions))
                    .pipe(jshint.reporter(stylish))
                    .pipe(eslint(_config.esLintOptions))
                    .pipe(eslint.format())
                    .pipe(gulp.dest(
                        function (file) {
                            return file.base;
                        }
                    ))
            );


            return merge(_subTasks);
        },
        setProjectPath: function (path) {
            projectPath = path;
        },
        setThemePath: function (path) {
            themePath = path;
        }
    }
};

