/**
 * Color Picker Field Block
 */

(function (blocks, element, blockEditor, components, i18n) {
	const { registerBlockType } = blocks;
	const { createElement: el, Fragment, useEffect } = element;
	const { InspectorControls, useBlockProps } = blockEditor;
	const { PanelBody, TextControl, ToggleControl, ColorPicker } = components;
	const { __ } = i18n;

	registerBlockType('giraforms/color-picker-field', {
		icon: {
			src: 'art',
			foreground: '#3858e9'
		},
		edit: function (props) {
			const { attributes, setAttributes, clientId } = props;
			const {
				fieldId,
				label,
				defaultValue,
				required,
				requiredMessage,
				helpText,
				showHexInput
			} = attributes;

			// Generate field ID if not set
			useEffect(() => {
				if (!fieldId) {
					setAttributes({
						fieldId: 'color_' + Math.random().toString(36).substring(2, 11),
					});
				}
			}, []);

			const blockProps = useBlockProps({
				className: 'giraforms-field giraforms-color-picker-field'
			});

			return el(
				Fragment,
				null,
				// Inspector Controls
				el(
					InspectorControls,
					null,
					el(
						PanelBody,
						{ title: __('Field Settings', 'giraforms'), initialOpen: true },
						el(TextControl, {
							label: __('Label', 'giraforms'),
							value: label,
							onChange: (value) => setAttributes({ label: value }),
						}),
						el(ToggleControl, {
							label: __('Required Field', 'giraforms'),
							checked: required,
							onChange: (value) => setAttributes({ required: value }),
						}),
						required && el(TextControl, {
							label: __('Required message', 'giraforms'),
							value: requiredMessage,
							onChange: (val) => setAttributes({ requiredMessage: val }),
							placeholder: __('This field is required.', 'giraforms'),
							help: __('Overrides the default required error message for this field.', 'giraforms'),
						}),
						el(TextControl, {
							label: __('Help text', 'giraforms'),
							value: helpText,
							onChange: (value) => setAttributes({ helpText: value }),
						})
					),
					el(
						PanelBody,
						{ title: __('Default Color', 'giraforms'), initialOpen: false },
						el(ColorPicker, {
							color: defaultValue,
							onChange: (value) => setAttributes({ defaultValue: value }),
							enableAlpha: false,
						})
					),
					el(
						PanelBody,
						{ title: __('Display Options', 'giraforms'), initialOpen: false },
						el(ToggleControl, {
							label: __('Show Hex Input', 'giraforms'),
							checked: showHexInput,
							onChange: (value) => setAttributes({ showHexInput: value }),
							help: __('Allow users to type hex color codes', 'giraforms'),
						})
					),
					// Conditional Logic Panel
					
				),
				// Block Content
				el(
					'div',
					blockProps,
					el(
						'label',
						{ className: required ? 'required' : '' },
						label
					),
					el(
						'div',
						{ className: 'giraforms-color-picker-wrapper' },
						el(
							'div',
							{ className: 'giraforms-color-preview', style: { backgroundColor: defaultValue } },
							el('input', {
								type: 'color',
								value: defaultValue,
								disabled: true,
							})
						),
						showHexInput && el('input', {
							type: 'text',
							value: defaultValue,
							placeholder: '#000000',
							disabled: true,
							className: 'giraforms-color-hex-input',
						})
					),
					helpText && el('p', { className: 'giraforms-help-text' }, helpText)
				)
			);
		},

		save: function (props) {
			const { attributes } = props;
			const {
				fieldId,
				label,
				defaultValue,
				required,
				requiredMessage,
				helpText,
				showHexInput
			} = attributes;

			const blockProps = useBlockProps.save({
				className: 'giraforms-field giraforms-color-picker-field',
				'data-field-id': fieldId,
			});

			return el(
				'div',
				blockProps,
				el(
					'label',
					{ htmlFor: fieldId, className: required ? 'required' : '' },
					label
				),
				el(
					'div',
					{ className: 'giraforms-color-picker-wrapper' },
					el(
						'div',
						{ className: 'giraforms-color-preview', style: { backgroundColor: defaultValue } },
						el('input', {
							type: 'color',
							id: fieldId,
							name: fieldId,
							value: defaultValue,
							required: required,
							'data-required-message': requiredMessage || undefined,
							className: 'giraforms-color-input',
						})
					),
					showHexInput && el('input', {
						type: 'text',
						id: fieldId + '_hex',
						value: defaultValue,
						placeholder: '#000000',
						pattern: '^#[0-9A-Fa-f]{6}$',
						maxLength: '7',
						className: 'giraforms-color-hex-input giraforms-input',
					})
				),
				helpText && el('div', { className: 'giraforms-field-description' }, helpText)
			);
		},
	});
})(
	window.wp.blocks,
	window.wp.element,
	window.wp.blockEditor,
	window.wp.components,
	window.wp.i18n
);
