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.oauth-1")
				# 	value: "oauth-1"
				]
			,
				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.oauth_client_key.label")
				type: CUI.Input
				regexp: "^[a-z0-9]*$"
				regexp_flags: "i"
				prevent_invalid_input: true
				name: "oauth_client_key"
			,
				form:
					label: $$("wordpress.config.oauth_client_secret.label")
				type: CUI.Input
				regexp: "^[a-z0-9]*$"
				regexp_flags: "i"
				prevent_invalid_input: true
				name: "oauth_client_secret"
			,
				type: CUI.FormButton
				name: "oauth_btn_generate"
				text: $$("wordpress.config.oauth_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"

					url = baseUrl + "/oauth1?"+
						CUI.encodeUrlData(
							wp_url: parent_data.url
							client_key: data.oauth_client_key
							client_secret: data.oauth_client_secret
						)

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

					# access_token needed to call plugin endpoint in fylr
					if ez5.session["data"]
						access_token = ez5.session["data"]["access_token"]
						if access_token
							url += "&access_token=" + access_token

					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 ["oauth_token", "oauth_token_secret"]
									form.getFieldsByName(key)[0].setValue(adata[key], no_trigger: false)
							return

			,
				form:
					label: $$("wordpress.config.oauth_token.label")
				type: CUI.Input
				name: "oauth_token"
			,
				form:
					label: $$("wordpress.config.oauth_token_secret.label")
				type: CUI.Input
				name: "oauth_token_secret"
			]

		oauth_fields = [
			"oauth_client_key",
			"oauth_client_secret",
			"oauth_btn_generate",
			"oauth_token",
			"oauth_token_secret"
		]

		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 "oauth-1"
				# 	show = oauth_fields
				# 	hide = http_auth_fields
				when "http-auth"
					hide = oauth_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())
