namespace _zeitbild.frontend_web.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"events",
		async (parameters, target_element) => {
			target_element.innerHTML = "";
			const content : string = await _zeitbild.frontend_web.view.calendar_view_table_html(
				{
					"calendar_ids": null,
					// TODO
					"from": {
						"year": 2024,
						"week": 37
					},
					// TODO
					"to": {
						"year": 2024,
						"week": 43
					},
					"timezone_shift": /*conf.timezone_shift*/0,
				}
			);
			target_element.innerHTML = content;
			target_element.querySelectorAll(".calendar-cell-regular").forEach(
				(element) => {
					element.addEventListener(
						"click",
						(event) => {
							if (! (element === event.target)) {
								// do nothing
							}
							else {
								const rel : string = element.getAttribute("rel");
								const parts : Array<string> = rel.split("-");
								const year : int = parseInt(parts[0]);
								const month : int = parseInt(parts[1]);
								const day : int = parseInt(parts[2]);
								lib_plankton.zoo_page.set(
									{
										"name": "event_add",
										"parameters": {
											"calendar_id": null,
											"year": year,
											"month": month,
											"day": day,
										}
									}
								);
							}
						}
					);
				}
			);
			target_element.querySelectorAll(".calendar-event_entry").forEach(
				(element) => {
					element.addEventListener(
						"click",
						() => {
							const rel : string = element.getAttribute("rel");
							const parts : Array<string> = rel.split("/");
							const calendar_id : _zeitbild.frontend_web.type.calendar_id = parseInt(parts[0]);
							const event_id : (null | _zeitbild.frontend_web.type.local_resource_event_id) = (
								(parts[1] === "-")
								?
								null
								:
								parseInt(parts[1])
							);
							const access_level : _zeitbild.frontend_web.type.enum_access_level = (() => {
								switch (parts[2]) {
									case "none": return _zeitbild.frontend_web.type.enum_access_level.none;
									case "view": return _zeitbild.frontend_web.type.enum_access_level.view;
									case "edit": return _zeitbild.frontend_web.type.enum_access_level.edit;
									case "admin": return _zeitbild.frontend_web.type.enum_access_level.admin;
								}
							}) ();
							switch (access_level) {
								case _zeitbild.frontend_web.type.enum_access_level.none: {
									throw (new Error("this event should not be visible"));
									break;
								}
								case _zeitbild.frontend_web.type.enum_access_level.view: {
									lib_plankton.zoo_page.set(
										{
											"name": "event_edit",
											"parameters": {
												"read_only": "yes",
												"calendar_id": calendar_id,
												"event_id": event_id,
											}
										}
									);
									break;
								}
								case _zeitbild.frontend_web.type.enum_access_level.edit:
								case _zeitbild.frontend_web.type.enum_access_level.admin: {
									lib_plankton.zoo_page.set(
										{
											"name": "event_edit",
											"parameters": {
												"read_only": "no",
												"calendar_id": calendar_id,
												"event_id": event_id,
											}
										}
									);
									break;
								}
							}
						}
					);
				}
			);
			return Promise.resolve<void>(undefined);
		},
	);

}
