/**
 * Generate api guide document. Using jsdoc.
 * @function task.worker.generateColorThemesLessFiles
 * @param {object} config - Work configuration.
 * @param {string} config.destDir - Destination directory.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var path = require('path'),
    fs = require('fs'),
    util = require('util'),
    lib = require('../../lib'),
    format = util.format,
    file = lib.file,
    math = lib.math,
    string = lib.string,
    mkdirP = file.mkdirP,
    capitalize = string.capitalize,
    naturalNumbers = math.naturalNumbers,
    writeReadonlyFile = file.writeReadonlyFile,
    _colorThemesData = require('./_color_themes_data'),
    _schemeColors = require('./_scheme_colors'),
    debug = require('./_debug'),
    loadHbsTmpl = file.loadHbsTmpl;

exports = module.exports = function (config, callback) {
    var destDir = path.resolve(config.destDir);
    exports._loadColorThemesTmpl(function (err, tmpl) {
        if (err) {
            callback(err);
            return;
        }
        var index = 0;
        require('async').series(
            _colorThemesData()
                .map(function (data) {
                    return function (callback) {
                        var scheme = data.scheme,
                            variation = data.variation,
                            hue = data.hue,
                            themeName = data.themeName,
                            dest = path.resolve(destDir, format('%s-%s/%s-%s-color-scheme-theme-%s.less',
                                variation, scheme, variation, scheme, themeName));
                        var content = tmpl({
                            scheme: scheme,
                            index: ++index,
                            themeName: themeName,
                            themeFullName: format('%s-%s-%s', variation, scheme, themeName),
                            schemeColors: _schemeColors(hue, scheme, variation),
                            variation: variation,
                            hue: hue,
                            Variation: capitalize(variation)
                        });
                        var destParentDir = path.dirname(dest);
                        mkdirP(destParentDir, function (err) {
                            if (err) {
                                callback(err);
                                return;
                            }
                            writeReadonlyFile(dest, content, function (err) {
                                if (!err) {
                                    debug.didCreateFile(dest);
                                }
                                callback(err);
                            });
                        });
                    }
                }),
            callback
        );

    });
};


/**
 * Load the color theme less template.
 * @param {function} callback
 * @protected
 * @ignore
 */
exports._loadColorThemesTmpl = function (callback) {
    var filename = path.resolve(__dirname, '../../tmpl/less/themes/theme-color.less.hbs');
    loadHbsTmpl(filename, callback);
};