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",
				"default",
				{
					"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 : _zeitbild.frontend_web.type.enum_access_level;
						attributed : lib_plankton.map.type_map<
							_zeitbild.frontend_web.type.user_id,
							_zeitbild.frontend_web.type.enum_access_level
						>;
					};
					resource_kind : string;
				}
			> = new lib_plankton.zoo_form.class_form<
				_zeitbild.frontend_web.type.calendar_object,
				{
					name : string;
					access : {
						default_level : _zeitbild.frontend_web.type.enum_access_level;
						attributed : lib_plankton.map.type_map<
							_zeitbild.frontend_web.type.user_id,
							_zeitbild.frontend_web.type.enum_access_level
						>;
					};
					resource_kind : string;
				}
			>(
				(calendar_object) => ({
					"name": calendar_object.name,
					"access": calendar_object.access,
					"resource_kind": calendar_object.resource.kind,
				}),
				(raw) => ({
					"name": raw.name,
					"access": raw.access,
					"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",
							"input": new lib_plankton.zoo_input.class_input_group(
								[
									{
										"name": "default_level",
										"input": _zeitbild.frontend_web.helpers.input_access_level(),
										"label": lib_plankton.translate.get("calendar.access.default_level"),
									},
									{
										"name": "attributed",
										"input": await _zeitbild.frontend_web.helpers.input_attributed_access(),
										"label": lib_plankton.translate.get("calendar.access.attributed"),
									},
								]
							),
							"label": lib_plankton.translate.get("calendar.access.access"),
						},
						{
							"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": "overview",
										"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);
		}
	);

}
