const gulp = require('gulp');
const sass = require('gulp-sass')(require('sass'));
const autoprefixer = require('gulp-autoprefixer');
const cleanCSS = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const plumber = require('gulp-plumber');
const notify = require('gulp-notify');

// Пути к файлам
const paths = {
    sass: {
        src: 'resources/sass/**/*.scss',
        dest: 'public/css/'
    },
    js: {
        src: 'resources/js/**/*.js',
        dest: 'public/js/'
    }
};

// Обработка ошибок
const onError = function(err) {
    notify.onError({
        title: "Gulp error in " + err.plugin,
        message: err.toString()
    })(err);
    this.emit('end');
};

// Компиляция SASS
function compileSass() {
    return gulp.src(paths.sass.src)
        .pipe(plumber({ errorHandler: onError }))
        .pipe(sourcemaps.init())
        .pipe(sass({
            outputStyle: 'expanded',
            includePaths: ['resources/sass']
        }).on('error', sass.logError))
        .pipe(autoprefixer({
            cascade: false
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paths.sass.dest))
        .pipe(notify({
            message: 'SASS compiled successfully!',
            onLast: true
        }));
}

// Минификация CSS
function minifyCss() {
    return gulp.src('public/css/styles.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(cleanCSS({
            level: 2
        }))
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest(paths.sass.dest))
        .pipe(notify({
            message: 'CSS minified successfully!',
            onLast: true
        }));
}

// Обработка JS
function compileJs() {
    return gulp.src(paths.js.src)
        .pipe(plumber({ errorHandler: onError }))
        .pipe(sourcemaps.init())
        .pipe(concat('script.js'))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(paths.js.dest))
        .pipe(notify({
            message: 'JS compiled successfully!',
            onLast: true
        }));
}

// Минификация JS
function minifyJs() {
    return gulp.src('public/js/script.js')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(uglify())
        .pipe(rename({
            suffix: '.min'
        }))
        .pipe(gulp.dest(paths.js.dest))
        .pipe(notify({
            message: 'JS minified successfully!',
            onLast: true
        }));
}

// Наблюдение за изменениями
function watchFiles() {
    gulp.watch(paths.sass.src, gulp.series(compileSass));
    gulp.watch(paths.js.src, gulp.series(compileJs));
}

// Экспорт задач
exports.sass = compileSass;
exports.js = compileJs;
exports.minifyCss = minifyCss;
exports.minifyJs = minifyJs;
exports.watch = gulp.series(
    gulp.parallel(compileSass, compileJs),
    watchFiles
);
exports.build = gulp.series(
    gulp.parallel(compileSass, compileJs),
    gulp.parallel(minifyCss, minifyJs)
);
exports.default = exports.watch;
