/*
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(
	"member_view",
	async (parameters, target_element) => {
		const id : int = parseInt(parameters["id"]);
		
		let dom_fragment : DocumentFragment = template_request("member_view");
		
		dom_fragment.querySelector(".member_view-title").textContent = lib_plankton.translate.get("page.member_view.title");
		
		/**
		 * @todo cache
		 */
		const groups_as_array : Array<{id : int; name : string;}> = await _espe.backend.group_list();
		const groups_as_map : Map<int, {name : string;}> = new Map<int, {name : string;}>();
		for (const group_thingy of groups_as_array)
		{
			groups_as_map.set(group_thingy.id, {"name": group_thingy.name});
		}
		
		const member_data = await _espe.backend.member_get(id);
		
		const form = new lib_plankton.zoo_form.class_form<
			{
				name : string;
				groups : Array<int>;
				enabled : boolean;
				email_address : (null | string);
				password_set : boolean;
			},
			{
				name : string;
				groups : Array<int>;
				enabled : boolean;
				email_address : (null | string);
				password_set : boolean;
			}
		>(
			value => ({
				"name": value.name,
				"groups": value.groups,
				"enabled": value.enabled,
				"email_address": value.email_address,
				"password_set": value.password_set,
			}),
			representation => ({
				"name": representation.name,
				"groups": representation.groups,
				"enabled": representation.enabled,
				"email_address": representation.email_address,
				"password_set": representation.password_set,
			}),
			new lib_plankton.zoo_input.class_input_group(
				[
					{
						"name": "name",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("domain.member.name.label"),
					},
					{
						"name": "enabled",
						"input": new lib_plankton.zoo_input.class_input_checkbox(),
						"label": lib_plankton.translate.get("domain.member.enabled.label"),
					},
					{
						"name": "password_set",
						"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
						"label": lib_plankton.translate.get("domain.member.password_set.label"),
					},
					{
						"name": "email_address",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": lib_plankton.translate.get("domain.member.email_address.label"),
					},
					{
						"name": "groups",
						/*
						"input": new lib_plankton.zoo_input.class_input_list(
							() => new lib_plankton.zoo_input.class_input_number()
						),
						 */
						"input": new lib_plankton.zoo_input.class_input_wrapped<Set<string>, Array<int>>(
							new _espe.class_input_set(
								groups_as_array.map(
									group_thingy => group_thingy.id.toFixed(0)
								),
								group_id_encoded => groups_as_map.get(parseInt(group_id_encoded)).name
							),
							(value_inner) => {
								const array : Array<int> = [];
								for (const group_id_encoded of value_inner)
								{
									array.push(parseInt(group_id_encoded));
								}
								return array;
							},
							(value_outer) => new Set<string>(value_outer.map(group_id => group_id.toFixed(0)))
						),
						"label": lib_plankton.translate.get("domain.member.groups.label"),
					},
				]
			),
			(
				[
					{
						"label": lib_plankton.translate.get("page.member_view.form.action.save"),
						"procedure": async (get_value, get_representation) => {
							const value = await get_value();
							await _espe.backend.member_modify(
								id,
								{
									"email_address": value.email_address,
									"groups": value.groups,
									"enabled": value.enabled,
								}
							);
						},
					},
				]
				/*
				.concat(
					member_data.registered
					? []
					: [
						{
							"label": lib_plankton.translate.get("page.member_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": "member_register",
											"parameters": {
												"id": id,
												"verification": "{{verification}}",
											}
										}
									)
								);
								if (_espe.conf.get().settings.test_mode) {
									alert(lib_plankton.translate.get("page.member_view.misc.test_info", {"url": url}));
								}
								else {
									alert(lib_plankton.translate.get("page.member_view.misc.summoned"));
								}
							},
						},
					]
				)
				 */
			)
		);
		await form.setup(dom_fragment.querySelector(".member_view-form") as HTMLElement);
		await form.input_write(
			{
				"name": member_data.name,
				"groups": member_data.groups,
				"enabled": member_data.enabled,
				"email_address": member_data.email_address,
				"password_set": member_data.password_set,
			}
		);
		
		target_element.appendChild(dom_fragment);
	},
);
