/*
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(
	"invitation_view",
	async (parameters, target_element) => {
		// functions
		const get_url = (item) => {
			const url : URL = new URL(window.location.toString());
			url.hash = lib_plankton.string.coin(
				"#invitation_handle,key={{key}}",
				{
					"key": item.key,
				}
			);
			return url.toString();
		};
		
		// parameters
		const id : int = parseInt(parameters["id"]);
		
		target_element.appendChild(_espe.helpers.template_request("invitation_view"));
		
		target_element.querySelector(".invitation_view-title").textContent = lib_plankton.translate.get("page.invitation_view.title");
		
		const indent = str => (/*"... " + */str);
		
		/**
		 * @todo cache
		 */
		const groups_as_array : Array<
			{
				id : int;
				preview : {
					name : string;
					label : string;
				};
			}
		> = await _espe.backend.group_list();
		const groups_as_map : Map<
			int,
			{
				name : string;
				label : string;
			}
		> = new Map<
			int,
			{
				name : string;
				label : string;
			}
		>();
		for (const group_thingy of groups_as_array)
		{
			groups_as_map.set(
				group_thingy.id,
				{
					"name": group_thingy.preview.name,
					"label": group_thingy.preview.label,
				}
			);
		}
		
		/**
		 * @todo unify with form of "invitation_create"
		 */
		const form = new lib_plankton.zoo_form.class_form<
			{
				key : string;
				expiry : (null | int);
				name_changeable : boolean;
				name_value : string;
				label_changeable : boolean;
				label_value : string;
				email_address_changeable : boolean;
				email_address_value : (null | string);
				groups_changeable : boolean;
				groups_value : Array<int>;
			},
			{
				key : string;
				expiry : (null | lib_plankton.pit.type_datetime);
				name : {
					changeable : boolean;
					value : string;
				};
				label : {
					changeable : boolean;
					value : string;
				};
				email_address : {
					changeable : boolean;
					value : string;
				};
				groups : {
					changeable : boolean;
					value : Array<int>;
				};
				url : string;
			}
		>(
			value => ({
				"expiry": (
					(value.expiry === null)
					?
					null
					:
					lib_plankton.pit.to_datetime(lib_plankton.pit.from_unix_timestamp(value.expiry))
				),
				"key": value.key,
				"name": {
					"changeable": value.name_changeable,
					"value": value.name_value,
				},
				"label": {
					"changeable": value.label_changeable,
					"value": value.label_value,
				},
				"email_address": {
					"changeable": value.email_address_changeable,
					"value": (value.email_address_value ?? ""),
				},
				"groups": {
					"changeable": value.groups_changeable,
					"value": value.groups_value,
				},
				"url": get_url(value),
			}),
			representation => ({
				"expiry": (
					(representation.expiry === null)
					?
					null
					:
					lib_plankton.pit.to_unix_timestamp(lib_plankton.pit.from_datetime(representation.expiry))
				),
				"key": representation.key,
				"name_changeable": representation.name.changeable,
				"name_value": representation.name.value,
				"label_changeable": representation.label.changeable,
				"label_value": representation.label.value,
				"email_address_changeable": representation.email_address.changeable,
				"email_address_value": _espe.helpers.null_when_empty(representation.email_address.value),
				"groups_changeable": representation.groups.changeable,
				"groups_value": representation.groups.value,
				"url": "",
			}),
			new lib_plankton.zoo_input.class_input_group(
				[
					{
						"name": "url",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("domain.invitation.url.label"),
					},
					{
						"name": "key",
						"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
						"label": lib_plankton.translate.get("domain.invitation.key.label"),
					},
					{
						"name": "name",
						"input": new lib_plankton.zoo_input.class_input_group(
							[
								{
									"name": "value",
									"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.changeable")),
								},
							]
						),
						"label": lib_plankton.translate.get("domain.member.name.label"),
					},
					{
						"name": "label",
						"input": new lib_plankton.zoo_input.class_input_group(
							[
								{
									"name": "value",
									"input": new lib_plankton.zoo_input.class_input_text(),
									"label": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.changeable")),
								},
							]
						),
						"label": lib_plankton.translate.get("domain.member.label.label"),
					},
					{
						"name": "email_address",
						"input": new lib_plankton.zoo_input.class_input_group(
							[
								{
									"name": "value",
									"input": new lib_plankton.zoo_input.class_input_text({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.changeable")),
								},
							]
						),
						"label": lib_plankton.translate.get("domain.member.email_address.label"),
					},
					{
						"name": "groups",
						"input": new lib_plankton.zoo_input.class_input_group(
							[
								{
									"name": "value",
									/*
									"input": new lib_plankton.zoo_input.class_input_list(
										() => new lib_plankton.zoo_input.class_input_text(),
										{"read_only": true}
									),
									 */
									"input": new lib_plankton.zoo_input.class_input_wrapped<Set<string>, Array<int>>(
										new lib_plankton.zoo_input.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)).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": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox({"read_only": true}),
									"label": indent(lib_plankton.translate.get("common.changeable")),
								},
							]
						),
						"label": lib_plankton.translate.get("domain.member.groups.label"),
					},
					{
						"name": "expiry",
						"input": new lib_plankton.zoo_input.class_input_soft(
							new lib_plankton.zoo_input.class_input_datetime_central_europe(
								{
									// "read_only": true,
									// "label_timezone_shift": indent(lib_plankton.translate.get("common.timezone_shift")),
									"label_date": indent(lib_plankton.translate.get("common.date")),
									"label_time": indent(lib_plankton.translate.get("common.time")),
								}
							)
						),
						"label": lib_plankton.translate.get("domain.invitation.expiry.label"),
					},
				]
			),
			[
			]
		);
		await form.setup(target_element.querySelector(".invitation_view-form") as HTMLElement);
		
		const data : {
			key: string;
			expiry : (null | int);
			name_changeable : boolean;
			name_value : string;
			label_changeable : boolean;
			label_value : string;
			email_address_changeable : boolean;
			email_address_value : (null | string);
			groups_changeable : boolean;
			groups_value : Array<int>;
		} = await _espe.backend.invitation_read(id);
		
		form.input_write(data);
	}
);
