/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß

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

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */


/**
 */
async function update_nav(
	options : {
		mode ?: string;
	} = {}
) : Promise<void>
{
	options = Object.assign(
		{
			"mode": (
				(await _espe.backend.logged_in())
				? "logged_in"
				: "logged_out"
			),
		},
		options
	);
	let dom_body = document.querySelector("body");
	if (options.mode === null) {
		dom_body.removeAttribute("rel");
	}
	else {
		dom_body.setAttribute("rel", options.mode);
	}
}


/**
 */
function setup_nav(
) : void
{
	const entries : Array<
		{
			location : lib_plankton.zoo_page.type_location;
			label : string;
			classes : Array<string>;
		}
	> = [
		{
			"location": {"name": "login", "parameters": {}},
			"label": lib_plankton.translate.get("page.login.title"),
			"classes": ["logged_out"],
		},
		{
			"location": {"name": "group_list", "parameters": {}},
			"label": lib_plankton.translate.get("page.group_list.title"),
			"classes": ["logged_in"],
		},
		{
			"location": {"name": "member_list", "parameters": {}},
			"label": lib_plankton.translate.get("page.member_list.title"),
			"classes": ["logged_in"],
		},
		{
			"location": {"name": "invite_list", "parameters": {}},
			"label": lib_plankton.translate.get("page.invite_list.title"),
			"classes": ["logged_in"],
		},
		{
			"location": {"name": "logout", "parameters": {}},
			"label": lib_plankton.translate.get("page.logout.title"),
			"classes": ["logged_in"],
		},
	];
	
	let dom_ul : HTMLElement = document.querySelector("nav > ul");
	entries.forEach(
		entry => {
			let dom_li : HTMLElement = document.createElement("li");
			entry.classes.forEach(class_ => {dom_li.classList.add(class_);});
			{
				let dom_a : HTMLElement = document.createElement("a");
				dom_a.textContent = entry.label;
				dom_a.setAttribute("href", lib_plankton.zoo_page.encode(entry.location));
				dom_li.appendChild(dom_a);
			}
			dom_ul.appendChild(dom_li);
		}
	);
	update_nav();
}


/**
 */
async function main(
) : Promise<void>
{
	await _espe.conf.load();
	
	await _espe.backend.init();
	
	await lib_plankton.translate.initialize(
		{
			"verbosity": 1,
			"packages": [
				JSON.parse(await lib_plankton.file.read("data/localization/deu.loc.json")),
				JSON.parse(await lib_plankton.file.read("data/localization/eng.loc.json")),
			],
			"order": ["deu", "eng"],
			"autopromote": false,
		}
	);
	
	await lib_plankton.zoo_page.init(
		document.querySelector("main"),
		{
			"fallback": {"name": "index", "parameters": {}},
		}
	);
	
	// set title
	document.querySelector("header > h1").textContent = _espe.conf.get().settings.title;
	document.querySelector("title").textContent = _espe.conf.get().settings.title;
	
	setup_nav();
	
	lib_plankton.zoo_page.start();
}
