/*
This file is part of »zeitbild«.

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

»zeitbild« 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.

»zeitbild« 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 »zeitbild«. If not, see <http://www.gnu.org/licenses/>.
 */


/**
 */
namespace _zeitbild.helpers
{
	
	/**
	 * @todo timezone
	 */
	function icalendar_datetime_to_own_datetime(
		ical_datetime : lib_plankton.ical.type_datetime
	) : lib_plankton.pit.type_datetime
	{
		return {
			"timezone_shift": 0,
			"date": {
				"year": ical_datetime.date.year,
				"month": ical_datetime.date.month,
				"day": ical_datetime.date.day,
			},
			"time": (
				(ical_datetime.time === null)
				?
				null
				:
				{
					"hour": ical_datetime.time.hour,
					"minute": ical_datetime.time.minute,
					"second": ical_datetime.time.second,
				}
			)
		};
	}
	
	
	/**
	 * @todo timezone
	 */
	export function icalendar_dt_to_own_datetime(
		ical_dt: lib_plankton.ical.type_dt
	) : lib_plankton.pit.type_datetime
	{
		return {
			"timezone_shift": 0,
			"date": ical_dt.value.date,
			"time": (
				(ical_dt.value.time === null)
				?
				null
				:
				{
					"hour": ical_dt.value.time.hour,
					"minute": ical_dt.value.time.minute,
					"second": ical_dt.value.time.second,
				}
			)
		};
	}
	
	
	/**
	 */
	export function icalendar_dt_from_own_datetime(
		datetime : lib_plankton.pit.type_datetime
	) : lib_plankton.ical.type_dt
	{
		const datetime_normalized : lib_plankton.pit.type_datetime = lib_plankton.pit.to_datetime(
			lib_plankton.pit.from_datetime(datetime),
			{
				"timezone_shift": 0,
			}
		);
		return {
			"tzid": "UTC",
			"value": {
				"date": {
					"year": datetime_normalized.date.year,
					"month": datetime_normalized.date.month,
					"day": datetime_normalized.date.day,
				},
				"time": (
					(datetime_normalized.time === null)
					?
					null
					:
					{
						"utc": true,
						"hour": datetime_normalized.time.hour,
						"minute": datetime_normalized.time.minute,
						"second": datetime_normalized.time.second,
					}
				)
			},
		};
	}
	
	
	/**
	 */
	export function icalendar_vevent_from_own_event(
		event_extended : _zeitbild.type_event_extended,
		index : int,
		{
			"stamp": stamp = "adhoc",
		}
		:
		{
			stamp ?: string;
		}
		=
		{
		}
	) : lib_plankton.ical.type_vevent
	{
		return {
			"uid": lib_plankton.string.coin(
				"zeitbild_{{stamp}}_{{id}}",
				{
					"stamp": stamp,
					// "id": event_extended.event_id.toFixed(0),
					"id": index.toFixed(0),
				}
			),
			"dtstamp": icalendar_dt_from_own_datetime(event_extended.event_object.begin).value,
			"dtstart": icalendar_dt_from_own_datetime(event_extended.event_object.begin),
			"dtend": (
				(event_extended.event_object.end === null)
				?
				undefined
				:
				icalendar_dt_from_own_datetime(event_extended.event_object.end)
			),
			"location": (event_extended.event_object.location ?? undefined),
			"summary": event_extended.event_object.name,
			"url": (event_extended.event_object.link ?? undefined),
			"description": (event_extended.event_object.description ?? undefined),
			/**
			 * @todo transform name
			 */
			"categories": [event_extended.calendar_name],
		};
	}
	
	
	/**
	 * @todo assign better uids
	 */
	export function icalendar_vcalendar_from_own_event_list(
		events_extended : Array<_zeitbild.type_event_extended>
	) : lib_plankton.ical.type_vcalendar
	{
		const pit_now : lib_plankton.pit.type_pit = lib_plankton.pit.now();
		const datetime_now : lib_plankton.pit.type_datetime = lib_plankton.pit.to_datetime(pit_now);
		const stamp : string = lib_plankton.string.coin(
			"{{year}}{{month}}{{day}}",
			{
				"year": datetime_now.date.year.toFixed(0).padStart(4, "0"),
				"month": datetime_now.date.month.toFixed(0).padStart(2, "0"),
				"day": datetime_now.date.day.toFixed(0).padStart(2, "0"),
			}
		);
		return {
			"version": "2.0",
			"prodid": "",
			"vevents": (
				events_extended
				.map<lib_plankton.ical.type_vevent>(
					(event_extended, index) => icalendar_vevent_from_own_event(
						event_extended,
						index,
						{
							"stamp": stamp,
						}
					)
				)
			),
			"method": "PUBLISH",
			"vtimezone": {
				"tzid": "Europe/Berlin",
			},
		};
	}
	
	
	/**
	 */
	export async function template_coin(
		name : string,
		data : Record<string, string>
	) : Promise<string>
	{
		const content : string = await lib_plankton.cache.get<string>(
			_zeitbild.cache_templates,
			name,
			null,
			() => (
				lib_plankton.file.read(
					lib_plankton.string.coin(
						"templates/{{name}}.html.tpl",
						{
							"name": name,
						}
					)
				)
				.then(
					x => Promise.resolve<string>(x.toString())
				)
			)
		);
		return Promise.resolve<string>(
			lib_plankton.string.coin(
				content,
				data
			)
		);
	}
	
	
	/**
	 * @todo outsource
	 */
	export async function promise_row<type_result>(
		members : Array<
			() => Promise<type_result>
		>
	) : Promise<
		Array<
			type_result
		>
	>
	{
		let results : Array<type_result> = [];
		for await (const member of members) {
			results.push(await member());
		}
		return Promise.resolve<Array<type_result>>(results);
	}
	
}
