/* globals FileUpload */
import _ from 'underscore';

Meteor.startup(function() {
	WebApp.connectHandlers.use('/avatar/', Meteor.bindEnvironment(function(req, res/*, next*/) {
		const params = {
			username: decodeURIComponent(req.url.replace(/^\//, '').replace(/\?.*$/, ''))
		};

		if (_.isEmpty(params.username)) {
			res.writeHead(403);
			res.write('Forbidden');
			res.end();
			return;
		}

		const match = /^\/([^?]*)/.exec(req.url);

		if (match[1]) {
			let username = decodeURIComponent(match[1]);
			let file;

			username = username.replace(/\.jpg$/, '');

			if (username[0] !== '@') {
				if (Meteor.settings && Meteor.settings.public && Meteor.settings.public.sandstorm) {
					const user = RocketChat.models.Users.findOneByUsername(username);
					if (user && user.services && user.services.sandstorm && user.services.sandstorm.picture) {
						res.setHeader('Location', user.services.sandstorm.picture);
						res.writeHead(302);
						res.end();
						return;
					}
				}
				file = RocketChat.models.Avatars.findOneByName(username);
			}

			if (file) {
				res.setHeader('Content-Security-Policy', 'default-src \'none\'');

				return FileUpload.get(file, req, res);
			} else {
				res.setHeader('Content-Type', 'image/svg+xml');
				res.setHeader('Cache-Control', 'public, max-age=0');
				res.setHeader('Expires', '-1');
				res.setHeader('Last-Modified', 'Thu, 01 Jan 2015 00:00:00 GMT');

				const reqModifiedHeader = req.headers['if-modified-since'];

				if (reqModifiedHeader) {
					if (reqModifiedHeader === 'Thu, 01 Jan 2015 00:00:00 GMT') {
						res.writeHead(304);
						res.end();
						return;
					}
				}

				if (RocketChat.settings.get('UI_Use_Name_Avatar')) {
					const user = RocketChat.models.Users.findOneByUsername(username, {
						fields: {
							name: 1
						}
					});

					if (user && user.name) {
						username = user.name;
					}
				}

				let color = '';
				let initials = '';

				if (username === '?') {
					color = '#000';
					initials = username;
				} else {
					color = RocketChat.getAvatarColor(username);

					initials = username.replace(/[^A-Za-z0-9]/g, '').substr(0, 1).toUpperCase();
				}

				const svg = `<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" pointer-events=\"none\" width=\"50\" height=\"50\">\n<rect height="50" width="50" fill=\"${ color }\"/>\n<text text-anchor=\"middle\" y=\"50%\" x=\"50%\" dy=\"0.36em\" pointer-events=\"auto\" fill=\"#ffffff\" font-family=\"Helvetica, Arial, Lucida Grande, sans-serif\" font-weight="400" font-size="28">\n${ initials }\n</text>\n</svg>`;

				res.write(svg);
				res.end();

				return;
			}
		}

		res.writeHead(404);
		res.end();
		return;
	}));
});

@@ -2184,70 +2184,8 @@
 = %60%3C
-?xml version=%5C%221.0%5C%22 encoding=%5C%22UTF-8%5C%22 standalone=%5C%22no%5C%22?%3E%5Cn%3C
 svg 
@@ -2224,32 +2224,8 @@
 vg%5C%22
- pointer-events=%5C%22none%5C%22
  wid
@@ -2229,17 +2229,17 @@
 width=%5C%22
-5
+8
 0%5C%22 heig
@@ -2243,17 +2243,17 @@
 eight=%5C%22
-5
+8
 0%5C%22%3E%5Cn%3Cr
@@ -2260,29 +2260,55 @@
 ect 
-height=%2250%22 width=%2250
+width=%5C%22100%25%5C%22 height=%5C%22100%25%5C%22 rx=%5C%226%5C%22 ry=%5C%226%5C
 %22 fi
@@ -2338,32 +2338,9 @@
 ext 
-text-anchor=%5C%22middle%5C%22 y
+x
 =%5C%225
@@ -2344,17 +2344,17 @@
 %5C%2250%25%5C%22 
-x
+y
 =%5C%2250%25%5C%22
@@ -2367,16 +2367,39 @@
 0.36em%5C%22
+ text-anchor=%5C%22middle%5C%22
  pointer
@@ -2412,12 +2412,12 @@
 s=%5C%22
-auto
+none
 %5C%22 f
@@ -2495,26 +2495,8 @@
 if%5C%22
- font-weight=%22400%22
  fon
@@ -2507,10 +2507,10 @@
 ze=%22
-28
+50
 %22%3E%5Cn

Update svg.