namespace _dali.pages.overview
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"overview",
		async (parameters, target_element) => {
			// params
			const view_mode : _dali.type.enum_view_mode = view_mode_determine(parameters["mode"] ?? "auto");
			
			/**
			 * @todo ordentlich machen (nicht nur week und list)
			 */
			const set_view_mode = (view_mode) => {
				const compact : boolean = (view_mode !== _dali.type.enum_view_mode.week);
				target_element.querySelector("#overview").classList.toggle("overview-compact", compact);
			};
			
			// exec
			target_element.innerHTML = await _dali.helpers.template_coin(
				"overview",
				"default",
				{
				}
			);
			
			// mode switcher
			{
				const widget_mode_switcher : _dali.class_widget = new _dali.widgets.mode_switcher.class_widget_mode_switcher(
					[
						{
							"mode": _dali.type.enum_view_mode.week,
							"label": lib_plankton.translate.get("page.overview.mode.week"),
						},
						{
							"mode": _dali.type.enum_view_mode.list,
							"label": lib_plankton.translate.get("page.overview.mode.list"),
						},
					],
					{
						"initial_selection": view_mode,
						"action_change": (view_mode) => {
							lib_plankton.zoo_page.set(
								{
									"name": "overview",
									"parameters": {
										"mode": _dali.view_mode_encode(view_mode),
									}
								}
							);
							set_view_mode(view_mode);
						}
					}
				);
				set_view_mode(view_mode);
				await widget_mode_switcher.load(target_element.querySelector("#overview-mode"));
			}
			
			let widget_weekview : _dali.widgets.weekview.class_widget_weekview;
			let widget_listview : _dali.widgets.listview.class_widget_listview;
			// hint
			{
				if (! await _dali.backend.is_logged_in())
				{
					target_element.querySelector("#overview-hint").textContent = lib_plankton.translate.get("page.overview.login_hint");
				}
				else
				{
					// do nothing
				}
			}
			// sources
			{
				const data : Array<
					{
						id : _dali.type.calendar_id;
						name : string;
						access_level : _dali.type.enum_access_level;
					}
				> = await _dali.backend.calendar_list(
				);
				const widget_sources = new _dali.widgets.sources.class_widget_sources(
					data,
					{
						"action_open": (entry) => {
							switch (entry.access_level)
							{
								case _dali.type.enum_access_level.none:
								{
									throw (new Error("this event should not be visible"));
									break;
								}
								case _dali.type.enum_access_level.edit:
								case _dali.type.enum_access_level.view:
								{
									lib_plankton.zoo_page.set(
										{
											"name": "calendar_edit",
											"parameters": {
												"read_only": "yes",
												"calendar_id": entry.id,
											}
										}
									);
									break;
								}
								case _dali.type.enum_access_level.admin:
								{
									lib_plankton.zoo_page.set(
										{
											"name": "calendar_edit",
											"parameters": {
												"read_only": "no",
												"calendar_id": entry.id,
											}
										}
									);
									break;
								}			
							}
						},
						"action_toggle_visibility": (entry) => {
							widget_weekview.toggle_visibility(entry.id);
							widget_listview.toggle_visibility(entry.id);
						},
					}
				);
				await widget_sources.load(target_element.querySelector("#overview-pane-left"));
			}
			// events
			{
				const get_entries = (from_pit, to_pit, calendar_ids) => _dali.backend.events(
					from_pit,
					to_pit,
					{
						"calendar_ids": calendar_ids,
					}
				);
				const action_select_event = (calendar_id, access_level, event_id) => {
					switch (access_level)
					{
						case _dali.type.enum_access_level.none:
						{
							throw (new Error("this event should not be visible"));
							break;
						}
						case _dali.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 _dali.type.enum_access_level.edit:
						case _dali.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;
						}
					}
				};
				// listview
				{
					widget_listview = (
						new _dali.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,
											}
										}
									);
								},
							}
						)
					);
					await widget_listview.load(target_element.querySelector("#overview-pane-right-listview"));
				}
				// weekview
				{
					widget_weekview = (
						new _dali.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_weekview.load(target_element.querySelector("#overview-pane-right-weekview"));
				}
			}
			return Promise.resolve<void>(undefined);
		},
	);

}
