"use strict";

var pathUtil = require("path");
var Q = require("q");
var gulp = require("gulp");
var rollup = require("rollup");
var less = require("gulp-less");
var uglify = require("rollup-plugin-uglify");
var jetpack = require("fs-jetpack");

var utils = require("./utils");

var projectDir = jetpack;
var srcDir = projectDir.cwd("./app");
var destDir = projectDir.cwd("./build");

var paths = {
    copyFromAppDir: [
        "./node_modules/**",
        "./vendor/**",
        "./images/**",
        "./**/*.html"
    ],
}

// -------------------------------------
// Tasks
// -------------------------------------

gulp.task("clean", function(callback) {
    return destDir.dirAsync(".", { empty: true });
});


var copyTask = function () {
    return projectDir.copyAsync("app", destDir.path(), {
        overwrite: true,
        matching: paths.copyFromAppDir
    });
};
gulp.task("copy", ["clean"], copyTask);
gulp.task("copy-watch", copyTask);


var bundle = function (src, dest) {
    var deferred = Q.defer();

    rollup.rollup({
        entry: src,
        plugins: [
          //  uglify()
        ]
    }).then(function (bundle) {
        var jsFile = pathUtil.basename(dest);
        
        var result = bundle.generate({
            format: "iife",
            sourceMap: true,
            sourceMapFile: jsFile,
        });

        return Q.all([
            destDir.writeAsync(dest, result.code + "\n//# sourceMappingURL=" + jsFile + ".map"),
            destDir.writeAsync(dest + ".map", result.map.toString()),
        ]);
    }).then(function () {
        deferred.resolve();
    }).catch(function (err) {
        console.error(err);
    });

    return deferred.promise;
};

var bundleApplication = function () {
    return Q.all([
        bundle(srcDir.path("background.js"), destDir.path("background.js")),
        bundle(srcDir.path("app.js"), destDir.path("app.js")),
    ]);
};

var bundleTask = function () {    
    return bundleApplication();
};

gulp.task("bundle", ["clean"], bundleTask);
gulp.task("bundle-watch", bundleTask);

var lessTask = function () {
    return gulp.src("app/stylesheets/main.less")
    .pipe(less())
    .pipe(gulp.dest(destDir.path("stylesheets")));
};
gulp.task("less", ["clean"], lessTask);
gulp.task("less-watch", lessTask);

gulp.task("finalize", ["clean"], function () {
    var manifest = srcDir.read("package.json", "json");
    // Add "dev" or "test" suffix to name, so Electron will write all data
    // like cookies and localStorage in separate places for each environment.
    switch (utils.getEnvName()) {
        case "development":
            manifest.name += "-dev";
            manifest.productName += " Dev";
            break;
        case "test":
            manifest.name += "-test";
            manifest.productName += " Test";
            break;
    }
    destDir.write("package.json", manifest);

    var configFilePath = projectDir.path("config/env_" + utils.getEnvName() + ".json");
    destDir.copy(configFilePath, "env_config.json");
});


gulp.task("watch", function () {
    gulp.watch("app/**/*.js", ["bundle-watch"]);
    gulp.watch(paths.copyFromAppDir, { cwd: "app" }, ["copy-watch"]);
    gulp.watch("app/**/*.less", ["less-watch"]);
});

gulp.task("build", ["bundle", "less", "copy", "finalize"]);
