namespace _dali.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"caldav",
		async (parameters, target_element) => {
			target_element.innerHTML = "";
			const conf = await _dali.backend.user_dav_conf();
			target_element.innerHTML = await _dali.helpers.template_coin(
				"caldav",
				"main",
				{
					"label": lib_plankton.translate.get("page.caldav.title"),
					"content": (
						(conf === null)
						?
						await _dali.helpers.template_coin(
							"caldav",
							"unavailable",
							{
								"text": lib_plankton.translate.get("page.caldav.unavailable"),
							}
						)
						:
						await _dali.helpers.template_coin(
							"caldav",
							"available",
							{
								"conf_title": lib_plankton.translate.get("page.caldav.conf.title"),
								"conf_content": (
									(conf.password === null)
									?
									await _dali.helpers.template_coin(
										"caldav",
										"conf-token_unset",
										{
											"text": lib_plankton.translate.get("page.caldav.conf.token_unset")
										}
									)
									:
									await _dali.helpers.template_coin(
										"caldav",
										"conf-token_set",
										{
											"address_label": lib_plankton.translate.get("page.caldav.conf.address"),
											"address_value": conf.address,
											"username_label": lib_plankton.translate.get("page.caldav.conf.username"),
											"username_value": conf.username,
											"password_label": lib_plankton.translate.get("page.caldav.conf.password"),
											"password_value": conf.password,
											"setup_hints_label": lib_plankton.translate.get("page.caldav.conf.setup_hints"),
											"setup_hint_entries": (
												await lib_plankton.call.promise_condense<string, unknown>(
													conf.setup_hints
													.map(
														entry => () => _dali.helpers.template_coin(
															"caldav",
															"conf-setup_hint_entry",
															{
																"text": entry.label,
																"href": entry.link,
																"remark": (
																	(entry.remark === null)
																	?
																	""
																	:
																	lib_plankton.string.coin(
																		" — {{content}}",
																		{
																			"content": entry.remark,
																		}
																	)
																)
															}
														)
													)
												)
											).join("")
										}
									)
								),
								"set_token_title": lib_plankton.translate.get("page.caldav.set_token.title"),
								"set_token_action": (
									(conf.password === null)
									?
									lib_plankton.translate.get("page.caldav.set_token.action.set")
									:
									lib_plankton.translate.get("page.caldav.set_token.action.overwrite")
								),
							}
						)
					)
				}
			);
			
			/**
			 * logic: set token
			 */
			{
				if (conf !== null)
				{
					document.querySelector("#caldav-set_token > button").addEventListener(
						"click",
						async () => {
							await _dali.backend.user_dav_token();
							lib_plankton.zoo_page.reload();
						}
					);
				}
			}
			
			return Promise.resolve<void>(undefined);
		}
	);

}

