namespace _zeitbild.frontend_web.pages
{
	
	/**
	 */
	lib_plankton.zoo_page.register(
		"calendar_add",
		async (parameters, target_element) => {
			target_element.innerHTML = "";
			target_element.innerHTML = await _zeitbild.frontend_web.helpers.template_coin(
				"calendar_add",
				{
					"label": lib_plankton.translate.get("page.calendar_add.title")
				}
			);
			const form : lib_plankton.zoo_form.class_form<
				_zeitbild.frontend_web.type.calendar_object,
				{
					name : string;
					access_default_level : string;
					resource_kind : string;
				}
			> = new lib_plankton.zoo_form.class_form<
				_zeitbild.frontend_web.type.calendar_object,
				{
					name : string;
					access_default_level : string;
					resource_kind : string;
				}
			>(
				(calendar_object) => ({
					"name": calendar_object.name,
					"access_default_level": (() => {
						switch (calendar_object.access.default_level) {
							case _zeitbild.frontend_web.type.enum_access_level.none: return "none";
							case _zeitbild.frontend_web.type.enum_access_level.view: return "view";
							case _zeitbild.frontend_web.type.enum_access_level.edit: return "edit";
							case _zeitbild.frontend_web.type.enum_access_level.admin: return "admin";
						}
					}) (),
					"resource_kind": calendar_object.resource.kind,
				}),
				(raw) => ({
					"name": raw.name,
					"access": {
						"default_level": (() => {
							switch (raw.access_default_level) {
								case "none": return _zeitbild.frontend_web.type.enum_access_level.none;
								case "view": return _zeitbild.frontend_web.type.enum_access_level.view;
								case "edit": return _zeitbild.frontend_web.type.enum_access_level.edit;
								case "admin": return _zeitbild.frontend_web.type.enum_access_level.admin;
							}
						}) (),
						"attributed": lib_plankton.map.hashmap.implementation_map(
							lib_plankton.map.hashmap.make(
								x => x.toFixed(0)
							)
						),
					},
					"resource": (() => {
						switch (raw.resource_kind) {
							case "local": {
								return {
									"kind": "local",
									"data": {
										"events": [],
									}
								};
								break;
							}
							case "caldav": {
								return {
									"kind": "caldav",
									"data": {
										"url": "", // TODO
										"read_only": true, // TODO
									}
								};
								break;
							}
							default: {
								throw (new Error("invalid resource kind: " + raw.resource_kind));
								break;
							}
						}
					}) (),
				}),
				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": "access_default_level",
							"input": new lib_plankton.zoo_input.class_input_selection(
								[
									{
										"value": "none",
										"label": lib_plankton.translate.get("access_level.none"),
									},
									{
										"value": "view",
										"label": lib_plankton.translate.get("access_level.view")
									},
									{
										"value": "edit",
										"label": lib_plankton.translate.get("access_level.edit")
									},
									{
										"value": "admin",
										"label": lib_plankton.translate.get("access_level.admin")
									},
								]
							),
							"label": lib_plankton.string.coin(
								"{{default_level}}",
								{
									"head": lib_plankton.translate.get("calendar.access.access"),
									"default_level": lib_plankton.translate.get("calendar.access.default_level"),
								}
							)
						},
						{
							"name": "resource_kind",
							"input": new lib_plankton.zoo_input.class_input_selection(
								[
									{
										"value": "local",
										"label": lib_plankton.translate.get("resource.kinds.local.title")
									},
									{
										"value": "caldav",
										"label": lib_plankton.translate.get("resource.kinds.caldav.title")
									},
								]
							),
							"label": lib_plankton.translate.get("resource.kind")
						},
					]
				),
				[
					{
						"label": lib_plankton.translate.get("page.calendar_add.actions.do"),
						"target": "submit",
						"procedure": async (get_value, get_representation) => {
							const value : any = await get_value();
							try {
								await _zeitbild.frontend_web.backend.calendar_add(
									value
								);
								lib_plankton.zoo_page.set(
									{
										"name": "events",
										"parameters": {}
									}
								);
							}
							catch (error) {
								// do nothing
								/*
								lib_plankton.zoo_page.set(
									{
										"name": "event_add",
										"parameters": {
										}
									}
								);
								 */
							}
						}
					},
				]
			);
			await form.setup(document.querySelector("#calendar_add_form"));
			return Promise.resolve<void>(undefined);
		}
	);

}
