const gulp = require('gulp');
const concat = require('gulp-concat');
const cleanCss = require('gulp-clean-css');
const browsersync = require('browser-sync').create();
const uglify = require('gulp-uglify');
const del = require('del');
const htmlmin = require('gulp-htmlmin');
const copy = require('gulp-copy');

// Concat and minify CSS files
gulp.task('build-css', () => {
    return gulp.src('src/Css/main.css')
    .pipe(concat('index.css'))
    .pipe(cleanCss())
    .pipe(gulp.dest('build/css'));
});

// Concat and minify application specific JS files
gulp.task('build-js', function () {
    return gulp.src(['src/JavaScript/Css.js', 
                     'src/JavaScript/Firework.js', 
                     'src/JavaScript/PWA.js',
                     'src/JavaScript/Memory.js',
                     'src/JavaScript/main.js'])
        .pipe(concat('index.js'))
        .pipe(uglify())
        .pipe(gulp.dest('build/js'));
});


// Concat and minify application specific JS files
gulp.task('build-sw', function () {
    return gulp.src(['src/sw.js'])
        .pipe(concat('sw.js'))
        .pipe(uglify())
        .pipe(gulp.dest('build'));
});

 
gulp.task('minify-html', () => {
  return gulp.src('src/index.html')
    .pipe(htmlmin({ collapseWhitespace: true }))
    .pipe(gulp.dest('build'));
});


gulp.task('copy-sitemap', function() {
  return gulp.src('src/sitemap.xml')
    .pipe(gulp.dest('build'));
});

gulp.task('copy-robots', function() {
  return gulp.src('src/robots.txt')
    .pipe(gulp.dest('build'));
});

gulp.task('copy-media', function() {
  return gulp.src('src/media/*')
    .pipe(gulp.dest('build/media'));
});

gulp.task('copy-data', function() {
  return gulp.src('src/config/*.json')
    .pipe(gulp.dest('build/config'));
});
gulp.task('copy-fonts', function() {
  return gulp.src('src/fonts/*')
    .pipe(gulp.dest('build/fonts'));
});

gulp.task('copy-favicon', function() {
  return gulp.src('src/favicon/*')
    .pipe(gulp.dest('build/favicon'));
});
gulp.task('copy-favicon-maskable', function() {
  return gulp.src('src/favicon/maskable/*')
    .pipe(gulp.dest('build/favicon/maskable'));
});

gulp.task('clean', async () => {
   return del.sync('build');
});

// Start session
gulp.task("clean build", (cb) => {
    return gulp.series('clean',
        'build-css', 'build-js', 'build-sw', 'minify-html', 
        'copy-media', 'copy-data', 'copy-favicon','copy-favicon-maskable', 'copy-fonts', 'copy-robots', 'copy-sitemap'
                )(cb);
});

// Start session
gulp.task("session-start", (cb) => {
    return gulp.series('clean','build-css', 'build-js','build-sw',  'minify-html', 
    'copy-media', 'copy-data', 'copy-favicon','copy-favicon-maskable',  'copy-fonts', 'copy-robots', 'copy-sitemap')(cb);
});

// Start session
gulp.task("build", (cb) => {
    return gulp.series('build-css', 'build-js','build-sw',  'minify-html', 
    'copy-media', 'copy-data', 'copy-favicon','copy-favicon-maskable',  'copy-fonts', 'copy-robots', 'copy-sitemap')(cb);
});

// static server and watching CSS/JS/HTML files for changes
gulp.task("server", (done) => {
  browsersync.init({
      server: './build',
      directory: true
  });

  // Watch for file changes
  gulp.watch('./src/Css/*.css', gulp.series('session-start'), browsersync.reload);
  gulp.watch('./src/JavaScript/*.js', gulp.series('session-start'), browsersync.reload);
  gulp.watch('./src/*.html', gulp.series('session-start'), browsersync.reload);
});


gulp.task('default', gulp.series('session-start'));