/*
 * SPDX-FileCopyrightText: 2012-2026 Univention GmbH
 * SPDX-License-Identifier: AGPL-3.0-only
 */

/*global define*/

define([
	"dojo/_base/declare",
	"dojo/_base/lang",
	"dojox/html/entities",
	"umc/dialog",
	"umc/widgets/TextBox",
	"umc/widgets/ComboBox",
	"umc/modules/schoolwizards/Wizard",
	"umc/i18n!umc/modules/schoolwizards"
], function(declare, lang, entities, dialog, TextBox, ComboBox, Wizard, _) {

	return declare("umc.modules.schoolwizards.SchoolWizard", [Wizard], {
		getGeneralPage: function() {
			// no need for "school" and "type" widgets
			return null;
		},

		getItemPage: function() {
			var widgets = [{
					type: TextBox,
					name: 'display_name',
					label: _('Name of the school'),
					description: _("The given value will be shown as school's name within UCS@school."),
					required: true
				}, {
					type: TextBox,
					name: 'name',
					label: _('School abbreviation'),
					description: _('The given value will be used as object name for the new school OU object within the LDAP directory and as prefix for several school objects like group names. It may consist of the letters a-z, the digits 0-9 and underscores. Usually it is safe to keep the suggested value.'),
					regExp: '^[a-zA-Z0-9](([a-zA-Z0-9_]*)([a-zA-Z0-9]$))?$',
					depends: ['display_name'],
					dynamicValue: lang.hitch(this, function(values) {
						if (this.editMode) {
							return values.name;
						}
						return values.display_name.replace(/[^a-zA-Z0-9]/g, '');
					}),
					disabled: this.editMode,
					required: true
				}, {
					type: TextBox,
					name: 'dc_name',
					label: _('Name of educational school server'),
					regExp: '^[a-zA-Z0-9](([a-zA-Z0-9-]*)([a-zA-Z0-9]$))?$',
					description: _('Name of the educational Replica Directory Node for the new school. The server name may consist of the letters a-z, the digits 0-9 and hyphens (-). The name of the educational server may not be equal to the administrative server!'),
					maxLength: 15,
					required: !this.editMode && !this.singleMaster,
					visible: !this.editMode && !this.singleMaster
				}, {
					type: TextBox,
					name: 'dc_name_administrative',
					label: _('Name of administrative school server (optional)'),
					regExp: '^[a-zA-Z0-9](([a-zA-Z0-9-]*)([a-zA-Z0-9]$))?$',
					description: _('Name of the administrative Replica Directory Node for the new school. The server name may consist of the letters a-z, the digits 0-9 and hyphens (-). The name of the administrative server may not be equal to the educational server! An administrative school server does not have to be specified.'),
					maxLength: 15,
					required: false,
					visible: !this.editMode && !this.singleMaster
				}];
			if (this.editMode) {
				widgets.push({
					name: 'home_share_file_server',
					type: ComboBox,
					label: _('Server for Windows home directories'),
					dynamicValues: lang.hitch(this, function() {
						return this.umcpCommand('schoolwizards/schools/share_servers').then(function(data) {
							return data.result;
						});
					})
				});
				widgets.push({
					name: 'class_share_file_server',
					type: ComboBox,
					label: _('Server for class shares'),
					dynamicValues: lang.hitch(this, function() {
						return this.umcpCommand('schoolwizards/schools/share_servers').then(function(data) {
							return data.result;
						});
					})
				});
			}
			return {
				name: 'item',
				headerText: this.description,
				widgets: widgets,
				layout: this.getSchoolPageLayout()
			};
		},

		getSchoolPageLayout: function() {
			var layout = [
				['display_name'],
				['name'],
				['dc_name'],
				['dc_name_administrative']
			];
			if (this.editMode) {
				return [{
					label: _('School information'),
					'class': 'umcTitlePaneOnContainer',
					layout: layout
				}, {
					label: _('Advanced settings'),
					'class': 'umcTitlePaneOnContainer',
					open: false,
					layout: ['home_share_file_server', 'class_share_file_server']
				}];
			}
			return layout;
		},

		restart: function() {
			this.getWidget('item', 'display_name').reset();
			this.getWidget('item', 'name').reset();
			this.getWidget('item', 'dc_name').reset();
			this.getWidget('item', 'dc_name_administrative').reset();
			this.inherited(arguments);
		},

		addNote: function() {
			var display_name = this.getWidget('item', 'display_name').get('value');
			var message = _('The school "%s" has been successfully created. Continue to create another school or press "Cancel" to close this wizard.', entities.encode(display_name));
			dialog.contextNotify(message);
		}
	});
});

