/*
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
{
	
	/**
	 */
	export async function main(
	)
	: Promise<void>
	{
		// conf
		await _dali.conf.init(
			"conf.json"
		);
		
		// init:logger
		lib_plankton.log.set_main_logger(
			[
				{
					"kind": "minlevel",
					"data": {
						"core": {
							"kind": "console",
							"data": {
							}
						},
						"threshold": "info"
					}
				},
			]
		);
		// init:overlay
		{
			await _dali.overlay.initialize();
			_dali.helpers.loading(true);
		}
		// init:localization
		{
			const order : Array<string> = ["deu", "eng"];
			await lib_plankton.translate.initialize(
				{
					"verbosity": 1,
					"packages": await Promise.all(
						order.map(
							code => (
								Promise.resolve(code)
								.then<string>(code => Promise.resolve(lib_plankton.string.coin("data/localization/{{code}}.loc.json", {"code": code})))
								.then<string>(lib_plankton.file.read)
								.then<any>(content => Promise.resolve(JSON.parse(content)))
							)
						)
					),
					"order": order,
					"autopromote": false,
				}
			);
		}
		// init:backend
		await _dali.backend.initialize(
			_dali.conf.get()["backend"]
		);
		// init:general cache
		lib_plankton.cache.init(_dali.general_cache);
		// init:model
		await _dali.model.initialize(
		);
		// init:page
		lib_plankton.zoo_page.init(
			document.querySelector("main"),
			{
				"fallback": {
					"name": "overview",
					"parameters": {}
				},
			}
		);
		// init:menu
		{
			const widget_menu : _dali.widgets.class_widget_menu = new _dali.widgets.class_widget_menu(
				[
					{
						"label": lib_plankton.translate.get("common.login"),
						"groups": ["logged_out"],
						"action": async () => {
							_dali.helpers.loading(true);
							const widget : lib_plankton.zoo_widget.interface_widget = new _dali.widgets.class_widget_login(
								{
									"action_cancel": () => {
										_dali.overlay.clear();
										_dali.overlay.toggle({"mode": false});
									},
									"action_success": async () => {
										_dali.helpers.loading(true);
										const status = await _dali.backend.status();
										_dali.notify_login(status.name);
										// _dali.overlay.clear();
										_dali.helpers.loading(false);
									},
								}
							);
							await widget.load(_dali.overlay.get_content_element());
							// _dali.helpers.loading(false);
							_dali.overlay.toggle({"mode": true});
						},
					},
					{
						"label": lib_plankton.translate.get("widget.caldav.title"),
						"groups": ["logged_in"],
						"action": async () => {
							_dali.helpers.loading(true);
							const widget : lib_plankton.zoo_widget.interface_widget = new _dali.widgets.class_widget_caldav();
							await widget.load(_dali.overlay.get_content_element());
							// _dali.helpers.loading(false);
							_dali.overlay.toggle({"mode": true});
						},
					},
					{
						"label": lib_plankton.translate.get("common.logout"),
						"groups": ["logged_in"],
						"action": async () => {
							_dali.helpers.loading(true);
							await _dali.logout();
							_dali.helpers.loading(false);
						},
					},
				]
			);
			await widget_menu.load(document.querySelector("header"));
			_dali.listen_login(
				async (name) => {
					widget_menu.set_groups(["logged_in"]);
					widget_menu.set_label(name);
				}
			);
			_dali.listen_logout(
				async () => {
					widget_menu.set_groups(["logged_out"]);
					widget_menu.set_label(null);
				}
			);
		}
		
		// process actions
		{
			let url_search_params : URLSearchParams = new URLSearchParams(window.location.search);
			const action : (null | string) = url_search_params.get("action");
			switch (action)
			{
				case "oidc_finish":
				{
					try
					{
						await _dali.oidc_finish(url_search_params.get("session_key"));
					}
					catch (error_)
					{
						// do nothing
					}
					document.location = "/";
					break;
				}
				default:
				case null:
				{
					// do nothing
					break;
				}
			}
		}
		
		// process status
		{
			const status = await _dali.backend.status();
			lib_plankton.log.info(
				"dali.status",
				status
			);
			if (status.logged_in)
			{
				await _dali.notify_login(status.name);
			}
			else
			{
				await _dali.notify_logout();
			}
		}
		
		// load overview
		{
			const widget : lib_plankton.zoo_widget.interface_widget = (
				new _dali.widgets.class_widget_overview(
				)
			);
			await widget.load(document.querySelector("main"));
		}
		_dali.helpers.loading(false);
		
		return Promise.resolve<void>(undefined);
	}
	
}

