const pkg = require('./package.json');
const gulp = require('gulp');
const fancyLog = require('fancy-log');
const del = require('del');
const plugins = require('gulp-load-plugins')({
    lazy: true,
    postRequireTransforms: {
        print: (plugin) => plugin.default,
        uglifyEs: (plugin) => plugin.default
    }
});

const onError = (error) => {
    fancyLog(error);
};

const sassOptions = {style: 'compressed'};

gulp.task('vendorJs', () => {

    fancyLog('Compiling vendor JS dependencies');

    return gulp.src(pkg.globs.vendorJs)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(plugins.concat('vendor.js'))
        .pipe(plugins.uglify())
        .pipe(gulp.dest(pkg.paths.build.js));
});

gulp.task('vendorCss', () => {
    fancyLog('Compiling vendor CSS dependencies');
    return gulp.src(pkg.globs.vendorCss)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(plugins.concatCss('vendor.css'))
        .pipe(plugins.cleanCss())
        .pipe(plugins.replace('../../../font-awesome/', './'))
        .pipe(plugins.replace('../../jquery-ui-dist/', ''))
        .pipe(gulp.dest(pkg.paths.build.css));
});

gulp.task('vendorFonts', () => {
    fancyLog('Copying vendor fonts for dependencies');
    return gulp.src(pkg.globs.vendorFlags)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(gulp.dest(pkg.paths.build.flags));
});

gulp.task('vendorFlags', () => {
    fancyLog('Copying flags svg for dependencies');
    return gulp.src(pkg.globs.vendorFonts)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(gulp.dest(pkg.paths.build.fonts));
});

gulp.task('vendorImages', () => {
    fancyLog('Copying vendor images for dependencies');
    return gulp.src(pkg.globs.vendorImages)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(gulp.dest(pkg.paths.build.images));
});

gulp.task('sourceImages', () => {
    fancyLog('Compiling source image files');
    return gulp.src(pkg.globs.sourceImages)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(gulp.dest(pkg.paths.build.images));
});

gulp.task('sourceCss', gulp.series('sourceImages', () => {
    fancyLog('Compiling source CSS files');
    return gulp.src(pkg.globs.sourceCss)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(plugins.sass(sassOptions).on('error', plugins.sass.logError))
        .pipe(plugins.autoprefixer())
        .pipe(plugins.concatCss('site.css'))
        .pipe(gulp.dest(pkg.paths.build.css));
}));

gulp.task('sourceJs', () => {
    fancyLog('Compiling source JS dependencies');
    return gulp.src(pkg.globs.sourceJs)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(plugins.concat('site.js'))
        .pipe(plugins.uglifyEs())
        .pipe(gulp.dest(pkg.paths.build.js));
});

gulp.task('sourceFonts', () => {
    fancyLog('Compiling source font files');
    return gulp.src(pkg.globs.sourceFonts)
        .pipe(plugins.plumber({errorHandler: onError}))
        .pipe(plugins.print())
        .pipe(gulp.dest(pkg.paths.build.fonts));
});

gulp.task('clean', () => {
    return del([pkg.paths.build.css + '/*', pkg.paths.build.js + '/*', !pkg.paths.build.js + '/app.js', pkg.paths.build.images + '/*', pkg.paths.build.fonts + '/*']);
});

gulp.task('vendor', gulp.series('vendorJs', 'vendorCss', 'vendorFonts', 'vendorFlags', 'vendorImages'));
gulp.task('source', gulp.series('sourceCss', 'sourceImages', 'sourceJs', 'sourceFonts'));
gulp.task('build', gulp.series('clean', 'vendor', 'source'));

gulp.task('watch', (done) => {
    gulp.watch(pkg.globs.sourceCss, gulp.series('sourceCss'));
    gulp.watch(pkg.globs.sourceImages, gulp.series('sourceImages'));
    gulp.watch(pkg.globs.sourceJs, gulp.series('sourceJs'));
    gulp.watch(pkg.globs.sourceJs, gulp.series('sourceFonts'));
    done();
});