/*
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_calendar_edit
		implements lib_plankton.zoo_widget.interface_widget
	{
		
		/**
		 */
		private groups : Array<{id : _dali.type_group_id; object : _dali.type_group_object;}>;
		
		
		/**
		 */
		private users : Array<{id : _dali.type_user_id; name : string;}>;
		
		
		/**
		 */
		private read_only : boolean;
		
		
		/**
		 */
		private action_cancel ?: (null | (() => void));
		
		
		/**
		 */
		private action_add ?: (null | ((value : _dali.type_calendar_object) => void));
		
		
		/**
		 */
		private action_change ?: (null | ((value : _dali.type_calendar_object) => void));
		
		
		/**
		 */
		private action_remove ?: (null | ((value : _dali.type_calendar_object) => void));
		
		
		/**
		 */
		private initial_value : _dali.type_calendar_object;
		
		
		/**
		 */
		public constructor(
			groups : Array<{id : _dali.type_group_id; object : _dali.type_group_object;}>,
			users : Array<{id : _dali.type_user_id; name : string;}>,
			initial_value : _dali.type_calendar_object,
			{
				"read_only": read_only = false,
				"action_cancel": action_cancel = null,
				"action_add": action_add = null,
				"action_change": action_change = null,
				"action_remove": action_remove = null,
			}
			:
			{
				read_only ?: boolean;
				action_cancel ?: (null | (() => void));
				action_add ?: (null | ((value : _dali.type_calendar_object) => void))
				action_change ?: (null | ((value : _dali.type_calendar_object) => void));
				action_remove ?: (null | ((value : _dali.type_calendar_object) => void));
			}
			=
			{
			}
		)
		{
			this.groups = groups;
			this.users = users;
			this.initial_value = initial_value;
			this.read_only = read_only;
			this.action_cancel = action_cancel;
			this.action_add = action_add;
			this.action_change = action_change;
			this.action_remove = action_remove;
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : HTMLElement
		) : Promise<void>
		{
			const dom_root = await _dali.helpers.element_from_template(
				"widget-calendar_edit",
				"main",
				{
				}
			);
			
			const form : lib_plankton.zoo_form.class_form<
				_dali.type_calendar_object,
				_dali.type_calendar_object
			> = new lib_plankton.zoo_form.class_form<
				_dali.type_calendar_object,
				_dali.type_calendar_object
			>(
				(value) => value,
				(raw) => raw,
				new lib_plankton.zoo_input.class_input_group<any>(
					[
						{
							"name": "name",
							"input": new lib_plankton.zoo_input.class_input_text(),
							"label": lib_plankton.translate.get("calendar.name")
						},
						{
							"name": "hue",
							"input": new lib_plankton.zoo_input.class_input_hue(
							),
							"label": lib_plankton.translate.get("calendar.hue"),
						},
						{
							"name": "access",
							"input": new lib_plankton.zoo_input.class_input_group(
								[
									{
										"name": "public",
										"input": new lib_plankton.zoo_input.class_input_checkbox(),
										"label": lib_plankton.translate.get("calendar.access.public"),
									},
									{
										"name": "default_level",
										"input": _dali.helpers.input_access_level(),
										"label": lib_plankton.translate.get("calendar.access.default_level"),
									},
									{
										"name": "attributed_group",
										"input": _dali.helpers.input_attributed_access_group(this.groups),
										"label": lib_plankton.translate.get("calendar.access.attributed_group"),
									},
									{
										"name": "attributed_user",
										"input": _dali.helpers.input_attributed_access_user(this.users),
										"label": lib_plankton.translate.get("calendar.access.attributed_user"),
									},
								]
							),
							"label": lib_plankton.translate.get("calendar.access.access"),
						},
						{
							"name": "resource",
							"input": new lib_plankton.zoo_input.class_input_hidden(
							),
							"label": lib_plankton.translate.get("calendar.resource"),
						},
					]
				),
				(
					[]
					// add
					.concat(
						((! this.read_only) && (! (this.action_add === null)))
						?
						[
							{
								"label": lib_plankton.translate.get("widget.calendar_edit.actions.add"),
								"procedure": async (get_value, get_representation) => {
									const value : _dali.type_calendar_object = await get_value();
									this.action_add(value);
								}
							},
						]
						:
						[]
					)
					// change
					.concat(
						((! this.read_only) && (! (this.action_change === null)))
						?
						[
							{
								"label": lib_plankton.translate.get("widget.calendar_edit.actions.change"),
								"procedure": async (get_value, get_representation) => {
									const value : _dali.type_calendar_object = await get_value();
									this.action_change(value);
								}
							},
						]
						:
						[]
					)
					// remove
					.concat(
						((! this.read_only) && (! (this.action_remove === null)))
						?
						[
							{
								"label": lib_plankton.translate.get("widget.calendar_edit.actions.remove"),
								"procedure": async (get_value, get_representation) => {
									if (! window.confirm(lib_plankton.translate.get("common.confirm_deletion")))
									{
										// do nothing
									}
									else
									{
										const value : _dali.type_calendar_object = await get_value();
										this.action_remove(value);
									}
								}
							},
						]
						:
						[]
					)
					// cancel
					.concat(
						(! (this.action_cancel === null))
						?
						[
							{
								"label": lib_plankton.translate.get("common.cancel"),
								"procedure": async () => {
									this.action_cancel();
								}
							},
						]
						:
						[]
					)
				)
			);
			await form.setup(dom_root);
			await form.input_lock(this.read_only);
			await form.input_write(this.initial_value);
			
			target_element.appendChild(dom_root);
			form.input_focus();
		}
		
	}
	
}
