var mime = require('mime');

function unsupportedLink(link){
  return (link[0].type != 'style' && link[0].type != 'style-block') ||
         link[1][1] != 'uri';
};

(module.exports = function(flow){
  var fconsole = flow.console;
  var quota = flow.options.cssInlineImage;

  for (var i = flow.files.queue.length - 1, file; file = flow.files.queue[i]; i--)
  {
    if (file.type == 'image')
    {
      fconsole.start(file.relpath);

      if (file.content.length > quota)
      {
        fconsole.endl('[skip] Too big (more than ' + quota + ' bytes)');
        continue;
      }

      if (file.linkBack.some(unsupportedLink))
      {
        fconsole.endl('[skip] Has non-css or not url() references');
        continue;
      }

      //
      // inline
      //

      var fileBase64Content = new Buffer(file.content, 'binary').toString('base64');
      var mimeType = mime.lookup(file.relpath);

      // replace refences
      fconsole.start('Replace references in css');
      for (var j = 0; j < file.linkBack.length; j++)
      {
        var cssToken = file.linkBack[j][1][2];

        fconsole.log(file.linkBack[j][0].relpath);
        cssToken.splice(1, cssToken.length,
          'string',
          'data:' + mimeType + ';base64,' + fileBase64Content);
      }
      fconsole.end();

      // remove from output
      fconsole.log('Remove file from output');
      flow.files.remove(file);

      // end
      fconsole.endl();
    }
  }

}).handlerName = '[res] Inline to base64';

module.exports.skip = function(flow){
  var quota = flow.options.cssInlineImage;

  if (isNaN(quota) || quota <= 0)
    return 'Use option --css-inline-image to set max size of image to be inlined';
};

@@ -1,16 +1,54 @@
+var atCss = require('../../ast').css;%0A
 var mime = requi
@@ -306,16 +306,80 @@
 Image;%0A%0A
+  fconsole.log('Inline resources less ' + quota + ' bytes%5Cn');%0A%0A
   for (v
@@ -620,27 +620,31 @@
 ig (
-more than ' + quota
+' + file.content.length
  + '
@@ -1185,11 +1185,8 @@
 %5D%5B1%5D
-%5B2%5D
 ;%0A%0A 
@@ -1196,125 +1196,347 @@
     
-fconsole.log(file.linkBack%5Bj%5D%5B0%5D.relpath);%0A        cssToken.splice(1, cssToken.length,%0A          'string',%0A          
+// remove link from resource links%0A        flow.resLinks.some(function(link, idx)%7B%0A          if (link.token === cssToken)%0A          %7B%0A            flow.resLinks.splice(idx, 1);%0A            return true;%0A          %7D%0A        %7D);%0A%0A        // replace reference to dataURI%0A        fconsole.log(file.linkBack%5Bj%5D%5B0%5D.relpath);%0A        atCss.packUri(
 'dat
@@ -1582,16 +1582,26 @@
 4Content
+, cssToken
 );%0A     

fix --css-inline-image