namespace _zeitbild.frontend_web.pages.overview
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"overview",
		async (parameters, target_element) => {
			// params
			const compact : boolean = (
				(
					parameters["compact"]
					??
					(
						(window.innerWidth >= 800)
						?
						"no"
						:
						"yes"
					)
				)
				===
				"yes"
			);
			
			// exec
			target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
				"overview",
				"default",
				{
				}
			);
			target_element.querySelector("#overview").classList.toggle("compact", compact);
			// sources
			{
				const data : Array<
					{
						id : _zeitbild.frontend_web.type.calendar_id;
						name : string;
						access_level : _zeitbild.frontend_web.type.enum_access_level;
					}
				> = await _zeitbild.frontend_web.backend.calendar_list(
				);
				const widget_sources = new _zeitbild.frontend_web.widgets.sources.class_widget_sources(
					data,
					{
						"action_select": (entry) => {
							switch (entry.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.edit:
								case _zeitbild.frontend_web.type.enum_access_level.view: {
									lib_plankton.zoo_page.set(
										{
											"name": "calendar_edit",
											"parameters": {
												"read_only": "yes",
												"calendar_id": entry.id,
											}
										}
									);
									break;
								}
								case _zeitbild.frontend_web.type.enum_access_level.admin: {
									lib_plankton.zoo_page.set(
										{
											"name": "calendar_edit",
											"parameters": {
												"read_only": "no",
												"calendar_id": entry.id,
											}
										}
									);
									break;
								}			
							}
						},
					}
				);
				if (compact) {
					// do nothing
				}
				else {
					await widget_sources.load(target_element.querySelector("#overview-pane-left"));
				}
			}
			// view
			{
				const get_entries = (from_pit, to_pit, calendar_ids) => _zeitbild.frontend_web.backend.events(
					from_pit,
					to_pit,
					{
						"calendar_ids": calendar_ids,
					}
				);
				const action_select_event = (calendar_id, access_level, event_id) => {
					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;
						}
					}
				};
				const widget = (
					compact
					?
					(
						new _zeitbild.frontend_web.widgets.listview.class_widget_listview(
							get_entries,
							{
								"action_select_event": action_select_event,
								"action_add": () => {
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
												"calendar_id": null,
												"year": null,
												"month": null,
												"day": null,
											}
										}
									);
								},
							}
						)
					)
					:
					(
						new _zeitbild.frontend_web.widgets.weekview.class_widget_weekview(
							get_entries,
							{
								"action_select_event": action_select_event,
								"action_select_day": (date) => {
									lib_plankton.zoo_page.set(
										{
											"name": "event_add",
											"parameters": {
												"calendar_id": null,
												"year": date.year,
												"month": date.month,
												"day": date.day,
											}
										}
									);
								},
							}
						)
					)
				);
				await widget.load(target_element.querySelector("#overview-pane-right"));
			}
			return Promise.resolve<void>(undefined);
		},
	);

}
