/*
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_create",
	async (parameters, target_element) => {
		target_element.appendChild(_zackeneule.helpers.template_request("invitation_create"));
		
		target_element.querySelector(".invitation_create-title").textContent = lib_plankton.translate.get("page.invitation_create.title");
		
		const indent = str => (/*"... " + */str);
		
		const group_data = await _zackeneule.logic.group_data();
		
		/**
		 * @todo unify with form of "invitation_view"
		 */
		const form = new lib_plankton.zoo_form.class_form<
			{
				data : {
					name_changeable : boolean;
					name_value : (null | string);
					label_changeable : boolean;
					label_value : (null | string);
					email_address_changeable : boolean;
					email_address_value : (null | string);
					groups_changeable : boolean;
					groups_value : Array<int>;
					expiry : (null | int);
				};
				send_immediatly : boolean;
			},
			{
				name : {
					changeable : boolean;
					value : string;
				};
				label : {
					changeable : boolean;
					value : string;
				};
				email_address : {
					changeable : boolean;
					value : string;
				};
				groups : {
					changeable : boolean;
					value : Array<int>;
				};
				expiry : (null | lib_plankton.pit.type_datetime);
				send_immediatly : boolean;
			}
		>(
			value => ({
				"name": {
					"changeable": value.data.name_changeable,
					"value": value.data.name_value,
				},
				"label": {
					"changeable": value.data.label_changeable,
					"value": value.data.label_value,
				},
				"email_address": {
					"changeable": value.data.email_address_changeable,
					"value": (value.data.email_address_value ?? ""),
				},
				"groups": {
					"changeable": value.data.groups_changeable,
					"value": value.data.groups_value,
				},
				"expiry": (
					(value.data.expiry === null)
					?
					null
					:
					lib_plankton.pit.to_datetime(lib_plankton.pit.from_unix_timestamp(value.data.expiry))
				),
				"send_immediatly": value.send_immediatly,
			}),
			representation => ({
				"data": {
					"name_changeable": representation.name.changeable,
					"name_value": _zackeneule.helpers.null_when_empty(representation.name.value),
					"label_changeable": representation.label.changeable,
					"label_value": _zackeneule.helpers.null_when_empty(representation.label.value),
					"email_address_changeable": representation.email_address.changeable,
					"email_address_value": _zackeneule.helpers.null_when_empty(representation.email_address.value),
					"groups_changeable": representation.groups.changeable,
					"groups_value": representation.groups.value,
					"expiry": (
						(representation.expiry === null)
						?
						null
						:
						lib_plankton.pit.to_unix_timestamp(lib_plankton.pit.from_datetime(representation.expiry))
					),
				},
				"send_immediatly": representation.send_immediatly,
			}),
			new lib_plankton.zoo_input.class_input_group(
				[
					{
						"name": "name",
						"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(),
									"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(),
									"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(),
									"label": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox(),
									"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_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": indent(lib_plankton.translate.get("common.initial_value")),
								},
								{
									"name": "changeable",
									"input": new lib_plankton.zoo_input.class_input_checkbox(),
									"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(
								{
									// "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"),
					},
					{
						"name": "send_immediatly",
						"input": new lib_plankton.zoo_input.class_input_checkbox(),
						"label": indent(lib_plankton.translate.get("page.invitation_create.form.field.send_immediatly.label")),
					},
				]
			),
			[
				{
					"label": lib_plankton.translate.get("page.invitation_create.form.action.submit"),
					"procedure": async (get_value, get_representation) => {
						const value = await get_value();
						const result : {id : int; key : string;} = await _zackeneule.backend.invitation_create(
							value.data,
							value.send_immediatly
						);
						lib_plankton.zoo_page.set(
							false
							?
							{
								"name": "invitation_view",
								"parameters": {"id": result.id.toFixed(0)}
							}
							:
							{
								"name": "invitation_list",
								"parameters": {}
							}
						);
					},
				}
			]
		);
		await form.setup(target_element.querySelector(".invitation_create-form") as HTMLElement);
		form.input_write(
			{
				"data": {
					"name_changeable": false,
					"name_value": "",
					"label_changeable": true,
					"label_value": "",
					"email_address_changeable": true,
					"email_address_value": null,
					"groups_changeable": false,
					"groups_value": [],
					/**
					 * @todo conf
					 */
					"expiry": lib_plankton.pit.shift_week(lib_plankton.pit.now(), 2),
				},
				"send_immediatly": true,
			}
		);
	}
);
