/*
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 = _espe.helpers.template_request("member_view");
		
		dom_fragment.querySelector(".member_view-title").textContent = lib_plankton.translate.get("page.member_view.title");
		
		const group_data = await _espe.logic.group_data();
		
		const member_data = await _espe.backend.member_get(id);
		
		const form = new lib_plankton.zoo_form.class_form<
			{
				name : string;
				label : string;
				groups : Array<int>;
				enabled : boolean;
				email_address : (null | string);
				password_set : boolean;
			},
			{
				name : string;
				label : string;
				groups : Array<int>;
				enabled : boolean;
				email_address : (null | string);
				password_set : boolean;
			}
		>(
			value => ({
				"name": value.name,
				"label": value.label,
				"groups": value.groups,
				"enabled": value.enabled,
				"email_address": value.email_address,
				"password_set": value.password_set,
			}),
			representation => ({
				"name": representation.name,
				"label": representation.label,
				"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": "label",
						"input": new lib_plankton.zoo_input.class_input_text(),
						"label": lib_plankton.translate.get("domain.member.label.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 lib_plankton.zoo_input.class_input_set(
								group_data.order.map(
									group_id => group_id.toFixed(0)
								),
								group_id_encoded => group_data.pool.get(parseInt(group_id_encoded)).label
							),
							(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,
								{
									"label": value.label,
									"email_address": value.email_address,
									"groups": value.groups,
									"enabled": value.enabled,
								}
							);
						},
					},
				]
			)
		);
		await form.setup(dom_fragment.querySelector(".member_view-form") as HTMLElement);
		await form.input_write(
			{
				"name": member_data.name,
				"label": member_data.label,
				"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);
	},
);
