// Env.
require( 'dotenv' ).config();

// Config.
const rootPath = './';

// Gulp.
const gulp = require( 'gulp' );

// Babel.
const babel = require( 'gulp-babel' );

// Plumber.
const plumber = require( 'gulp-plumber' );

// Autoprefixer.
const autoprefixer = require( 'gulp-autoprefixer' );

// Sass.
const sass = require( 'gulp-sass' )( require( 'sass' ) );

// Sourcemaps.
const sourcemaps = require( 'gulp-sourcemaps' );

// Browser sync.
const browserSync = require( 'browser-sync' ).create();

// Environments.
const environments = require( 'gulp-environments' );
const development = environments.development;

// Style - public.
gulp.task( 'style:public', function() {
	return gulp.src( rootPath + 'public/src/sass/*.scss' )
		.pipe( development( sourcemaps.init() ) )
		.pipe( sass().on( 'error', sass.logError ) )
		.pipe( development( sourcemaps.write( { includeContent: false } ) ) )
		.pipe( development( sourcemaps.init( { loadMaps: true } ) ) )
		.pipe( plumber() )
		.pipe( sass() )
		.pipe( autoprefixer() )
		.pipe( development( sourcemaps.write( '.' ) ) )
		.pipe( gulp.dest( rootPath + 'public/assets/css' ) );
} );

// Style - admin.
gulp.task( 'style:admin', function() {
	return gulp.src( rootPath + 'admin/src/sass/*.scss' )
		.pipe( development( sourcemaps.init() ) )
		.pipe( sass().on( 'error', sass.logError ) )
		.pipe( development( sourcemaps.write( { includeContent: false } ) ) )
		.pipe( development( sourcemaps.init( { loadMaps: true } ) ) )
		.pipe( plumber() )
		.pipe( sass() )
		.pipe( autoprefixer() )
		.pipe( development( sourcemaps.write( '.' ) ) )
		.pipe( gulp.dest( rootPath + 'admin/assets/css' ) );
} );

// Script - public.
gulp.task( 'script:public', function() {
	return gulp.src( [ rootPath + 'public/src/script/*.js' ] )
		.pipe( development( sourcemaps.init() ) )
		.pipe( babel( {
			presets: [ '@babel/env' ],
		} ) )
		.pipe( development( sourcemaps.write( '.' ) ) )
		.pipe( gulp.dest( rootPath + 'public/assets/js' ) );
} );

// Script - admin.
gulp.task( 'script:admin', function() {
	return gulp.src( [ rootPath + 'admin/src/script/*.js' ] )
		.pipe( development( sourcemaps.init() ) )
		.pipe( babel( {
			presets: [ '@babel/env' ],
		} ) )
		.pipe( development( sourcemaps.write( '.' ) ) )
		.pipe( gulp.dest( rootPath + 'admin/assets/js' ) );
} );

// Images.
gulp.task( 'images:admin', function() {
	return gulp.src( [ rootPath + 'admin/src/images/**/*.{gif,jpg,png,svg}' ] )
		.pipe( gulp.dest( rootPath + 'admin/assets/images' ) );
} );

// Watch.
gulp.task( 'watch', function() {
	browserSync.init( {
		proxy: process.env.DEV_SERVER_URL,
		open: true,
	} );

	// Watch SCSS - public files.
	gulp.watch( rootPath + 'public/src/sass/**/*.scss', gulp.series( 'style:public' ) ).on( 'change', browserSync.reload );

	// Watch SCSS - admin files.
	gulp.watch( rootPath + 'admin/src/sass/**/*.scss', gulp.series( 'style:admin' ) );

	// Watch JS - public files.
	gulp.watch( rootPath + 'public/src/script/**/*.js', gulp.series( 'script:public' ) ).on( 'change', browserSync.reload );

	// Watch JS - admin files.
	gulp.watch( rootPath + 'admin/src/script/**/*.js', gulp.series( 'script:admin' ) );

	// Watch PHP files.
	gulp.watch( rootPath + '**/**/*.php' ).on( 'change', browserSync.reload );
} );

// Tasks.
gulp.task( 'default', gulp.series( 'watch' ) );
gulp.task( 'style', gulp.series( 'style:public', 'style:admin' ) );
gulp.task( 'script', gulp.series( 'script:public', 'script:admin' ) );
gulp.task( 'build', gulp.series( 'style', 'script', 'images:admin' ) );
