/*
Espe | Ein schlichtes Werkzeug zur Mitglieder-Verwaltung | Frontend
Copyright (C) 2024 Christian Fraß

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see 
<https://www.gnu.org/licenses/>. 
 */

namespace _espe
{
	
	/**
	 * @todo outsource to plankton
	 */
    export class class_input_set implements lib_plankton.zoo_input.interface_input<Set<string>>
    {
        
        /**
         */
		private options : Array<string>;
		
		
        /**
         */
		private show_element : ((element : string) => string);
		
		
        /**
         */
        private read_only : boolean;
        
        
        /**
         */
        private checkboxes : Map<string, HTMLInputElement>;
        
        
        /**
         */
        public constructor(
			options : Array<string>,
			show_element : ((element : string) => string),
			{
				"read_only": read_only = false,
			} : {
				read_only ?: boolean;
			} = {
			}
		)
		{
			this.show_element = show_element;
			this.options = options;
			this.read_only = read_only;
			this.checkboxes = new Map<string, HTMLInputElement>();
		}
		
		
        /**
         * [implementation]
         */
        public setup(
			parent : HTMLElement
		) : Promise<void>
        {
			for (const option of this.options)
			{
				// label
				const element_container : HTMLElement = document.createElement("label");
				element_container.classList.add("plankton_input_set_option");
				element_container.setAttribute("rel", option);
				// checkbox
				{
					const element_checkbox : HTMLInputElement = document.createElement("input");
					element_checkbox.setAttribute("type", "checkbox");
					(this.read_only && element_checkbox.setAttribute("disabled", "disabled"));
					element_container.appendChild(element_checkbox);
					this.checkboxes.set(option, element_checkbox);
				}
				// text
				{
					const element_text : HTMLElement = document.createElement("span");
					element_text.textContent = this.show_element(option);
					element_container.appendChild(element_text);
				}
				parent.appendChild(element_container);
			}
			return Promise.resolve<void>(undefined);
		}
		
		
        /**
         * [implementation]
         */
        public read(
        ) : Promise<Set<string>>
        {
			const result : Set<string> = new Set<string>();
			for (const option of this.options)
			{
				const element_checkbox : HTMLInputElement = this.checkboxes.get(option);
				if (element_checkbox.checked) {
					result.add(option);
				}
			}
			return Promise.resolve<Set<string>>(result);
		}
		
		
        /**
         * [implementation]
         */
        public write(
			value : Set<string>
		) : Promise<void>
        {
			for (const option of this.options)
			{
				const element_checkbox : HTMLInputElement = this.checkboxes.get(option);
				element_checkbox.checked = value.has(option);
			}
			return Promise.resolve<void>(undefined);
		}
		
    }
    	
}
