/**
 * 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");

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 compleGlj_jspaths = config.compleGlj_jspaths;
let compleGlj_csspaths = config.compleGlj_css;
let compleRation_ration_jspaths = config.compleRation_ration_jspaths;
let compleRation_ration_csspaths = config.compleRation_ration_css;
let compleRation_glj_jspaths = config.compleRation_glj_jspaths;
let compleRation_glj_csspaths = config.compleRation_glj_css;
let compleRation_coe_jspaths = config.compleRation_coe_jspaths;
let compleRation_coe_csspaths = config.compleRation_coe_css;
let compleRation_inst_csspaths = config.compleRation_inst_css;
let compleRation_inst_jspaths = config.compleRation_inst_jspaths;
let unitPrice_jspaths = config.unitPrice_jspaths;

let version=config.version;
let cssDest='web/dest/css';
let scriptsDest='web/dest/scripts';

let commonOptions={
    jspaths:common_jspaths,
    csspaths:common_css,
    concatName:'common.all.min'
};

let headerOptions={
    version:version,
    scriptsDest:scriptsDest,
    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']
};

let loginOptions={
    version:version,
    scriptsDest:scriptsDest,
    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']
}

let pmOptions={
    version:version,
    scriptsDest:scriptsDest,
    jspaths:pm_jspaths,
    csspaths:pm_csspaths,
    concatName:'pm.all.min',
    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']
};

let mainOptions={
    version:version,
    scriptsDest:scriptsDest,
    jspaths:main_jspaths,
    csspaths:main_csspaths,
    concatName:'main.all.min',
    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']
}

let compleGljOptions = {
    version: version,
    scriptsDest: scriptsDest,
    jspaths: compleGlj_jspaths,
    csspaths: compleGlj_csspaths,
    concatName: 'compleGlj.all.min',
    srcHtml: 'web/src/html/complementary_glj_lib/tools-gongliaoji.html',
    htmlDest: 'web/building_saas/complementary_glj_lib/html/tools-gongliaoji.html',
    htmlName: 'tools-gongliaoji.html',
    injectList: [
        'web/dest/scripts/compleGlj.all.min' + version + '.js',
        'web/dest/css/compleGlj.all.min' + version + '.css',
        'web/dest/css/common.all.min.' + version + '.css'
    ]
};

let compleRation_rationOptions = {
    version: version,
    scriptsDest: scriptsDest,
    jspaths: compleRation_ration_jspaths,
    csspaths: compleRation_ration_csspaths,
    concatName: 'compleRation_ration.all.min',
    srcHtml: 'web/src/html/complementary_ration_lib/dinge.html',
    htmlDest: 'web/building_saas/complementary_ration_lib/html/dinge.html',
    htmlName: 'dinge.html',
    injectList: [
        'web/dest/scripts/compleRation_ration.all.min' + version + '.js',
        'web/dest/css/compleRation_ration.all.min' + version + '.css',
        'web/dest/scripts/common.all.min.'+version+'.js',
        'web/dest/css/common.all.min.' + version + '.css'
    ]
};

let compleRation_gljOptions = {
    version: version,
    scriptsDest: scriptsDest,
    jspaths: compleRation_glj_jspaths,
    csspaths: compleRation_glj_csspaths,
    concatName: 'compleRation_glj.all.min',
    srcHtml: 'web/src/html/complementary_ration_lib/gongliao.html',
    htmlDest: 'web/building_saas/complementary_ration_lib/html/gongliao.html',
    htmlName: 'gongliao.html',
    injectList: [
        'web/dest/scripts/compleRation_glj.all.min' + version + '.js',
        'web/dest/scripts/compleRation_glj.all.min' + version + '.css',
        'web/dest/scripts/common.all.min.'+version+'.js',
        'web/dest/css/common.all.min.' + version + '.css'
    ]
};

let compleRation_coeOptions = {
    version: version,
    scriptsDest: scriptsDest,
    jspaths: compleRation_coe_jspaths,
    csspaths: compleRation_coe_csspaths,
    concatName: 'compleRation_coe.all.min',
    srcHtml: 'web/src/html/complementary_ration_lib/fuzhu.html',
    htmlDest: 'web/building_saas/complementary_ration_lib/html/fuzhu.html',
    htmlName: 'fuzhu.html',
    injectList: [
        'web/dest/scripts/compleRation_coe.all.min' + version + '.js',
        'web/dest/scripts/compleRation_coe.all.min' + version + '.css',
        'web/dest/scripts/common.all.min.'+version+'.js',
        'web/dest/css/common.all.min.' + version + '.css'
    ]
};

let compleRation_instOptions = {
    version: version,
    scriptsDest: scriptsDest,
    jspaths: compleRation_inst_jspaths,
    csspaths: compleRation_inst_csspaths,
    concatName: 'compleRation_inst.all.min',
    srcHtml: 'web/src/html/complementary_ration_lib/anzhuang.html',
    htmlDest: 'web/building_saas/complementary_ration_lib/html/anzhuang.html',
    htmlName: 'anzhuang.html',
    injectList: [
        'web/dest/scripts/compleRation_inst.all.min' + version + '.js',
        'web/dest/scripts/compleRation_inst.all.min' + version + '.css',
        'web/dest/scripts/common.all.min.'+version+'.js',
        'web/dest/css/common.all.min.' + version + '.css'
    ]
}

let unitPriceOptions={
  version:version,
  scriptsDest:scriptsDest,
  jspaths:unitPrice_jspaths,
  concatName:'unitPrice.all.min',
  htmlDest:'web/building_saas/unit_price_file',
  htmlName:'index.html',
  injectList:['web/dest/scripts/unitPrice.all.min.'+version+'.js']
}

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

function css(options) {
    if(options.csspaths){
        return gulp.src(options.csspaths)
            .pipe($.plumber())
            .pipe($.cssnano({reduceIdents: false}))
            .pipe($.concat(options.concatName+"."+version+".css"))
            .pipe(gulp.dest(cssDest));
    }
    return;
}

function inject(options) {
    var target = gulp.src(options.htmlDest+'/'+options.htmlName);
    var sources = gulp.src(options.injectList, {read: false});

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

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


gulp.task('minify', function (){
    return minify(commonOptions);
});

gulp.task('css',function () {
    return css(commonOptions);
})

gulp.task('common', ['minify','css']);

gulp.task('login_minify',['common'], function (){
    return minify(loginOptions);
});

gulp.task('login_css',function () {
    return css(loginOptions);
})

gulp.task('login_inject',['login_minify','login_css'],function () {
    return inject(loginOptions);
})

gulp.task('login',['login_inject'], function (){
    return htmlmin(loginOptions);
});


gulp.task('header_minify',['common'], function (){
    return minify(headerOptions);
});

gulp.task('header_css',function () {
    return css(headerOptions);
})

gulp.task('header_inject',['header_minify','header_css'],function () {
    return inject(headerOptions);
})

gulp.task('header',['header_inject'], function (){
    return htmlmin(headerOptions);
});

gulp.task('pm_minify',['common'], function (){
    return minify(pmOptions);
});

gulp.task('pm_css',function () {
    return css(pmOptions);
})

gulp.task('pm_inject',['pm_minify','pm_css'],function () {
    return inject(pmOptions);
})

gulp.task('pm',['pm_inject'], function (){
    return htmlmin(pmOptions);
});

gulp.task('main_minify',['common'], function (){
    return minify(mainOptions);
});

gulp.task('main_css',function () {
    return css(mainOptions);
})

gulp.task('main_inject',['main_minify'],function () {//, ['main_minify','main_css'  ] main css 打包到一起会出现样式冲突问题， 现改成不打包
    return inject(mainOptions);
})

gulp.task('main',['main_inject'], function (){
    return htmlmin(mainOptions);
});

gulp.task('compleGlj_minify', ['common'], function () {
    return minify(compleGljOptions);
});

gulp.task('compleGlj_css', function () {
    return css(compleGljOptions);
});

gulp.task('compleGlj_inject', ['compleGlj_minify', 'compleGlj_css'], function () {
    return inject(compleGljOptions);
});

gulp.task('compleGlj', ['compleGlj_inject'], function () {
    return htmlmin(compleGljOptions);
});

gulp.task('compleRation_ration_minify', ['common'], function () {
    return minify(compleRation_rationOptions);
});

gulp.task('compleRation_ration_css', function () {
    return css(compleRation_rationOptions);
});

gulp.task('compleRation_ration_inject', ['compleRation_ration_minify', 'compleRation_ration_css'], function () {
    return inject(compleRation_rationOptions);
});

gulp.task('compleRation_ration', ['compleRation_ration_inject'], function () {
    return htmlmin(compleRation_rationOptions);
});

gulp.task('compleRation_glj_minify', ['common'], function () {
    return minify(compleRation_gljOptions);
});

gulp.task('compleRation_glj_css', function () {
    return css(compleRation_gljOptions);
});

gulp.task('compleRation_glj_inject', ['compleRation_glj_minify', 'compleRation_glj_css'], function () {
    return inject(compleRation_gljOptions);
});

gulp.task('compleRation_glj', ['compleRation_glj_inject'], function () {
    return htmlmin(compleRation_gljOptions);
});

gulp.task('compleRation_coe_minify', ['common'], function () {
    return minify(compleRation_coeOptions);
});

gulp.task('compleRation_coe_css', function () {
    return css(compleRation_coeOptions);
});

gulp.task('compleRation_coe_inject', ['compleRation_coe_minify', 'compleRation_coe_css'], function () {
    return inject(compleRation_coeOptions);
});

gulp.task('compleRation_coe', ['compleRation_coe_inject'], function () {
    return htmlmin(compleRation_coeOptions);
});

gulp.task('compleRation_inst_minify', ['common'], function () {
    return minify(compleRation_instOptions);
});

gulp.task('compleRation_inst_css', function () {
    return css(compleRation_instOptions);
});

gulp.task('compleRation_inst_inject', ['compleRation_inst_minify', 'compleRation_inst_css'], function () {
    return inject(compleRation_instOptions);
});

gulp.task('compleRation_inst', ['compleRation_inst_inject'], function () {
    return htmlmin(compleRation_instOptions);
});

gulp.task('unitPrice_minify', function (){
  return minify(unitPriceOptions);
});

gulp.task('unitPrice_inject',['unitPrice_minify'],function (){
  return inject(unitPriceOptions);
})

gulp.task('unit_price',['unitPrice_inject'], function (){
  return htmlmin(unitPriceOptions);
});

gulp.task('build',['header','login','pm','main', 'compleGlj', 'compleRation_ration', 'compleRation_glj', 'compleRation_coe', 'compleRation_inst','unit_price']);