/*
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_special_number_input
		implements lib_plankton.zoo_widget.interface_widget
	{
		
		/**
		 */
		private action_change : ((int) => Promise<void>);
		
		
		/**
		 */
		private label : (null | string);
		
		
		/**
		 */
		private minimum : (null | int);
		
		
		/**
		 */
		private maximum : (null | int);
		
		
		/**
		 */
		private value : int;
		
		
		/**
		 */
		private dom_root : (null | HTMLElement);
		
		
		/**
		 */
		public constructor(
			{
				"label": label = null,
				"minimum": minimum = null,
				"maximum": maximum = null,
				"initial_value": initial_value = 0,
				"action_change": action_change = ((value) => Promise.resolve<void>(undefined)),
			}
			:
			{
				label ?: (null | string);
				minimum ?: (null | int);
				maximum ?: (null | int);
				initial_value ?: int;
				action_change ?: ((int) => Promise<void>);
			}
			=
			{
			}
		)
		{
			this.label = label;
			this.minimum = minimum;
			this.maximum = maximum;
			this.value = initial_value;
			this.action_change = action_change;
			this.dom_root = null;
		}
		
		
		/**
		 */
		public set_value(
			value : int
		) : void
		{
			this.value = value;
			const dom_input : HTMLInputElement = (this.dom_root.querySelector(".widget-special_number_input-input") as HTMLInputElement);
			dom_input.value = value.toFixed(0);
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : HTMLElement
		)
		: Promise<void>
		{
			const dom_root = await _dali.helpers.element_from_template(
				"widget-special_number_input",
				"main",
				{
					"label": this.label,
				}
			);
			
			const dom_input : HTMLInputElement = (dom_root.querySelector(".widget-special_number_input-input") as HTMLInputElement);
			
			// listeners
			{
				dom_input.addEventListener(
					"change",
					() => {
						const value : int = parseInt(dom_input.value);
						if (
							((this.minimum === null) || (value >= this.minimum))
							&&
							((this.maximum === null) || (value <= this.maximum))
						)
						{
							this.value = value;
							this.action_change(this.value);
						}
						else
						{
							// do nothing
						}
					}
				);
				dom_root.querySelector(".widget-special_number_input-prev").addEventListener(
					"click",
					() => {
						if ((this.minimum === null) || (this.value > this.minimum))
						{
							this.value -= 1;
							dom_input.value = this.value.toFixed(0);
							this.action_change(this.value);
						}
						else
						{
							// do nothing
						}
					}
				);
				dom_root.querySelector(".widget-special_number_input-next").addEventListener(
					"click",
					() => {
						if ((this.maximum === null) || (this.value < this.maximum))
						{
							this.value += 1;
							dom_input.value = this.value.toFixed(0);
							this.action_change(this.value);
						}
						else
						{
							// do nothing
						}
					}
				);
			}
			
			// content
			{
				dom_input.value = this.value.toFixed(0);
			}
			
			target_element.appendChild(dom_root);
			this.dom_root = dom_root;
		}
		
	}

}
