/**
 * Generate sized images
 * @function task.worker.generateSizedImageFiles
 * @example
 *
 *      resizeImageFiles({
 *          src: 'src/img/favicon.png',
 *          size:[
 *              {width:32, height:32},
 *              {width:64, height:64}
 *          ],
 *          dest:'public/img/favicon-{{width}}x{{height}}.png
 *      }, function(err){
 *          console.log('done!');
 *      });
 *
 * @param {object} config - Work configuration.
 * @param {string} config.src - Source image path.
 * @param {array} config.sizes - Sizes of images.
 * @param {string} config.dest - Dest file path pattern.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var path = require('path'),
    fs = require('fs'),
    debug = require('./_debug'),
    lib = require('../../lib'),
    image = lib.image,
    resizeImage = image.resizeImage;

module.exports = function (config, callback) {
    var async = require('async');
    var src = path.resolve(config.src),
        dest = path.resolve(config.dest);
    async.parallel(
        [].concat(config.sizes).map(function (size) {
            return function (callback) {
                var filename = dest.replace('{{width}}', size.width || size)
                    .replace('{{height}}', size.height || size);
                resizeImage(
                    src,
                    size,
                    filename,
                    function (err) {
                        debug.didCreateImage(filename);
                        callback(err)
                    }
                )
            }
        }),
        function (err) {
            callback(err);
        }
    )
};