/*
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
{
	
	/**
	 */
	type type_get_entries = (
		(
			from_pit : lib_plankton.pit.type_pit,
			to_pit : lib_plankton.pit.type_pit,
			calendar_ids : Array<_dali.type_calendar_id>
		)
		=>
		Promise<Array<_dali.type_event_object_extended>>
	);

	
	/**
	 */
	type type_create_event = (
		(
			stuff ?: {
				date ?: lib_plankton.pit.type_date;
			}
		)
		=>
		Promise<void>
	);
	
	
	/**
	 */
	type type_edit_event = (
		(
			event_key : _dali.type_event_key
		)
		=>
		Promise<void>
	);
	
	
	/**
	 */
	export class class_widget_multiview
		implements lib_plankton.zoo_widget.interface_widget
	{
		
		/**
		 */
		private initial_view_mode : _dali.enum_view_mode;
		
		
		/**
		 */
		private weekview_initial_vertical : boolean;
		
		
		/**
		 */
		private get_entries : type_get_entries;
		
		
		/**
		 */
		private action_create_event : type_create_event;
		
		
		/**
		 */
		private action_edit_event : type_edit_event;
		
		
		/**
		 */
		private dom_context : (null | HTMLElement);
		
		
		/**
		 */
		private widget_mode_switcher : _dali.widgets.class_widget_mode_switcher;
		
		
		/**
		 */
		private widget_weekview : _dali.widgets.class_widget_weekview;
		
		
		/**
		 */
		private widget_listview : _dali.widgets.class_widget_listview;
		
		
		/**
		 */
		public constructor(
			get_entries : type_get_entries,
			{
				"initial_view_mode": initial_view_mode = _dali.enum_view_mode.week,
				"weekview_initial_vertical": weekview_initial_vertical = false,
				"action_create_event": action_create_event = ((stuff) => Promise.resolve<void>(undefined)),
				"action_edit_event": action_edit_event = ((event_key) => Promise.resolve<void>(undefined)),
			}
			:
			{
				initial_view_mode ?: _dali.enum_view_mode;
				weekview_initial_vertical ?: boolean;
				action_create_event ?: type_create_event;
				action_edit_event ?: type_edit_event;
			}
			=
			{
			}
		)
		{
			this.get_entries = get_entries;
			this.initial_view_mode = initial_view_mode;
			this.weekview_initial_vertical = weekview_initial_vertical;
			this.action_create_event = action_create_event;
			this.action_edit_event = action_edit_event;
			this.dom_context = null;
		}
		
		
		/**
		 */
		public toggle_calendar_visibilty(
			calendar_id : _dali.type_calendar_id,
			{
				"mode": mode = null,
			}
			:
			{
				mode ?: (null | boolean);
			}
			=
			{
			}
		)
		: void
		{
			this.widget_weekview.toggle_visibility(calendar_id, {"mode": mode});
			this.widget_listview.toggle_visibility(calendar_id, {"mode": mode});
		}
		
		
		/**
		 */
		private set_view_mode
		(
			view_mode : _dali.enum_view_mode
		)
		: void
		{
			this.dom_context.setAttribute("rel", _dali.view_mode_encode(view_mode));
		}
		
		
		/**
		 */
		public async update_entries(
		)
		:
		Promise<void>
		{
			await Promise.all(
				[
					this.widget_weekview.update_entries(),
					this.widget_listview.update_entries(),
				]
			);
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : Element
		)
		: Promise<void>
		{
			this.dom_context = (target_element as HTMLElement);
			
			this.dom_context.classList.add("widget-multiview");
			
			// mode switcher
			{
				this.widget_mode_switcher = (
					new _dali.widgets.class_widget_mode_switcher(
						[
							{
								"mode": _dali.enum_view_mode.week,
								/**
								 * @todo as dependency
								 */
								"label": lib_plankton.translate.get("widget.overview.mode.week"),
							},
							{
								"mode": _dali.enum_view_mode.list,
								/**
								 * @todo as dependency
								 */
								"label": lib_plankton.translate.get("widget.overview.mode.list"),
							},
						],
						{
							"initial_selection": this.initial_view_mode,
							"action_change": (view_mode) => {
								this.set_view_mode(view_mode);
							}
						}
					)
				);
				let dom_wrapper = document.createElement("div");
				dom_wrapper.classList.add("widget-multiview-mode_switcher");
				await this.widget_mode_switcher.load(dom_wrapper);
				this.dom_context.appendChild(dom_wrapper);
			}
			// weekview
			{
				this.widget_weekview = (
					new _dali.widgets.class_widget_weekview(
						this.get_entries,
						{
							"action_select_event": (event_key) => this.action_edit_event(event_key),
							"action_select_day": (date) => this.action_create_event({"date": date}),
							"vertical": this.weekview_initial_vertical,
						}
					)
				);
				let dom_wrapper = document.createElement("div");
				dom_wrapper.classList.add("widget-multiview-weekview");
				await this.widget_weekview.load(dom_wrapper);
				this.dom_context.appendChild(dom_wrapper);
			}
			// listview
			{
				this.widget_listview = (
					new _dali.widgets.class_widget_listview(
						this.get_entries,
						{
							"action_select": (event_key) => this.action_edit_event(event_key),
							"action_add": () => this.action_create_event(),
						}
					)
				);
				let dom_wrapper = document.createElement("div");
				dom_wrapper.classList.add("widget-multiview-listview");
				await this.widget_listview.load(dom_wrapper);
				this.dom_context.appendChild(dom_wrapper);
			}
			this.set_view_mode(this.initial_view_mode);
			
			return Promise.resolve<void>(undefined);
		}
			
	}
	
}
