class BaseConfigWordpress extends BaseConfigPlugin

	getFieldDefFromParm: (baseConfig, pname, def, parent_def) ->
		if def.plugin_type != "wordpress-auth"
			return

		console.debug "BaseConfigWordpress.getFieldDefFromParm:", @data, pname, def, baseConfig.locaKey("parameter")

		field =
			type: CUI.Form
			name: "auth"
			form:
				column: "maximize"
			onDataInit: (_form, _data) =>
				if not _data.uuid
					_data.uuid = ez5.generateUUID()
				set_visibility(_form)
				return
			onDataChanged: (data, df) =>
				set_visibility(df.getForm())
			fields: [
				type: CUI.Select
				name: "auth_type"
				form:
					label: $$("wordpress.config.auth_type.label")
				options: [
					text: $$("wordpress.config.auth_type.option.http-auth")
					value: "http-auth"
				,
					text: $$("wordpress.config.auth_type.option.oauth2")
					value: "oauth2"
				]
			,
				form:
					label: $$("wordpress.config.http_auth_login.label")
				type: CUI.Input
				name: "http_auth_login"
			,
				form:
					label: $$("wordpress.config.http_auth_password.label")
				type: CUI.Input
				name: "http_auth_password"
			,
				form:
					label: $$("wordpress.config.oauth2_client_id.label")
				type: CUI.Input
				regexp: "^[a-z0-9]*$"
				regexp_flags: "i"
				prevent_invalid_input: true
				name: "oauth2_client_id"
			,
				form:
					label: $$("wordpress.config.oauth2_client_secret.label")
				type: CUI.Input
				regexp: "^[a-z0-9]*$"
				regexp_flags: "i"
				prevent_invalid_input: true
				name: "oauth2_client_secret"
			,
				type: CUI.FormButton
				name: "oauth2_btn_generate"
				text: $$("wordpress.config.oauth2_client_secret.button.generate")
				onClick: (ev, df) =>
					form = df.getForm()
					data = form.getData()
					parent_data = form.getParentData()

					if ez5.version("6")
						baseUrl = CUI.parseLocation(ez5.pluginManager.getPlugin("easydb-wordpress-plugin").getPluginURL()).path
					else
						baseUrl = "/api/v1/plugin/base/easydb-wordpress-plugin"

					# used for different calls of the same endpoint
					# this is the first step, later steps are defined by the endpoint
					# and will be returned by the oauth2 server
					_state =
						step: "initial"
						# uuid is passed back and forth between plugin and wordpress server over multiple callbacks.
						# so in every callback, the plugin can use it to find the necessary settings
						uuid: data.uuid
					url = baseUrl + "/oauth2?" +
						CUI.encodeUrlData(
							state: CUI.util.utoa(JSON.stringify(_state))
						)

					if window.easydb_server_url and ez5.session.getPref("plugins_load_from_remote")
						url = window.easydb_server_url + url

					console.debug "opening:", url, data, parent_data

					new_window = window.open(url, "_blank", "width=500,height=500,top=50%,left=50%,menubar=0")
					check_win = =>
						if new_window.closed
							console.debug "ignore listener", msg_listener
							CUI.Events.ignore(msg_listener)
						else
							CUI.setTimeout(ms: 100, call: check_win)

					check_win()

					msg_listener = CUI.Events.listen
						type: "message"
						node: window
						call: (ev) =>
							adata = ev.getNativeEvent().data
							console.error "Message received:", adata
							new_window.close()
							if adata.error
								CUI.problem(text: $$(adata.error.code, adata.error.parameters))
							else
								for key in ["oauth2_access_token", "oauth2_token_type"]
									form.getFieldsByName(key)[0].setValue(adata[key], no_trigger: false)
							return
			,
				form:
					label: $$("wordpress.config.oauth2_access_token.label")
				type: CUI.Input
				name: "oauth2_access_token"
			,
				form:
					label: $$("wordpress.config.oauth2_token_type.label")
				type: CUI.Input
				name: "oauth2_token_type"
			]

		oauth2_fields = [
			"oauth2_client_id",
			"oauth2_client_secret",
			"oauth2_btn_generate",
			"oauth2_access_token",
			"oauth2_token_type",
		]

		http_auth_fields = [
			"http_auth_login",
			"http_auth_password"
		]

		set_visibility = (form) =>

			show = []
			hide = []

			console.debug "auth type: ", form.getData().auth_type
			switch form.getData().auth_type
				when "oauth2"
					show = oauth2_fields
					hide = http_auth_fields
				when "http-auth"
					hide = oauth2_fields
					show = http_auth_fields

			for key in show
				form.getFieldsByName(key)[0].show(true)

			for key in hide
				form.getFieldsByName(key)[0].hide(true)

			CUI.Events.trigger
				type: "content-resize"
				node: form

		field


CUI.ready =>
	BaseConfig.registerPlugin(new BaseConfigWordpress())
