var gulp = require("gulp");
var sass = require("gulp-sass");
var autoprefixer = require("gulp-autoprefixer");
var plumber = require("gulp-plumber");
var pleeease = require('gulp-pleeease');
var uglify = require("gulp-uglify");
var browserSync = require("browser-sync");

gulp.task("server", function() {
    browserSync({
        server: {
            baseDir: "./"
        }
    });
    gulp.watch("assets/**", function() {
        gulp.src("assets/**/*.scss")
        .pipe(plumber())
        .pipe(sass({ style: 'expanded' }).on('error', sass.logError))
        .pipe(pleeease({
            autoprefixer: {
                browsers: [
                'Explorer >= 9',
                'last 1 Chrome versions',
                'last 1 Firefox versions',
                'last 1 Safari versions',
                'last 2 iOS versions',
                'Android 4'
                ]
            },
            rem: false,
            minifier: false,
            sourcemaps: false
        }))
        .pipe(gulp.dest("public/webroot"))
        .pipe(browserSync.reload({stream:true}))
    });
    gulp.watch("public/*.html", function() {
        browserSync.reload();
    });
});
gulp.task("js", function() {
    gulp.src(["js/**/*.js","!js/min/**/*.js"])
    .pipe(uglify())
    .pipe(gulp.dest("./js/min"))
    .pipe(browserSync.reload({stream:true}))
});

gulp.task("sass", function() {
    gulp.src("assets/**/*.scss")
    .pipe(plumber())
    .pipe(sass({ style: 'expanded' }).on('error', sass.logError))
    .pipe(pleeease({
        autoprefixer: {
            browsers: [
            'Explorer >= 9',
            'last 1 Chrome versions',
            'last 1 Firefox versions',
            'last 1 Safari versions',
            'last 2 iOS versions',
            'Android 4'
            ]
        },
        rem: false,
        minifier: false,
        sourcemaps: false
    }))
    .pipe(gulp.dest("public/webroot"))
    .pipe(browserSync.reload({stream:true}))
});

gulp.task("default",['server'], function() {
    gulp.watch(["js/**/*.js","!js/min/**/*.js"],["js"]);
    gulp.watch("sass/**/*.scss",["sass"]);
});
