"use strict";

var controllers = require('./lib/controllers'),
	user = require.main.require('./src/user'),
	groups = require.main.require('./src/groups'),
	emailer = require.main.require('./src/emailer'),
	meta = require.main.require('./src/meta'),
	notifications = require.main.require('./src/notifications'),
	utils = require.main.require('./public/src/utils'),

	async = require.main.require('async'),
	nconf = require.main.require('nconf'),
	winston = require.main.require('winston'),

	plugin = {};

plugin.init = function(params, callback) {
	var router = params.router,
		hostMiddleware = params.middleware,
		hostControllers = params.controllers;
		
	// We create two routes for every view. One API call, and the actual route itself.
	// Just add the buildHeader middleware to your route and NodeBB will take care of everything for you.

	router.get('/admin/plugins/registration-notification', hostMiddleware.admin.buildHeader, controllers.renderAdminPage);
	router.get('/api/admin/plugins/registration-notification', controllers.renderAdminPage);

	callback();
};

plugin.addAdminNavigation = function(header, callback) {
	header.plugins.push({
		route: '/plugins/registration-notification',
		icon: 'fa-tint',
		name: 'Registration Notification'
	});

	callback(null, header);
};

plugin.onRegister = function(data) {
	var allowed = ['normal', 'invite-only', 'admin-invite-only'];
	if (allowed.indexOf(meta.config.registrationType) !== -1) {
		sendNotification(data);
	}
};

plugin.onQueued = function(data, callback) {
	var allowed = ['admin-approval', 'admin-approval-ip'];
	if (allowed.includes(meta.config.registrationApprovalType)) {
		var payload = data.userData;
		payload.userslug = utils.slugify(payload.username);
		sendNotification({
			user: payload,
		});
	} 
	callback(null, data);
};

function sendNotification(data) {
	async.parallel({
		method: async.apply(meta.settings.getOne, 'registration-notification', 'method'),
		adminUids: async.apply(groups.getMembers, 'administrators', 0, -1)
	}, function(err, metadata) {
		if (metadata.method === 'email') {
			var site_title = meta.config.title !== undefined ? meta.config.title : 'NodeBB';

			async.eachSeries(metadata.adminUids, function(uid, next) {
				emailer.send('new-registration', uid, {
					site_title: site_title,
					subject: '[' + site_title + '] New User Registration',
					user: data.user,
					url: nconf.get('url')
				}, next);
			}, onError);
		} else {
			// No match, send notification
			notifications.create({
				bodyShort: 'A user by the name of ' + data.user.username + ' has registered',
				bodyLong: '',
				image: data.user.picture,
				nid: 'plugin:registration-notification:' + Date.now(),
				path: '/user/' + data.user.userslug
			}, function(err, notification) {
				notifications.push(notification, metadata.adminUids, onError);
			});
		}
	});

	var onError = function(err) {
		if (err) {
			winston.error('[plugin/registration-notification] Encountered an error while notifying admins.');
			console.log(err.stack);
		}
	};
};

module.exports = plugin;

@@ -8,19 +8,21 @@
 rict%22;%0A%0A
-var
+const
  control
@@ -56,19 +56,24 @@
 ollers')
-,%0A%09
+;%0Aconst 
 user = r
@@ -105,19 +105,24 @@
 c/user')
-,%0A%09
+;%0Aconst 
 groups =
@@ -158,19 +158,24 @@
 groups')
-,%0A%09
+;%0Aconst 
 emailer 
@@ -213,19 +213,24 @@
 mailer')
-,%0A%09
+;%0Aconst 
 meta = r
@@ -262,19 +262,24 @@
 c/meta')
-,%0A%09
+;%0Aconst 
 notifica
@@ -333,16 +333,23 @@
 ns')
-,%0A%09utils
+;%0Aconst slugify
  = r
@@ -375,30 +375,34 @@
 ('./
-public/src/utils'),%0A%0A%09
+src/slugify');%0A    %0Aconst 
 asyn
@@ -434,19 +434,24 @@
 'async')
-,%0A%09
+;%0Aconst 
 nconf = 
@@ -479,19 +479,24 @@
 'nconf')
-,%0A%09
+;%0Aconst 
 winston 
@@ -532,12 +532,17 @@
 on')
-,%0A%0A%09
+;%0A%0Aconst 
 plug
@@ -550,10 +550,22 @@
 n = 
-%7B%7D
+module.exports
 ;%0A%0Ap
@@ -1763,14 +1763,8 @@
 g = 
-utils.
 slug
@@ -3100,30 +3100,4 @@
 %0A%7D;%0A
-%0Amodule.exports = plugin;%0A

use slugify module