/**
 * Generate Iconfont and Sass file using Settings passed
 * and folder of icon svgs
 */
module.exports = function(gulp, projectPath, themePath, config) {
    var TASK_NAME = 'iconfont',
        FONT_NAME = 'ms-icons';

    return {
        name: TASK_NAME,
        task: function getIconFontTask(done) {
            var _subTasks = [],
                _extend = require('merge'),
                _defaultSettingsIconFontCss = {},
                _settingsIconFontCss = {},
                _defaultSettingsIconFont = {},
                _settingsIconFont = {},
                _iconfont = require('gulp-iconfont'),
                iconfontCss = require('gulp-iconfont-css'),
                merge = require('merge-stream'),
                runTimestamp = Math.round(Date.now()/1000);

            // console.log(themePath + config.src);
            // console.log(themePath + config.dest);

            _defaultSettingsIconFontCss = {
                fontName: FONT_NAME,
                path: './task/_icon-font-map.template.scss',
                targetPath: '../../css/_icon-font-map.scss',
                fontPath: '../fonts/icons/'
            };

            _defaultSettingsIconFont = {
                prependUnicode: false, // Recommended option
                fontName: 'icons', // Name of the font
                formats: ['ttf', 'eot', 'woff', 'svg', 'woff2'], // The font file formats that will be created
                normalize: true,
                fontHeight: 1001,
                timestamp: runTimestamp // Recommended to get consistent builds when watching files
            };

            if (typeof config.iconFontCss === 'undefined') {
                config.iconFontCss = {};
            }

            if (typeof config.iconFont === 'undefined') {
                config.iconFont = {};
            }

            _settingsIconFontCss = _extend.recursive(true, _defaultSettingsIconFontCss, config.iconFontCss);
            _settingsIconFont = _extend.recursive(true, _defaultSettingsIconFont, config.iconFont);

            _subTasks.push(
                gulp.src(
                    themePath + config.src,
                    {
                        cwd: projectPath,
                        allowEmpty: true
                    })
                .pipe( iconfontCss(_settingsIconFontCss))
                .pipe(_iconfont(_settingsIconFont))
                .pipe(gulp.dest(
                    themePath + config.dest,
                    { cwd: projectPath }
                ))
            );

            return merge(_subTasks);
        },
        setProjectPath: function (path) {
            projectPath = path;
        },
        setThemePath: function (path) {
            themePath = path;
        }
    }
};

