const auth = require('@feathersjs/authentication');
const { BadRequest } = require('@feathersjs/errors');
const globalHooks = require('../../../hooks');

const restrictToCurrentSchool = globalHooks.ifNotLocal(globalHooks.restrictToCurrentSchool);
const restrictToUsersOwnClasses = globalHooks.ifNotLocal(globalHooks.restrictToUsersOwnClasses);

const validateInput = (context) => {

	if(!(context.data.gradeLevel || (context.data.name && context.data.name.trim())) {
		throw new BadRequest('If grade level is not set, a name have to be');
	}

	return context;
}

const prepareGradeLevelUnset = (context) => {
	if (!context.data.gradeLevel) {
		const unset = context.data.$unset || {};
		unset.gradeLevel = '';
		context.data.$unset = unset;
	}

	return context;
};

exports.before = {
	all: [auth.hooks.authenticate('jwt')],
	find: [
		globalHooks.hasPermission('USERGROUP_VIEW'),
		restrictToCurrentSchool,
		restrictToUsersOwnClasses,
	],
	get: [restrictToUsersOwnClasses],
	create: [globalHooks.hasPermission('USERGROUP_CREATE'), restrictToCurrentSchool],
	update: [globalHooks.hasPermission('USERGROUP_EDIT'), restrictToCurrentSchool, prepareGradeLevelUnset],
	patch: [
		globalHooks.hasPermission('USERGROUP_EDIT'),
		restrictToCurrentSchool,
		globalHooks.permitGroupOperation,
		prepareGradeLevelUnset,
	],
	remove: [globalHooks.hasPermission('USERGROUP_CREATE'), restrictToCurrentSchool, globalHooks.permitGroupOperation],
};

const addDisplayName = (hook) => {
	let data = hook.result.data || hook.result;
	const arrayed = !(Array.isArray(data));
	data = (Array.isArray(data)) ? (data) : ([data]);
	if ((((hook.params.query || {}).$sort || {}).displayName || {}).toString() === '1') {
		data.sort((a, b) => a.displayName.toLowerCase() > b.displayName.toLowerCase());
	} else if ((((hook.params.query || {}).$sort || {}).displayName || {}).toString() === '-1') {
		data.sort((a, b) => a.displayName.toLowerCase() < b.displayName.toLowerCase());
	}

	if (arrayed) {
		data = data[0];
	}
	if (hook.result.data) {
		hook.result.data = data;
	} else {
		(hook.result = data);
	}
	return Promise.resolve(hook);
};

exports.after = {
	all: [],
	find: [addDisplayName],
	get: [
		addDisplayName,
		globalHooks.ifNotLocal(
			globalHooks.denyIfNotCurrentSchool({
				errorMessage: 'Die angefragte Gruppe gehört nicht zur eigenen Schule!',
			}),
		)],
	create: [],
	update: [],
	patch: [],
	remove: [],
};

@@ -348,20 +348,29 @@
 nst 
-validateInpu
+prepareGradeLevelUnse
 t = 
@@ -388,15 +388,14 @@
 %3E %7B%0A
-%0A
 %09if
+ 
 (!
-(
 cont
@@ -418,233 +418,28 @@
 vel 
-%7C%7C (context.data.name && context.data.name.trim())) %7B%0A%09%09throw new BadRequest('If grade level is not set, a name have to be');%0A%09%7D%0A%0A%09return context;%0A%7D%0A%0Aconst prepareGradeLevelUnset = (context) =%3E %7B%0A%09if (!context.data.gradeLevel
+&& context.data.name
 ) %7B%0A
@@ -778,24 +778,27 @@
 ,%0A%09create: %5B
+%0A%09%09
 globalHooks.
@@ -823,33 +823,35 @@
 RGROUP_CREATE'),
- 
+%0A%09%09
 restrictToCurren
@@ -857,16 +857,19 @@
 ntSchool
+,%0A%09
 %5D,%0A%09upda
@@ -869,24 +869,27 @@
 ,%0A%09update: %5B
+%0A%09%09
 globalHooks.
@@ -920,17 +920,19 @@
 _EDIT'),
- 
+%0A%09%09
 restrict
@@ -947,17 +947,19 @@
 tSchool,
- 
+%0A%09%09
 prepareG
@@ -972,16 +972,19 @@
 velUnset
+,%0A%09
 %5D,%0A%09patc

remove input validation