/*
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_login_internal
		implements lib_plankton.zoo_widget.interface_widget
	{
		
		/**
		 */
		private preparation_data : any;
		
		
		/**
		 */
		private initial_name : (null | string);
		
		
		/**
		 * [hook]
		 */
		private action_cancel : (null | (() => void));
		
		
		/**
		 * [hook]
		 */
		private action_success : (null | (() => void));
		
		
		/**
		 */
		public constructor(
			preparation_data : any,
			{
				"initial_name": initial_name = null,
				"action_cancel": action_cancel = null,
				"action_success": action_success = null,
			}
			:
			{
				initial_name ?: (null | string);
				action_cancel ?: (null | (() => void));
				action_success ?: (null | (() => void));
			}
			=
			{
			}
		)
		{
			this.preparation_data = preparation_data;
			this.initial_name = initial_name;
			this.action_cancel = action_cancel;
			this.action_success = action_success;
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : HTMLElement
		)
		: Promise<void>
		{
			const form : lib_plankton.zoo_form.class_form<
				{name : string; password : string;},
				{name : string; password : string;}
			> = new lib_plankton.zoo_form.class_form<
				{name : string; password : string;},
				{name : string; password : string;}
			>(
				x => x,
				x => x,
				new lib_plankton.zoo_input.class_input_group<
					{name : string; password : string;}
				>(
					[
						{
							"name": "name",
							"input": new lib_plankton.zoo_input.class_input_text(),
							"label": lib_plankton.translate.get("widget.login.internal.name"),
						},
						{
							"name": "password",
							"input": new lib_plankton.zoo_input.class_input_password(),
							"label": lib_plankton.translate.get("widget.login.internal.password"),
						},
					]
				),
				(
					[]
					.concat(
						[
							{
								"label": lib_plankton.translate.get("widget.login.internal.do"),
								"procedure": async (get_value, get_representation) => {
									const value : any = await get_value();
									try
									{
										await _dali.backend.session_begin(
											value.name,
											value.password
										);
										if (this.action_success !== null) this.action_success();
									}
									catch (error)
									{
										// todo
									}
								}
							}
						]
					)
					.concat(
						(this.action_cancel === null)
						?
						[]
						:
						[
							{
								"label": lib_plankton.translate.get("common.cancel"),
								"procedure": () => {
									this.action_cancel();
								}
							}
						]
					)
				)
			);
			target_element.innerHTML = "";
			await form.setup(target_element);
			await form.input_write(
				{
					"name": this.initial_name,
					"password": "",
				}
			);
			form.input_focus();
		}
		
	}
	
}
