/**
 * Generate file config helper.
 * @module ci.worker.generateFileConfigHelper
 * @param {object} config - Task configuration
 * @param {string} config.tmplDir - Template file directory.
 * @param {string} config.dest - Destination file name.
 * @param {string} [config.testDest] - Destination file name for unit test.
 * @author Taka Okunishi
 *
 */
var path = require('path'),
    lib = require('../../lib'),
    file = lib.file,
    string = lib.string,
    _workerName = require('./_worker_name'),
    _loadLocalTmpl = require('./_load_local_tmpl'),
    debug = require('./_debug');

exports = module.exports = function (config, callback) {
    var dest = path.resolve(config.dest),
        baseDir = path.resolve('.'),
        tmplDir = path.resolve(baseDir, config.tmplDir),
        testDest = config.testDest && path.resolve(config.testDest);
    exports._loadFileConfigHelperTmpl(function (err, tmpl) {
        if (err) {
            callback(err);
            return;
        }
        file.readdirRecursive(tmplDir, function (err, tmplFiles) {
            if (err) {
                callback(err);
                return;
            }
            var data = {
                authorModule: _workerName.full(__filename),
                files: tmplFiles
                    .filter(function (filename) {
                        return !exports._shouldIgnore(filename);
                    })
                    .map(function (filename) {
                        return path.resolve(tmplDir, filename);
                    })
                    .map(function (filename) {
                        var name = exports._name(filename);
                        return {
                            name: name,
                            Name: string.capitalize(name),
                            relativePath: path.relative(baseDir, filename)
                        };
                    })
            };
            var content = tmpl(data);
            file.writeReadonlyFile(dest, content, function (err) {
                if (!err) {
                    debug.didCreateFile(dest);
                }
                if (testDest) {
                    data.modulePath = path.relative(path.dirname(testDest), dest);
                    data.moduleName = string.camelize(path.basename(dest, '.js'));
                    exports._writeTestFile(testDest, data, callback);
                } else {
                    callback(err);
                }
            });

        });
    });
};

exports._writeTestFile = function (dest, data, callback) {
    exports._loadFileConfigHelperTestTmpl(function (err, tmpl) {
        require('./_write_unit_test_file')(dest, tmpl, data, callback);
    });
};

exports._name = function (filename) {
    return string.camelize(path.basename(filename, '.hbs').replace(/[\.\-]/g, '_')).replace(/_/g, '');
};

exports._shouldIgnore = function (filename) {
    var subDirnames = path.dirname(filename).split(path.sep);
    for (var i = 0; i < subDirnames.length; i++) {
        var isProtected = subDirnames[i].match(/^[_\.].+/);
        if (isProtected) {
            return true;
        }
    }
    var ignorePatterns = ['.DS_Store'];
    for (var j = 0; j < ignorePatterns.length; j++) {
        var hit = path.basename(filename) === ignorePatterns[j];
        if (hit) {
            return true;
        }
    }

    return false;
};


exports._loadFileConfigHelperTmpl = function (callback) {
    _loadLocalTmpl('js/ci_file_config_helper.js.hbs', callback);
};

exports._loadFileConfigHelperTestTmpl = function (callback) {
    _loadLocalTmpl('js/ci_file_config_helper_test.js.hbs', callback);
};