namespace _zeitbild.frontend_web.widgets.listview
{
	
	/**
	 */
	type type_entry = {
		calendar_id : _zeitbild.frontend_web.type.calendar_id;
		calendar_name : string;
		access_level : _zeitbild.frontend_web.type.enum_access_level;
		event_id : (null | _zeitbild.frontend_web.type.local_resource_event_id);
		event_object : _zeitbild.frontend_web.type.event_object;
	};
	
	
	/**
	 */
	type type_get_entries = (
		(
			from_pit : lib_plankton.pit.type_pit,
			to_pit : lib_plankton.pit.type_pit,
			calendar_ids : Array<_zeitbild.frontend_web.type.calendar_id>
		)
		=>
		Promise<Array<type_entry>>
	);
	
	
	/**
	 */
	export class class_widget_listview extends _zeitbild.class_widget
	{
		
		/**
		 */
		private get_entries : type_get_entries;
		
		
		/**
		 */
		private action_select_event : (
			(
				calendar_id : _zeitbild.frontend_web.type.calendar_id,
				access_level : _zeitbild.frontend_web.type.enum_access_level,
				event_id : _zeitbild.frontend_web.type.local_resource_event_id
			)
			=>
			void
		);
		
		
		/**
		 */
		private action_add : (
			(
			)
			=>
			void
		);
		
		
		/**
		 */
		public constructor(
			get_entries : type_get_entries,
			options : {
				action_select_event ?: (
					(
						calendar_id : _zeitbild.frontend_web.type.calendar_id,
						access_level : _zeitbild.frontend_web.type.enum_access_level,
						event_id : _zeitbild.frontend_web.type.local_resource_event_id
					)
					=>
					void
				);
				action_add ?: (
					(
					)
					=>
					void
				);
			} = {}
		)
		{
			options = Object.assign(
				{
					"action_select_event": (calendar_id, access_level, event_id) => {},
					"action_select_add": () => {},
				},
				options
			);
			super();
			this.get_entries = get_entries;
			this.action_select_event = options.action_select_event;
			this.action_add = options.action_add;
		}
		
		
		/**
		 * [implementation]
		 */
		public async load(
			target_element : Element
		) : Promise<void>
		{
			const now_pit : lib_plankton.pit.type_pit = lib_plankton.pit.now();
			const from_pit : lib_plankton.pit.type_pit = now_pit;
			const to_pit : lib_plankton.pit.type_pit = lib_plankton.pit.shift_week(now_pit, +2);
			const entries : Array<type_entry> = await this.get_entries(
				from_pit,
				to_pit,
				null
			);
			entries.sort(
				(x, y) => (
					lib_plankton.pit.from_datetime(x.event_object.begin)
					-
					lib_plankton.pit.from_datetime(y.event_object.begin)
				)
			);
			
			// view
			{
				target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
					"widget-listview",
					"main",
					{
						"entries": (
							(
								await _zeitbild.frontend_web.helpers.promise_row<string>(
									entries
									.map(
										(entry) => () => _zeitbild.frontend_web.helpers.template_coin(
											"widget-listview",
											"entry",
											{
												"name": entry.event_object.name,
												"calendar": entry.calendar_name,
												"when": _zeitbild.frontend_web.helpers.timespan_format(entry.event_object.begin, entry.event_object.end),
												"label_location": lib_plankton.translate.get("event.location"),
												"location": (
													(entry.event_object.location === null)
													?
													"?"
													:
													entry.event_object.location
												),
												"label_description": lib_plankton.translate.get("event.description"),
												"description": (
													(entry.event_object.description === null)
													?
													"?"
													:
													entry.event_object.description
												),
												"raw": JSON.stringify(entry),
												"color": lib_plankton.color.output_hex(
													lib_plankton.color.give_generic(
														(entry.calendar_id - 1),
														{
															"saturation": 0.375,
															"value": 0.375,
														}
													),
												),
												"rel": lib_plankton.string.coin(
													"{{calendar_id}}/{{event_id}}",
													{
														"calendar_id": entry.calendar_id.toFixed(0),
														"event_id": (
															(! (entry.event_id === null))
															?
															entry.event_id.toFixed(0)
															:
															"-"
														),
													}
												),
											},
										)
									)
								)
							)
							.join("")
						),
					}
				);
			}
			
			// control
			{
				target_element.querySelectorAll(".listview-entry").forEach(
					(element) => {
						element.addEventListener(
							"click",
							() => {
								const rel : string = element.getAttribute("rel");
								const parts : Array<string> = rel.split("/");
								const calendar_id : _zeitbild.frontend_web.type.calendar_id = parseInt(parts[0]);
								const event_id : (null | _zeitbild.frontend_web.type.local_resource_event_id) = (
									parts[1] === "-"
									?
									null
									:
									parseInt(parts[1])
								);
								this.action_select_event(
									calendar_id,
									event_id,
									_zeitbild.frontend_web.type.enum_access_level.admin // TODO
								);
							}
						);
					}
				);
			}
			
			return Promise.resolve<void>(undefined);
		}
			
	}
	
}
