/*
This file is part of »dali«.

Copyright 2025 'kcf' <fenris@folksprak.org>

»dali« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

»dali« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with »dali«. If not, see <http://www.gnu.org/licenses/>.
 */


namespace _dali.widgets
{
	
	/**
	 */
	export class class_widget_caldav
	{
		
		/**
		 */
		public constructor(
		)
		{
		}
		
		
		/**
		 */
		public async load(
			target_element : HTMLElement
		)
		: Promise<void>
		{
			target_element.innerHTML = "";
			const conf = await _dali.backend.user_dav_conf();
			target_element.innerHTML = await _dali.helpers.template_coin(
				"widget-caldav",
				"main",
				{
					"title": lib_plankton.translate.get("widget.caldav.title"),
					"content": (
						(conf === null)
						?
						await _dali.helpers.template_coin(
							"widget-caldav",
							"unavailable",
							{
								"text": lib_plankton.translate.get("widget.caldav.unavailable"),
							}
						)
						:
						await _dali.helpers.template_coin(
							"widget-caldav",
							"available",
							{
								"conf_title": lib_plankton.translate.get("widget.caldav.conf.title"),
								"conf_content": (
									(conf.password === null)
									?
									await _dali.helpers.template_coin(
										"widget-caldav",
										"conf-token_unset",
										{
											"text": lib_plankton.translate.get("widget.caldav.conf.token_unset")
										}
									)
									:
									await _dali.helpers.template_coin(
										"widget-caldav",
										"conf-token_set",
										{
											"address_label": lib_plankton.translate.get("widget.caldav.conf.address"),
											"address_value": conf.address,
											"username_label": lib_plankton.translate.get("widget.caldav.conf.username"),
											"username_value": conf.username,
											"password_label": lib_plankton.translate.get("widget.caldav.conf.password"),
											"password_value": conf.password,
											"setup_hints_label": lib_plankton.translate.get("widget.caldav.conf.setup_hints"),
											"setup_hint_entries": (
												await lib_plankton.call.promise_condense<string, unknown>(
													conf.setup_hints
													.map(
														entry => () => _dali.helpers.template_coin(
															"widget-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("widget.caldav.set_token.title"),
								"set_token_action": (
									(conf.password === null)
									?
									lib_plankton.translate.get("widget.caldav.set_token.action.set")
									:
									lib_plankton.translate.get("widget.caldav.set_token.action.overwrite")
								),
							}
						)
					),
					"close": lib_plankton.translate.get("common.close"),
				}
			);
			// logic
			{
				// set token
				{
					if (conf === null)
					{
						// do nothing
					}
					else
					{
						target_element.querySelector(".widget-caldav-set_token > button").addEventListener(
							"click",
							async () => {
								await _dali.backend.user_dav_token();
								await this.load(target_element);
							}
						);
					}
				}
				// close
				{
					target_element.querySelector(".widget-caldav-close").addEventListener(
						"click",
						() => {
							_dali.overlay.toggle({"mode": false});
						}
					);
				}
			}
			// init
			{
				(target_element.querySelector(".widget-caldav-close") as HTMLElement).focus();
			}
			
			return Promise.resolve<void>(undefined);
		}
		
	}
	
}

