/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

lib_plankton.zoo_page.register(
	"view",
	async (parameters, target_element) => {
		const id : int = parseInt(parameters["id"]);
		
		let dom_fragment : DocumentFragment = template_request("view");
		
		dom_fragment.querySelector(".view-title").textContent = lib_plankton.translate.get("page.view.title");
		
		const member_data = await _espe.backend.member_get(id);
		
		const form = new lib_plankton.zoo_form.class_form<
			{
				membership_number : string;
				name_real_value : string;
				name_real_index : int;
				registered : boolean;
				enabled : boolean;
				email_address_private : (null | string);
				email_address_veiled : (null | string);
				email_address_nominal : (null | string);
				email_redirect_to_private_address : boolean;
				email_allow_sending : boolean;
				name_login : string;
				password_set : boolean;
			},
			{
				membership_number : string;
				name_real_value : string;
				name_real_index : int;
				registered : boolean;
				enabled : boolean;
				email_address_private : (null | string);
				email_address_veiled : (null | string);
				email_address_nominal : (null | string);
				email_redirect_to_private_address : boolean;
				email_allow_sending : boolean;
				name_login : string;
				password_set : boolean;
			}
		>(
			value => value,
			representation => representation,
			new lib_plankton.zoo_input.class_input_group(
				[
					{
						"name": "name_real_value",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.name_real_value.label"),
					},
					{
						"name": "name_real_index",
						"input": new lib_plankton.zoo_input.class_input_number({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.name_real_index.label"),
					},
					{
						"name": "membership_number",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.membership_number.label"),
					},
					{
						"name": "enabled",
						"input": new lib_plankton.zoo_input.class_input_checkbox(),
						"label": lib_plankton.translate.get("page.view.form.field.enabled.label"),
					},
					{
						"name": "registered",
						"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.registered.label"),
					},
					{
						"name": "name_login",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.name_login.label"),
					},
					{
						"name": "password_set",
						"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.password_set.label"),
					},
					{
						"name": "email_address_private",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": lib_plankton.translate.get("page.view.form.field.email_address_private.label"),
					},
					{
						"name": "email_address_veiled",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.email_address_veiled.label"),
					},
					{
						"name": "email_address_nominal",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.email_address_nominal.label"),
					},
					{
						"name": "email_redirect_to_private_address",
						"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
						"label": lib_plankton.translate.get("page.view.form.field.email_redirect_to_private_address.label"),
					},
					{
						"name": "email_allow_sending",
						"input": new lib_plankton.zoo_input.class_input_checkbox(),
						"label": lib_plankton.translate.get("page.view.form.field.email_allow_sending.label"),
					},
				]
			),
			(
				[
					{
						"label": lib_plankton.translate.get("page.view.form.action.save"),
						"procedure": async (get_value, get_representation) => {
							const value = await get_value();
							await _espe.backend.member_modify(
								id,
								{
									"email_address_private": value.email_address_private,
									"enabled": value.enabled,
									"registered": value.registered,
								}
							);
						},
					},
				]
				.concat(
					member_data.registered
					? []
					: [
						{
							"label": lib_plankton.translate.get("page.view.form.action.summon"),
							"procedure": async (get_value, get_representation) => {
								const url : string = await _espe.backend.member_summon(
									id,
									lib_plankton.zoo_page.encode(
										{
											"name": "register",
											"parameters": {
												"id": id,
												"verification": "{{verification}}",
											}
										}
									)
								);
								if (_espe.conf.get().settings.test_mode) {
									alert(lib_plankton.translate.get("page.view.misc.test_info", {"url": url}));
								}
								else {
									alert(lib_plankton.translate.get("page.view.misc.summoned"));
								}
							},
						},
					]
				)
			)
		);
		await form.setup(dom_fragment.querySelector(".view-form") as HTMLElement);
		await form.input_write(
			{
				"membership_number": member_data.membership_number,
				"name_real_value": member_data.name_real_value,
				"name_real_index": member_data.name_real_index,
				"registered": member_data.registered,
				"enabled": member_data.enabled,
				"email_address_private": member_data.email_address_private,
				"email_address_veiled": (
					member_data.email_use_veiled_address
					? member_data.email_address_veiled
					: ("(" + lib_plankton.translate.get("common.not_used") + ")")
				),
				"email_address_nominal": (
					member_data.email_use_nominal_address
					? member_data.email_address_nominal
					: ("(" + lib_plankton.translate.get("common.not_used") + ")")
				),
				"email_redirect_to_private_address": member_data.email_redirect_to_private_address,
				"email_allow_sending": member_data.email_allow_sending,
				"name_login": member_data.name_login,
				"password_set": member_data.password_set,
			}
		);
		
		target_element.appendChild(dom_fragment);
	},
);
