
/**
 * Created by chen on 2017/9/26.
 */

let gulp =require( 'gulp');
let gulpLoadPlugins = require('gulp-load-plugins');
let del = require('del');
let uglify = require('gulp-uglify-es').default;
let config = require("./config/gulpConfig.js");
let  runSequence = require('run-sequence');

const $ = gulpLoadPlugins();
let common_jspaths=config.common_jspaths;
let common_css=config.common_css;
let pm_jspaths=config.pm_jspaths;
let pm_csspaths=config.pm_css;
let login_jspaths=config.login_jspaths;
let main_jspaths=config.main_jspaths;
let main_csspaths=config.main_css;
let version=config.version;

let taskObject={};
function task_init(options) {
    taskObject=options;
    taskObject.version=version;
    taskObject.scriptsDest='web/dest/scripts';
}

gulp.task('minify', function (){
    if(taskObject.jspaths){
        return gulp.src(taskObject.jspaths)
            .pipe($.plumber())
           // .pipe(uglify())
            .pipe($.concat(taskObject.concatName+"."+version+".js"))
            .pipe(gulp.dest(taskObject.scriptsDest));
    }
   return;
});


gulp.task('copy',function () {
    return gulp.src(taskObject.srcHtml)
            .pipe($.plumber())
            .pipe(gulp.dest(taskObject.htmlDest));
})

gulp.task('inject',['minify','css'], function () {
    var target = gulp.src(taskObject.htmlDest+'/'+taskObject.htmlName);
    var sources = gulp.src(taskObject.injectList, {read: false});

    return target.pipe($.plumber())
         .pipe($.inject(sources))
         .pipe(gulp.dest(taskObject.htmlDest));
});

gulp.task('htmlmin',function () {
    return gulp.src(taskObject.htmlDest+'/*.html')
      //  .pipe($.htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest(taskObject.htmlDest));
})

gulp.task('css',function () {
    if(taskObject.pm_csspaths){
        return gulp.src(taskObject.pm_csspaths)
            .pipe($.plumber())
            .pipe($.cssnano())
            .pipe($.concat(taskObject.concatName+"."+version+".css"))
            .pipe(gulp.dest(taskObject.cssDest));
    }
    return;
})

gulp.task('common', function (){
    let options = {
        jspaths:common_jspaths,
        pm_csspaths:common_css,
        concatName:'common.all.min',
        cssDest:'web/dest/css'
    }
    task_init(options);
    runSequence('minify','css');
});

gulp.task('header', function (){
    let options = {
        concatName:'header.all.min',
        srcHtml:'web/src/html/common/header.html',
        htmlDest:'web/common/html',
        htmlName:'header.html',
        injectList:['web/dest/scripts/common.all.min.'+version+'.js']
    };
    task_init(options);
    runSequence('inject','htmlmin');
});


gulp.task('login', function (){
    let options = {
        jspaths:login_jspaths,
        concatName:'login.all.min',
        srcHtml:'web/src/html/login/login.html',
        htmlDest:'web/users/html',
        htmlName:'login.html',
        injectList:[  'web/dest/scripts/common.all.min.'+version+'.js',
                        'web/dest/scripts/login.all.min.'+version+'.js',
                        'web/dest/css/common.all.min.'+version+'.css']
    }
    task_init(options);
    runSequence('inject','htmlmin');
});

gulp.task('pm', function (){
    let options = {
        jspaths:pm_jspaths,
        pm_csspaths:pm_csspaths,
        concatName:'pm.all.min',
        cssDest:'web/dest/css',
        srcHtml:'web/src/html/pm/*.html',
        htmlDest:'web/building_saas/pm/html',
        htmlName:'project-management.html',
        injectList:['web/dest/scripts/pm.all.min.'+version+'.js',
                      'web/dest/css/common.all.min.'+version+'.css',
                      'web/dest/css/pm.all.min.'+version+'.css']
    }
    task_init(options);
    runSequence('inject','htmlmin');
});

gulp.task('main',function () {
    let options ={
        jspaths:main_jspaths,
        pm_csspaths:main_csspaths,
        concatName:'main.all.min',
        cssDest:'web/dest/css',
        srcHtml:'web/src/html/main/main.html',
        htmlDest:'web/building_saas/main/html',
        htmlName:'main.html',
        injectList:['web/dest/scripts/main.all.min.'+version+'.js',
            'web/dest/css/common.all.min.'+version+'.css',
            'web/dest/css/main.all.min.'+version+'.css']
    }
    task_init(options);
    runSequence('inject','htmlmin');
})

gulp.task('build',['common'],function () {
    runSequence('header',['login','pm','main']);
});