"use strict";
const gulp = require("gulp");
const sass = require("gulp-sass");
const autoprefixer = require("gulp-autoprefixer");
const cssmin = require("gulp-cssmin");
const rename = require("gulp-rename");
const replace = require("gulp-replace");
const scssMerge = require("./merge");
const copyFn = require("./copy");
const config = require("../../config/dev.env");

const PACKAGE_PATH = "../../node_modules/"; //node包路径
const STATIC_PATH = "../../static/themes/style/"; //主题文件路径
const FONT_PATH = "../../node_modules/ui-component-v4/lib/theme-chalk/fonts/**"; //图标库路径
const IMAGES_PATH = "../../node_modules/ui-component-v4/lib/theme-chalk/images/**"; //图标库路径
const ICON_PATH = "../../node_modules/ui-component-v4/packages/theme-chalk/src/icon/**"; //图标库路径

gulp.task("copyProjectResources", function() {
    let installedProject = config["INSTALLED_PROJECT"];
    console.log(installedProject);
    return gulp.src("").pipe(copyFn(installedProject));
});

gulp.task("mergeScss", function() {
    return (
        gulp
            .src("./style/variable-theme-default.scss")
            .pipe(scssMerge("style/bundle.scss", "../" + PACKAGE_PATH))
            // .pipe(cssmin())
            .pipe(gulp.dest("./lib"))
    );
});

gulp.task("compile", function() {
    return gulp
        .src("./style/variable-theme-*.scss")
        .pipe(
            replace(/~.*?fonts/g, function(match, p1, offset, string) {
                //替换~ 为包的相对路径
                return "fonts";
            })
        )
        .pipe(
            replace(/~.*?images/g, function(match, p1, offset, string) {
                //替换~ 为包的相对路径
                return "images";
            })
        )
        .pipe(
            replace(/'~/g, function(match, p1, offset, string) {
                //替换~ 为包的相对路径
                return "'" + PACKAGE_PATH;
            })
        )
        .pipe(
            replace(/"~/g, function(match, p1, offset, string) {
                //替换~ 为包的相对路径
                return '"' + PACKAGE_PATH;
            })
        )
        .pipe(sass())
        .pipe(
            autoprefixer({
                browsers: ["ie > 9", "last 2 versions"],
                cascade: false
            })
        )
        .pipe(cssmin())
        .pipe(
            rename(function(dir, base, ext) {
                //重命名输出文件，如theme-blue.css
                return base.replace("variable-", "") + ".css";
            })
        )
        .pipe(gulp.dest(STATIC_PATH));
});
gulp.task("compile-dev", function() {
    return (
        gulp
            .src("./style/variable-theme-default.scss")
            .pipe(
                replace(/~.*?fonts/g, function(match, p1, offset, string) {
                    //替换~ 为包的相对路径
                    return "fonts";
                })
            )
            .pipe(
                replace(/~.*?images/g, function(match, p1, offset, string) {
                    //替换~ 为包的相对路径
                    return "images";
                })
            )
            .pipe(
                replace(/'~/g, function(match, p1, offset, string) {
                    //替换~ 为包的相对路径
                    return "'" + PACKAGE_PATH;
                })
            )
            .pipe(
                replace(/"~/g, function(match, p1, offset, string) {
                    //替换~ 为包的相对路径
                    return '"' + PACKAGE_PATH;
                })
            )
            .pipe(sass())
            .pipe(
                autoprefixer({
                    browsers: ["ie > 9", "last 2 versions"],
                    cascade: false
                })
            )
            // .pipe(cssmin())
            .pipe(
                rename(function(dir, base, ext) {
                    //重命名输出文件，如theme-blue.css
                    return base.replace("variable-", "") + ".css";
                })
            )
            .pipe(gulp.dest(STATIC_PATH))
    );
});

gulp.task("copyfont", function() {
    return gulp
        .src(FONT_PATH)
        .pipe(cssmin())
        .pipe(gulp.dest(STATIC_PATH + "/fonts"));
});

gulp.task("copyicon", function() {
    return gulp
        .src(ICON_PATH)
        .pipe(cssmin())
        .pipe(gulp.dest(STATIC_PATH + "/icon"));
});

gulp.task("copyuiimages", function() {
    return gulp
        .src(IMAGES_PATH)
        .pipe(cssmin())
        .pipe(gulp.dest(STATIC_PATH + "/images"));
});

gulp.task("copyimages", function() {
    return gulp
        .src("images/**")
        .pipe(cssmin())
        .pipe(gulp.dest(STATIC_PATH + "../images"));
});

gulp.task("watch", function() {
    gulp.watch("./style/*.scss", ["compile"]);
});
gulp.task("default", ["compile", "copyfont", "copyicon", "copyimages", "copyuiimages"]);
gulp.task("merge", ["mergeScss"]);
gulp.task("watching", ["compile", "watch"]);
gulp.task("copyResources", ["copyProjectResources"]);
