/**
 * Sass compilation and Autoprefixer
 */
module.exports = function(gulp, projectPath, themePath, includesPath, config) {
    var TASK_NAME = 'sass',
        DEFAULT_CONFIG = {
            'src': ['**/*.scss', '!**/.tmp/**/*.scss', '!**/assets/**/*.scss'],
            'sourceMaps': true,
            'cssNano': false,
            'dest': '/',
            'sassOptions': {
                errLogToConsole: true,
                outputStyle: 'expanded',
                includePaths: [projectPath + includesPath]
            },
            'autoprefixerOptions': {}
        };

    return {
        name: TASK_NAME,
        task: function getSassTask(done) {
            var _subTasks = [],
                _config,
                _extend = require('merge'),
                plumber = require('gulp-plumber'),
                notify = require('gulp-notify'),
                watchSass = require("gulp-watch-sass"),
                sass = require('gulp-sass'),
                postcssPlugins = [],
                sourcemaps = require('gulp-sourcemaps'),
                gulpif = require('gulp-if'),
                postcss = require('gulp-postcss'),
                autoprefixer = require('autoprefixer'),
                cssnano = require('cssnano'),
                merge = require('merge-stream');

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

            if (_config.cssNano === false) {
                postcssPlugins = [autoprefixer(_config.autoprefixerOptions)]
            } else {
                postcssPlugins = [autoprefixer(_config.autoprefixerOptions), cssnano()]
            }

                return watchSass(_config.src.map(function (item) {
                    if (item.indexOf('!') === -1) {
                        return projectPath + themePath + item;
                    }
                    return item;
                }), { includePaths: _config.sassOptions.includePaths })
                    .pipe(plumber({ errorHandler: function(err) {
                            var bs = require('browser-sync').get('Dev Server');
                            if (bs) {
                                bs.notify('<span style="font-size:16px; display: inline-block; background: red; color: white; max-width: 400px;">👎 &nbsp; Error In Compiling Sass</span>', 3000);
                            }

                            notify({
                                title: "Gulp error in " + err.plugin,
                                message:  err.toString()
                            }).write(err);
                        }}))
                    .pipe(gulpif(_config.sourceMaps === true, sourcemaps.init()))
                    .pipe(sass(_config.sassOptions))
                    .pipe(postcss(postcssPlugins))
                    .pipe(gulpif(_config.sourceMaps === true, sourcemaps.write()))
                    .pipe(gulp.dest(
                        function (file) {
                            return file.base;
                        }
                    ));
        },
        setProjectPath: function (path) {
            projectPath = path;
        },
        setThemePath: function (path) {
            themePath = path;
        }
    }
};

