var gulp = require("gulp"),
    fs = require('fs'),
    include = require("gulp-include"),
    babel = require('gulp-babel'),
    rename = require('gulp-rename'),
    uglify = require('gulp-uglify'),
    pump = require('pump'),
    replace = require('gulp-token-replace'),
    header = require('gulp-header'),
    d = new Date(),
    headerComment = '/*Generated on:' + d + '*/\n';


    gulp.task("scripts3", function () {
        console.log("-- gulp is running task 'scripts'");
        var config = fs.readFileSync('./all_missions.json');
        var iframes = fs.readFileSync('./html/iframes.html');
        var navbar = fs.readFileSync('./html/navbar.html');
        gulp.src("src/autoloaderV2.js")
            .pipe(include())
            .on('error', console.log)
            .pipe(replace({
                global:{
                    ADis_AllMissions: config.toString(),
                    ADis_Iframes: iframes.toString(),
                    ADis_Navbar: navbar.toString()
                },
                prefix:"/***",
                suffix:"***/0"
            }))
            .pipe(replace({
                global:{
                    ADis_AllMissions: config.toString(),
                    ADis_Iframes: iframes.toString(),
                    ADis_Navbar: navbar.toString()
                },
                prefix:"/***",
                suffix:"***/ 0"
            }))
            .pipe(header(headerComment))
            .pipe(gulp.dest("dist"));
    });

    gulp.task("scripts", function () {
        console.log("-- gulp is running task 'scripts'");
        var config = fs.readFileSync('./all_missions.json', );
        gulp.src("src/autoloader.js")
            .pipe(include())
            .on('error', console.log)
            .pipe(replace({
                global:{
                    ADis_AllMissions: config.toString()
                },
                prefix:"/***",
                suffix:"***/0"
            }))
            .pipe(header(headerComment))
            .pipe(gulp.dest("dist"));
    });

gulp.task("minify", ["scripts"], function (cb) {
    console.log("-- gulp is running task 'minify'");
    pump([
            gulp.src('src/autoloader.js'),
            babel({
                presets: ['@babel/env']
            }),
            uglify(),
            rename({ suffix: '.min' }),
            gulp.dest('dist')
        ],
        cb
    );
});

gulp.task("scripts2", function () {
    console.log("-- gulp is running task 'scripts'");

    gulp.src("src/standalone.js")
        .pipe(include())
        .on('error', console.log)
        .pipe(header(headerComment))
        .pipe(gulp.dest("dist"));
});

gulp.task("minify2", ["scripts2"], function (cb) {
    console.log("-- gulp is running task 'minify'");
    pump([
            gulp.src('src/standalone.js'),
            babel({
                presets: ['@babel/env']
            }),
            uglify(),
            rename({ suffix: '.min' }),
            gulp.dest('dist')
        ],
        cb
    );
});

gulp.task("default", ["scripts"]);