/*
npm run gulp watch

//generate sections
npm run gulp sections 

//generate blocks
npm run gulp blocks

//take screenshots for all sections
npm run gulp screenshots 

//take screenshots for all blocks
npm run gulp screenshots-blocks

//take screenshots for specified section groups
npm run gulp screenshots -hero -features 
*/ 
"use strict";

const gulp = require('gulp');
const fileinclude = require('gulp-file-include');
//const sass = require('gulp-sass')(require('sass'));
const sass = require('gulp-sass')(require('node-sass'));
const sassGlob = require('gulp-sass-glob');
const {formatHTML} = require('gulp-format-html');
//const gutil = require('gulp-util');
const rename = require("gulp-rename");
const imageResize = require('gulp-image-resize');
const through2 = require( 'through2' );    
const fs = require('fs');
const path = require('path');
const { screenshots, sections, styles, connect, disconnect } = require('./vvveb.js');
let baseHref = '';
let blockPrefix = "landing";
let themeName = "landing";

//get command line parameters after some specified parameter
function parameters() {
	return process.argv.slice(3);
}

const touch = () => through2.obj( function( file, enc, cb ) {
    if ( file.stat ) {
        file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
    }
    cb( null, file );
});


console.log( process.env.npm_config_path);
let dir = process.env.npm_config_path;
if (dir) {
	console.log('Starting directory: ' + process.cwd());
	try {
	  process.chdir(dir);
	  console.log('New directory: ' + process.cwd());
	}
	catch (err) {
	  console.log('chdir: ' + err);
	}
}

themeName = path.basename(process.cwd());

gulp.task('fileinclude', function() {
  return gulp.src(['./src/*.html', './src/**/*.html', '!**/-*/**', '!**/_*/**'])
    .pipe(fileinclude({
      prefix: '@@',
      basepath: '@file'
    }))
    .pipe(formatHTML())
    .pipe( touch() )
    .pipe(gulp.dest('./'));
});

gulp.task('sass', function() {
  return gulp.src(['./scss/*.scss', './scss/**/*.scss', '!**/_*/**'])
    .pipe(sassGlob())
    .pipe(sass())
    .pipe(gulp.dest('./css'));
});


gulp.task('watch', function () {
    gulp.watch(['./src/*.html', './src/**/*.html'], gulp.series('fileinclude'));
    gulp.watch(['./scss/*.scss', './scss/**/*.scss', './scss/**/**/*.scss'], gulp.series('sass'));
});


gulp.task('gconnect', async function (done) {
    return await connect();
});

gulp.task('gdisconnect', async function (done) {
    return await disconnect();
});

gulp.task('take-screenshots',  async function (done) {
	 return await screenshots('sections');
});

gulp.task('take-screenshots-blocks',  async function (done) {
	 return await screenshots("blocks");
});

gulp.task('take-screenshots-styles',  async function (done) {
	 return await screenshots("styles");
});

gulp.task('sections', async function () {
	 return sections("section", parameters());
});

gulp.task('blocks', async function () {
	 return sections("block", parameters());
});

gulp.task('styles', async function () {
	 return styles(parameters());
});


// Default Task
gulp.task('default', gulp.series('fileinclude', 'sass'));
gulp.task('screenshots', gulp.series('gconnect', 'take-screenshots', 'gdisconnect'));
gulp.task('screenshots-blocks', gulp.series('gconnect', 'take-screenshots-blocks', 'gdisconnect'));
gulp.task('screenshots-styles', gulp.series('gconnect', 'take-screenshots-styles', 'gdisconnect'));
gulp.task('sections', gulp.series('sections'));
gulp.task('blocks', gulp.series('blocks'));
gulp.task('styles', gulp.series('styles'));
