// Include gulp
var gulp    = require('gulp');

// Include Our Plugins
var jshint      = require('gulp-jshint');
var concat      = require('gulp-concat');
var uglify      = require('gulp-uglify');
var rename      = require('gulp-rename');
var less      = require('gulp-less');
var svgo        = require('gulp-svgo');
var cleanCSS    = require('gulp-clean-css');
var nib         = require('nib');

var php = function () {
    var through = require('through2');
    var exec = require('child_process').exec;
    var PluginError = require('plugin-error');

    return through.obj(function (file, enc, cb) {

        if (file.isStream())
            return cb(new PluginError('php', 'Streaming not supported'));

        if (file.isNull())
            return cb(null, file);

        exec('php "'+file.path+'"', function (err, stdout) {
            if (err)
                return cb(new PluginError('php', err));

            file.contents = new Buffer.alloc(stdout);

            return cb(null, file);
        });
    });

};

// Lint Task
gulp.task('lint', function() {
    return gulp.src('./assets/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

// Concatenate & Minify JS
gulp.task('scripts', function() {
    return gulp.src(['./assets/js/select2.js', './assets/js/lightbox.js', './assets/js/simplebar.js', './assets/js/dragscroll.js', './assets/js/sticky.jquery.js', './assets/js/main.js'])
        .pipe(concat('all.js'))
        .pipe(gulp.dest('./assets/js'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./assets/js'));
});

gulp.task('main-min', function() {
    return gulp.src(['./assets/js/main.js'])
        .pipe(gulp.dest('./assets/js'))
        .pipe(rename('main.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./assets/js'));
});

// Less
gulp.task('less', function () {
    return gulp.src('./assets/css/styles.less')
        .pipe(less({use: nib()}))
        .pipe(gulp.dest('./assets/css'))
        .pipe(rename('styles.css'))
        //.pipe(cleanCSS({level: 1}))
        .pipe(gulp.dest('./assets/css'));
});
gulp.task('less-min', function () {
    return gulp.src('./assets/css/styles.less')
        .pipe(less({use: nib()}))
        .pipe(gulp.dest('./assets/css'))
        .pipe(rename('styles.min.css'))
        .pipe(cleanCSS({level: 1}))
        .pipe(gulp.dest('./assets/css'));
});
gulp.task('all', function () {
    return gulp.src('./assets/css/all.less')
        .pipe(less({use: nib()}))
        .pipe(gulp.dest('./assets/css'))
        .pipe(rename('all.css'))
        //.pipe(cleanCSS({level: 1}))
        .pipe(gulp.dest('./assets/css'));
});
gulp.task('all-min', function () {
    return gulp.src('./assets/css/all.less')
        .pipe(less({use: nib()}))
        .pipe(gulp.dest('./assets/css'))
        .pipe(rename('all.min.css'))
        .pipe(cleanCSS({level: 1}))
        .pipe(gulp.dest('./assets/css'));
});
// SVG to JSON
gulp.task('svg-json', function () {
    gulp.src('assets/images/*.svg')
        .pipe(svgo())
        .pipe(gulp.dest('assets/images/min'));

    gulp.src('assets/images/index.php')
        .pipe(php())
        .pipe(rename('svg.json'))
        .pipe(gulp.dest('assets/images/svg'))
});

// Watch Files For Changes
gulp.task('watch', function() {
    gulp.watch('./assets/js/*.js',  gulp.series('scripts', 'main-min'));
    gulp.watch('./assets/less/*.less',  gulp.series('less', 'less-min', 'all', 'all-min'));
    //gulp.watch('assets/images/*.svg',  gulp.series('svg-json'));
});

// Default Task
gulp.task('default', gulp.series('lint', 'less', 'scripts'));
gulp.task('dev', gulp.series('lint', 'less', 'scripts', 'watch'));
gulp.task('css', gulp.series('less', 'less-min', 'all', 'all-min'));
gulp.task('js', gulp.series('scripts', 'main-min'));