namespace _zeitbild.frontend_web.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"event_add",
		async (parameters, target_element) => {
			const calendar_id : (null | int) = (
				("calendar_id" in parameters)
				?
				parseInt(parameters["calendar_id"])
				:
				null
			);
			const year : (null | int) = (
				("year" in parameters)
				?
				parseInt(parameters["year"])
				:
				null
			);
			const month : (null | int) = (
				("month" in parameters)
				?
				parseInt(parameters["month"])
				:
				null
			);
			const day : (null | int) = (
				("day" in parameters)
				?
				parseInt(parameters["day"])
				:
				null
			);
			const date : lib_plankton.pit.type_date = (
				(
					(year !== null)
					&&
					(month !== null)
					&&
					(day !== null)
				)
				?
				{
					"year": year,
					"month": month,
					"day": day,
				}
				:
				lib_plankton.pit.to_datetime(lib_plankton.pit.now()).date
			);
			target_element.innerHTML = "";
			target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
				"event_add",
				"default",
				{
					"label": lib_plankton.translate.get("page.event_add.title")
				}
			);
			const form : lib_plankton.zoo_form.class_form<
				{
					calendar_id : _zeitbild.frontend_web.type.calendar_id;
					event_object : _zeitbild.frontend_web.type.event_object;
				},
				{
					calendar_id : string;
					name : string;
					begin : lib_plankton.pit.type_datetime;
					end : (null | lib_plankton.pit.type_datetime);
					location : (null | string);
					description : (null | string);
				}
			> = new lib_plankton.zoo_form.class_form<
				{
					calendar_id : _zeitbild.frontend_web.type.calendar_id;
					event_object : _zeitbild.frontend_web.type.event_object;
				},
				{
					calendar_id : string;
					name : string;
					begin : lib_plankton.pit.type_datetime;
					end : (null | lib_plankton.pit.type_datetime);
					location : (null | string);
					description : (null | string);
				}
			>(
				(value) => ({
					"calendar_id": value.calendar_id.toFixed(0),
					"name": value.event_object.name,
					"begin": value.event_object.begin,
					"end": value.event_object.end,
					"location": value.event_object.location,
					"description": value.event_object.description,
				}),
				(representation) => ({
					"calendar_id": parseInt(representation.calendar_id),
					"event_object": {
						"name": representation.name,
						"begin": representation.begin,
						"end": representation.end,
						"location": representation.location,
						"description": representation.description,
					}
				}),
				new lib_plankton.zoo_input.class_input_group<any>(
					[
						{
							"name": "calendar_id",
							"input": new lib_plankton.zoo_input.class_input_selection(
								(
									(await _zeitbild.frontend_web.backend.calendar_list())
									.filter(
										(entry) => (
											(entry.access_level === _zeitbild.frontend_web.type.enum_access_level.edit)
											||
											(entry.access_level === _zeitbild.frontend_web.type.enum_access_level.admin)
										)
									)
									.map(
										(entry) => ({
											"value": entry.id.toFixed(0),
											"label": entry.name,
										})
									)
								)
							),
							"label": lib_plankton.translate.get("calendar.calendar")
						},
						{
							"name": "name",
							"input": new lib_plankton.zoo_input.class_input_text(
							),
							"label": lib_plankton.translate.get("event.name")
						},
						{
							"name": "begin",
							"input": new lib_plankton.zoo_input.class_input_datetime(
								{
									"label_timezone_shift": lib_plankton.translate.get("common.timezone_shift"),
									"label_date": lib_plankton.translate.get("common.date"),
									"label_time": lib_plankton.translate.get("common.time"),
								}
							),
							"label": lib_plankton.translate.get("event.begin")
						},
						{
							"name": "end",
							"input": new lib_plankton.zoo_input.class_input_soft<lib_plankton.pit.type_datetime>(
								new lib_plankton.zoo_input.class_input_datetime(
									{
										"label_timezone_shift": lib_plankton.translate.get("common.timezone_shift"),
										"label_date": lib_plankton.translate.get("common.date"),
										"label_time": lib_plankton.translate.get("common.time"),
									}
								)
							),
							"label": lib_plankton.translate.get("event.end")
						},
						{
							"name": "location",
							"input": new lib_plankton.zoo_input.class_input_soft<string>(
								new lib_plankton.zoo_input.class_input_text(
								)
							),
							"label": lib_plankton.translate.get("event.location")
						},
						{
							"name": "description",
							"input": new lib_plankton.zoo_input.class_input_soft<string>(
								new lib_plankton.zoo_input.class_input_textarea(
								)
							),
							"label": lib_plankton.translate.get("event.description")
						},
					]
				),
				[
					{
						"label": lib_plankton.translate.get("page.event_add.actions.do"),
						"target": "submit",
						"procedure": async (get_value, get_representation) => {
							const value : any = await get_value();
							try {
								await _zeitbild.frontend_web.backend.calendar_event_add(
									value.calendar_id,
									value.event_object
								);
								lib_plankton.zoo_page.set(
									{
										"name": "overview",
										"parameters": {}
									}
								);
							}
							catch (error) {
								// do nothing
								/*
								lib_plankton.zoo_page.set(
									{
										"name": "event_add",
										"parameters": {
										}
									}
								);
								 */
							}
						}
					},
				]
			);
			await form.setup(document.querySelector("#event_add_form"));
			await form.input_write(
				{
					"calendar_id": (calendar_id ?? 0),
					"event_object": {
						"name": "",
						"begin": {
							"timezone_shift": 0,
							"date": date,
							"time": null
						},
						"end": null,
						"location": null,
						"description": null,
					}
				}
			);
			return Promise.resolve<void>(undefined);
		}
	);
	
}
