namespace _dali.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"event_edit",
		async (parameters, target_element) => {
			const read_only : boolean = ((parameters["read_only"] ?? "yes") === "yes");
			const calendar_id : int = parseInt(parameters["calendar_id"]);
			const event_id : int = parseInt(parameters["event_id"]);
			target_element.innerHTML = "";
			target_element.innerHTML = await _dali.helpers.template_coin(
				"event_edit",
				"default",
				{
					"label": (
						read_only
						?
						lib_plankton.translate.get("page.event_edit.title.read_only")
						:
						lib_plankton.translate.get("page.event_edit.title.regular")
					)
				}
			);
			const form : lib_plankton.zoo_form.class_form<
				_dali.type.event_object,
				{
					name : string;
					begin : lib_plankton.pit.type_datetime;
					end : (null | lib_plankton.pit.type_datetime);
					location : (null | string);
					link : (null | string);
					description : (null | string);
				}
			> = new lib_plankton.zoo_form.class_form<
				_dali.type.event_object,
				{
					name : string;
					begin : lib_plankton.pit.type_datetime;
					end : (null | lib_plankton.pit.type_datetime);
					location : (null | string);
					link : (null | string);
					description : (null | string);
				}
			>(
				(value) => ({
					"name": value.name,
					"begin": value.begin,
					"end": value.end,
					"location": value.location,
					"link": value.link,
					"description": value.description,
				}),
				(representation) => ({
					"name": representation.name,
					"begin": representation.begin,
					"end": representation.end,
					"location": representation.location,
					"link": representation.link,
					"description": representation.description,
				}),
				new lib_plankton.zoo_input.class_input_group<any>(
					[
						{
							"name": "name",
							"input": new lib_plankton.zoo_input.class_input_text(
							),
							"label": lib_plankton.translate.get("event.name")
						},
						{
							"name": "begin",
							"input": _dali.helpers.datetime_input(),
							"label": lib_plankton.translate.get("event.begin")
						},
						{
							"name": "end",
							"input": new lib_plankton.zoo_input.class_input_soft<lib_plankton.pit.type_datetime>(
								_dali.helpers.datetime_input()
							),
							"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": "link",
							"input": new lib_plankton.zoo_input.class_input_soft<string>(
								new lib_plankton.zoo_input.class_input_text(
								)
							),
							"label": lib_plankton.translate.get("event.link")
						},
						{
							"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")
						},
					]
				),
				(
					read_only
					?
					[
					]
					:
					[
						{
							"label": lib_plankton.translate.get("page.event_edit.actions.change"),
							"target": "submit",
							"procedure": async (get_value, get_representation) => {
								const value : any = await get_value();
								try {
									await _dali.backend.calendar_event_change(
										calendar_id,
										event_id,
										value
									);
									lib_plankton.zoo_page.set(
										{
											"name": "overview",
											"parameters": {}
										}
									);
								}
								catch (error) {
									lib_plankton.log.warning("page_event_edit_error", {"error": String(error)});
									// do nothing
									/*
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
											}
										}
									);
									 */
								}
							}
						},
						{
							"label": lib_plankton.translate.get("page.event_edit.actions.remove"),
							"target": "submit",
							"procedure": async (get_value, get_representation) => {
								try {
									await _dali.backend.calendar_event_remove(
										calendar_id,
										event_id
									);
									lib_plankton.zoo_page.set(
										{
											"name": "overview",
											"parameters": {}
										}
									);
								}
								catch (error) {
									lib_plankton.log.warning("page_event_edit_error", {"error": String(error)});
									// do nothing
									/*
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
											}
										}
									);
									 */
								}
							}
						},
					]
				)
			);
			await form.setup(document.querySelector("#event_edit_form"));
			const event_object : _dali.type.event_object = await _dali.backend.calendar_event_get(
				calendar_id,
				event_id
			);
			await form.input_write(
				event_object
			);
			return Promise.resolve<void>(undefined);
		}
	);
	
}
