/**
 * Compile jade files in directory.
 * @function task.worker.compileJadeDirectory
 * @param {object} config - Work configuration.
 * @param {string} config.srcDir - Source jade directory.
 * @param {string} config.ignore - Ignore  jade pattern. Glob.
 * @param {boolean} config.minify - Minify file or not.
 * @param {string} config.dataFile - File name which contains jade render context data.
 * @param {string} config.pattern - Source jade file pattern. Glob.
 * @param {string} config.destDir - Destination html directory.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var apeman = require('apeman'),
    path = require('path'),
    debug = require('./_debug'),
    lib = require('../../lib'),
    compile = lib.compile,
    file = lib.file,
    html = lib.html,
    prettifyHtml = html.prettifyHtml,
    minify = lib.minify,
    minifyHtmlFile = minify.minifyHtmlFile,
    forceUnlink = file.forceUnlink,
    writeReadonlyFile = file.writeReadonlyFile,
    _convertDirectory = require('./_convert_directory'),
    compileJadeFile = compile.compileJadeFile;

exports = module.exports = function (config, callback) {
    var shouldMinify = config.minify,
        data = require(config.dataFile);
    _convertDirectory({
        srcDir: config.srcDir,
        destDir: config.destDir,
        pattern: config.pattern || '**/*.+(js|jade)',
        ignore: config.ignore,
        fileNameConverter: function (filename) {
            return filename.replace(/\.jade$/, '.html').replace(/\.js$/, '.html');
        },
        fileContentConverter: function (src, dest, callback) {
            exports._compile(src, dest, data, function (err) {
                if (shouldMinify) {
                    exports._minify(dest, function (err, minified) {
                        if (!err) {
                            debug.didCreateFile(minified);
                        }
                        callback(err);
                    });
                } else {
                    if (!err) {
                        debug.didCreateFile(dest);
                    }
                    callback(err);
                }
            });
        }
    }, function (err) {
        callback(err);
    });
};

exports._compile = function (src, dest, data, callback) {
    var isCompiler = path.extname(src) === '.js';
    if (isCompiler) {
        var compiled;
        try {
            compiled = prettifyHtml(require(src)(data));
        } catch (e) {
            callback(e);
        }
        if (!compiled) {
            callback(null);
            return;
        }
        writeReadonlyFile(dest, compiled.toString(), function (err) {
            callback(err);
        });
    } else {
        compileJadeFile(src, dest, data, callback);
    }
};

exports._minify = function (src, callback) {
    var minified = src.replace(/\.html$/, '.min.html');
    minifyHtmlFile(src, minified, function (err) {
        if (err) {
            callback(err);
            return;
        }
        forceUnlink(src, function (err) {
            callback(err, minified);
        });
    });
};