let argv         = require('minimist')(process.argv.slice(2));
let autoprefixer = require('gulp-autoprefixer');
let cache        = require('gulp-cached');
let concat       = require('gulp-concat');
let gulp         = require('gulp');
let gulpif       = require('gulp-if');
let pug          = require('gulp-pug');
let livereload   = require('gulp-livereload');
let plumber      = require('gulp-plumber');
let rename       = require('gulp-rename');
let sass         = require('gulp-sass');
let sourcemaps   = require('gulp-sourcemaps');
let uglify       = require('gulp-uglify');

let enabled = {
	failCheck: !argv.production,
	maps: argv.production,
	prettyPug: !argv.production,
	uglify: argv.production
};

var destination = {
	css: 'cloud/static/assets/css',
	pug: 'cloud/templates',
	js: 'cloud/static/assets/js',
}

/* CSS */
gulp.task('styles', function(done) {
	gulp
		.src('src/styles/**/*.sass')
		.pipe(gulpif(enabled.failCheck, plumber()))
		.pipe(gulpif(enabled.maps, sourcemaps.init()))
		.pipe(sass({outputStyle: 'compressed'}))
		.pipe(autoprefixer("last 3 versions"))
		.pipe(gulpif(enabled.maps, sourcemaps.write(".")))
		.pipe(gulp.dest(destination.css))
		.pipe(gulpif(argv.livereload, livereload()));
	done();
});


/* Templates - Pug */
gulp.task('templates', function(done) {
	gulp.src('src/templates/**/*.pug')
		.pipe(gulpif(enabled.failCheck, plumber()))
		.pipe(cache('templating'))
		.pipe(pug({
				pretty: enabled.prettyPug
		}))
		.pipe(gulp.dest(destination.pug))
		.pipe(gulpif(argv.livereload, livereload()));
	done();
});


/* Individually uglified scripts */
gulp.task('scripts', function(done) {
	gulp.src('src/scripts/*.js')
		.pipe(gulpif(enabled.failCheck, plumber()))
		.pipe(cache('scripting'))
		.pipe(gulpif(enabled.maps, sourcemaps.init()))
		.pipe(gulpif(enabled.uglify, uglify()))
		.pipe(rename({suffix: '.min'}))
		.pipe(gulpif(enabled.maps, sourcemaps.write(".")))
		.pipe(gulp.dest(destination.js))
		.pipe(gulpif(argv.livereload, livereload()));
	done();
});


/* Collection of scripts in src/scripts/tutti/ to merge into tutti.min.js */
/* Since it's always loaded, it's only for functions that we want site-wide */
gulp.task('scripts_tutti', function(done) {
	gulp.src('src/scripts/tutti/**/*.js')
		.pipe(gulpif(enabled.failCheck, plumber()))
		.pipe(gulpif(enabled.maps, sourcemaps.init()))
		.pipe(concat("tutti.min.js"))
		.pipe(gulpif(enabled.uglify, uglify()))
		.pipe(gulpif(enabled.maps, sourcemaps.write(".")))
		.pipe(gulp.dest(destination.js))
		.pipe(gulpif(argv.livereload, livereload()));
	done();
});


// While developing, run 'gulp watch'
gulp.task('watch', function(done) {
	// Only reload the pages if we run with --livereload
	if (argv.livereload){
		livereload.listen();
	}

	gulp.watch('src/styles/**/*.sass', gulp.series('styles'));
	gulp.watch('src/templates/**/*.pug', gulp.series('templates'));
	gulp.watch('src/scripts/*.js', gulp.series('scripts'));
	gulp.watch('src/scripts/tutti/*.js', gulp.series('scripts_tutti'));
});


// Run 'gulp' to build everything at once
gulp.task('default', gulp.series('styles', 'templates', 'scripts', 'scripts_tutti'));