var fs = require('fs');
var path = require('path');
var sizeOf = require('image-size');
var mustache = require('mustache');
var through = require('through');
var gutil = require('gulp-util');
var mime = require('mime');
var SVGO = require('svgo');
var svgo = new SVGO();

module.exports = function(opts) {
	var tpl = fs.readFileSync(opts.styleTemplate).toString();
	var buffer = [];
	var svgs = [];

	var bufferContents = function(file) {
		var type = mime.lookup(file.path);
		var dim = sizeOf(file.path);

		if(type !== 'image/svg+xml') {
			// console.log(imgData, dim);
			dim.data = [
				'url(data:',
				mime.lookup(dim.type),
				';base64,',
				file.contents.toString('base64'),
				')'
			].join('');
			dim.name = path.basename(file.path, '.' + dim.type);
			buffer.push(dim);
		} else if(type === 'image/svg+xml') {
			svgo.optimize(file.contents.toString(), function(res) {
				dim = {
					width: dim.width,
					height: dim.height,
					data: [
						'url(data:image/svg+xml;charset=utf8,',
						encodeURIComponent(res.data),
						')'
					].join(''),
					format: 'svg',
					name: path.basename(file.path, '.svg')
				};
				buffer.push(dim);
			});
		}
	};

	var endStream = function() {
		this.emit('data', new gutil.File({
			  contents: new Buffer(mustache.render(tpl, {
				  items: buffer
			  }), 'utf8'),
			  path: opts.styleName
		  }));
		this.emit('end');
	};

	return new through(bufferContents, endStream);
};

var fs = require('fs');
var path = require('path');
var sizeOf = require('image-size');
var mustache = require('mustache');
var through = require('through');
var gutil = require('gulp-util');
var mime = require('mime');
var SVGO = require('svgo');
var svgo = new SVGO();

function strictEncodeURIComponent(str) {
	return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
		return '%' + c.charCodeAt(0).toString(16);
	});
}

module.exports = function(opts) {
	var tpl = fs.readFileSync(opts.styleTemplate).toString();
	var buffer = [];
	var svgs = [];

	var bufferContents = function(file) {
		var type = mime.lookup(file.path);
		var dim = sizeOf(file.path);

		if(type !== 'image/svg+xml') {
			// console.log(imgData, dim);
			dim.data = [
				'url(data:',
				mime.lookup(dim.type),
				';base64,',
				file.contents.toString('base64'),
				')'
			].join('');
			dim.name = path.basename(file.path, '.' + dim.type);
			buffer.push(dim);
		} else if(type === 'image/svg+xml') {
			svgo.optimize(file.contents.toString(), function(res) {
				dim = {
					width: dim.width,
					height: dim.height,
					data: [
						'url(data:image/svg+xml;charset=utf8,',
						strictEncodeURIComponent(res.data),
						')'
					].join(''),
					format: 'svg',
					name: path.basename(file.path, '.svg')
				};
				buffer.push(dim);
			});
		}
	};

	var endStream = function() {
		this.emit('data', new gutil.File({
			  contents: new Buffer(mustache.render(tpl, {
				  items: buffer
			  }), 'utf8'),
			  path: opts.styleName
		  }));
		this.emit('end');
	};

	return new through(bufferContents, endStream);
};

Support RFC 3986 strict