'use strict';

var config = protoss.config.images,
    packages = protoss.packages,
    notifier = protoss.helpers.notifier;

/**
 * Make svg-icons for use in html
 */

module.exports = function () {
    packages.gulp.task('protoss/images/make-svg-icons', function(cb) {

        if(config.svgIcons) {

            var icons,
                queue;

            var makeSprite = function (iconSet) {

                var make = function () {

                    packages.gulp.src(config.src + 'svg-icons/' + iconSet + '/*.svg')

                        // Prevent pipe breaking
                        .pipe(packages.plumber(function(error) {
                            notifier.error('An error occurred while making svg icons: ' + error);
                            this.emit('end');
                        }))

                        // Add classes
                        .pipe(packages.cheerio({
                            run: function ($, file) {
                                var $svg = $('svg');
                                // http://www.carsonshold.com/2015/11/svg-icon-workflow-with-gulp-and-shopify/
                                if (file.relative.indexOf('--colored') >= 0){
                                    $svg.addClass('svg-icon svg-icon--colored')
                                }
                                $svg.addClass('svg-icon');
                            }
                        }))

                        // Make icons
                        .pipe(packages.svgSprite({
                            svg: {
                                namespaceClassnames: false
                            },
                            shape: {
                                id: {
                                    generator: function(name) {
                                        var id = name.substr(0, name.lastIndexOf('.')) || name;
                                        id = 'icon-' + id;
                                        if (config.svgIconsPrefix && iconSet != 'main' && config.svgIconsInline)
                                            id = iconSet + '_' + id;
                                        return id;
                                    }
                                }
                            },
                            mode: {
                                symbol: {
                                    inline: config.svgIconsInline,
                                    sprite: iconSet,
                                    dest: '.'
                                }
                            }
                        }))

                        // Save icons.svg file
                        .pipe(packages.gulp.dest(config.dest + 'icons/'))

                        // Save for inline embedding
                        // TODO: remove or wrap in if statement
                        .pipe(packages.rename({
                            prefix: '_',
                            extname: '.jade'
                        }))
                        .pipe(packages.gulp.dest(protoss.config.templates.src + 'icons/'))

                        .on('end', handleQueue)

                };

                var handleQueue = function() {

                    notifier.info('Svg-icons processed:', iconSet);

                    if(queue) {

                        queue--;

                        if(queue === 0) {

                            if(protoss.flags.isWatching)
                                packages.browserSync.reload();

                            notifier.success('Svg-icons maked');

                            cb(null); // End task

                        }
                    }
                };

                return make();

            };

            icons = protoss.helpers.listDir(config.src + 'svg-icons', 'dirs', 'names'); // get folders with icons
            queue = icons.length;

            if(queue)
                icons.forEach(makeSprite);
            else
                cb(null); // End task

        } else {
            cb(null); // End task
        }

    });
};

@@ -2676,32 +2676,129 @@
           .pipe(
+packages.gulpif(%0A                            !config.svgIconsInline,%0A                            
 packages.gulp.de
@@ -2815,35 +2815,35 @@
 dest + 'icons/')
-)%0A%0A
+%0A  
                 
@@ -2852,38 +2852,60 @@
     
-  // Save for inline embedding
+))%0A                        .on('end', handleQueue)%0A%0A
 %0A   
@@ -2932,44 +2932,45 @@
  // 
-TODO: remov
+Or save in .jad
 e 
+f
 or 
-wrap in if statement
+inline embedding
 %0A   
@@ -3009,16 +3009,15 @@
 ges.
-rename(%7B
+gulpif(
 %0A   
@@ -3045,19 +3045,29 @@
     
-prefix: '_'
+config.svgIconsInline
 ,%0A  
@@ -3092,16 +3092,45 @@
         
+packages.rename(%7Bprefix: '_',
 extname:
@@ -3137,16 +3137,18 @@
  '.jade'
+%7D)
 %0A       
@@ -3164,17 +3164,16 @@
         
-%7D
 ))%0A     
@@ -3189,32 +3189,128 @@
           .pipe(
+packages.gulpif(%0A                            config.svgIconsInline,%0A                            
 packages.gulp.de
@@ -3352,18 +3352,43 @@
 icons/')
-)
 %0A
+                        ))
 %0A       

fix svg icons task - now not created .jade with icon if disable inline svg sprite